Итак, я создаю программу, которая преобразует двоичные, шестнадцатеричные и десятичные значения друг в друга. Таким образом, массовый конвертер для трех типов данных. Я выполнил все алгоритмы для преобразования, однако в моем GUI я столкнулся с проблемой. Проблема в том, что мои кнопки не реагируют на нажатие и, следовательно, не запускают код в слушателе действия.
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public menu() {
GUI();
}
public static void main(String args[]) {
new menu();
}
private void GUI() {
JFrame main = new JFrame("Converter App");
main.setSize(1000, 1000);
main.setVisible(true);
main.setLayout(null);
JPanel main_Panel = new JPanel();
main_Panel.setBounds(5,5, 1000, 275);
main_Panel.setBackground(Color.gray);
main_Panel.setVisible(true);
main_Panel.setLayout(null);
JRadioButton binary = new JRadioButton("Binary");
JRadioButton hexadecimal = new JRadioButton("Hexadecimal");
JRadioButton decimal = new JRadioButton("Decimal");
JRadioButton binary_Fraction = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed = new JRadioButton ("Signed Decimal");
ButtonGroup input = new ButtonGroup();
input.add(binary); input.add(decimal); input.add(decimal_Fraction); input.add(decimal_Signed);
input.add(hexadecimal); input.add(binary_Fraction); input.add(binary_Signed);
JLabel input_Ins = new JLabel ("Please choose your input: ");
JLabel output_Ins = new JLabel ("Please choose your output: ");
binary.setBounds(15, 50, 150, 30);
binary.setBackground(Color.white);
binary_Fraction.setBounds(15, 100, 150, 30);
binary_Fraction.setBackground(Color.white);
binary_Signed.setBounds(15, 150, 150, 30);
binary_Signed.setBackground(Color.white);
hexadecimal.setBounds(90, 200, 150, 30);
hexadecimal.setBackground(Color.white);
decimal.setBounds(180, 50, 150, 30);
decimal.setBackground(Color.white);
decimal_Fraction.setBounds(180, 100, 150, 30);
decimal_Fraction.setBackground(Color.white);
decimal_Signed.setBounds(180, 150, 150, 30);
decimal_Signed.setBackground(Color.white);
input_Ins.setBounds( 70, 10, 200, 20);
input_Ins.setBackground(Color.white);
input_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
input_Ins.setOpaque(true);
output_Ins.setBounds( 575, 10, 200, 20);
output_Ins.setBackground(Color.white);
output_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
output_Ins.setOpaque(true);
JRadioButton binary_Out = new JRadioButton("Binary");
JRadioButton hexadecimal_Out = new JRadioButton("Hexadecimal");
JRadioButton decimal_Out = new JRadioButton("Decimal");
JRadioButton binary_Fraction_Out = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed_Out = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction_Out = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed_Out = new JRadioButton ("Signed Decimal");
ButtonGroup output = new ButtonGroup();
output.add(binary_Out); output.add(binary_Fraction_Out); output.add(binary_Signed_Out); output.add(hexadecimal_Out);
output.add(decimal_Out); output.add(decimal_Fraction_Out); output.add(decimal_Signed_Out);
binary_Out.setBounds(500, 50, 150, 30);
binary_Out.setBackground(Color.white);
binary_Fraction_Out.setBounds(500, 100, 150, 30);
binary_Fraction_Out.setBackground(Color.white);
binary_Signed_Out.setBounds(500, 150, 150, 30);
binary_Signed_Out.setBackground(Color.white);
hexadecimal_Out.setBounds(600, 200, 150, 30);
hexadecimal_Out.setBackground(Color.white);
decimal_Out.setBounds(700, 50, 150, 30);
decimal_Out.setBackground(Color.white);
decimal_Fraction_Out.setBounds(700, 100, 150, 30);
decimal_Fraction_Out.setBackground(Color.white);
decimal_Signed_Out.setBounds(700, 150, 150, 30);
decimal_Signed_Out.setBackground(Color.white);
main.add(main_Panel);
main_Panel.revalidate();
main_Panel.repaint();
main_Panel.add(binary); main_Panel.add(hexadecimal); main_Panel.add(decimal); main_Panel.add(binary_Fraction); main_Panel.add(input_Ins); main_Panel.add(output_Ins);
main_Panel.add(decimal_Signed); main_Panel.add(decimal_Fraction); main_Panel.add(binary_Signed);
main_Panel.add(binary_Out); main_Panel.add(hexadecimal_Out); main_Panel.add(decimal_Out); main_Panel.add(binary_Fraction_Out);
main_Panel.add(decimal_Signed_Out); main_Panel.add(decimal_Fraction_Out); main_Panel.add(binary_Signed_Out);
JPanel work = new JPanel();
work.setBounds(0, 300, 600, 400);
work.setBackground(Color.gray);
JButton instructions = new JButton("Instructions");
JLabel user_Input = new JLabel("Please insert an appropriate input: ");
JLabel user_Output = new JLabel("Your converted output is: ");
JLabel instructions_For_Instructions = new JLabel("Please click on the instructions button first.");
JTextField user_in = new JTextField();
JLabel user_out = new JLabel();
JButton source = new JButton("Sources");
JButton convert = new JButton("Convert");
instructions.setBounds(400, 5, 150, 50);
instructions.setBackground( Color.white);
user_Input.setBounds(150, 125, 300, 50);
user_Input.setFont(new Font("Serif", Font.PLAIN, 20));
user_Input.setBackground(Color.white);
user_Input.setOpaque(true);
user_Output.setBounds( 150, 250, 300, 50 );
user_Output.setFont(new Font("Serif", Font.PLAIN, 20));
user_Output.setBackground(Color.white);
user_Output.setOpaque(true);
user_in.setBounds(500, 125, 200, 50);
user_in.setBackground( Color.white);
user_in.setFont(new Font("Serif", Font.PLAIN, 20));
user_in.setDocument(new JTextFieldLimit(10));
user_out.setBounds(500, 250, 200, 50);
user_out.setBackground( Color.white);
user_out.setOpaque(true);
source.setBounds(880, 5, 100, 50);
source.setBackground(Color.white);
instructions_For_Instructions.setBounds(100, 10, 270, 25);
instructions_For_Instructions.setBackground(Color.WHITE);
instructions_For_Instructions.setOpaque(true);
convert.setBounds(750, 200, 100, 50);
convert.setBackground(Color.white);
System.out.println("Yes");
convert.addActionListener(this);
binary.addActionListener(this);
source.addActionListener(this);
main.add(work);
work.add(instructions); work.add(user_Input); work.add(user_Output); work.add(user_in);
work.add(user_out); work.add(source); work.add(instructions_For_Instructions); work.add(convert);
work.setVisible(true);
work.setSize(1000, 400);
work.setLayout(null);
main.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == source) {
JOptionPane.showMessageDialog(null, "YES");
}
if (e.getSource() == convert) {
user_out.setText("Yes");
//binary_To_Dec_Converter();
}
}
public void binary_To_Dec_Converter() {
if (binary.isSelected() ) {
String input_From_User = user_in.getText();
String converted = Integer.toString(Binary_Converter.Binary_To_Dec(input_From_User));
System.out.println(converted);
user_out.setText("YES");
}
}
}