Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的ActionListener在单击时不工作?_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java 为什么我的ActionListener在单击时不工作?

Java 为什么我的ActionListener在单击时不工作?,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,所以我正在创建一个程序,将二进制、十六进制和十进制值相互转换。三种数据类型的质量转换器。我已经完成了转换的所有算法,但是在我的GUI中我遇到了一个问题。问题是我的按钮不会响应被单击的操作,因此不会在操作侦听器中启动代码 class JTextFieldLimit extends PlainDocument { private int limit; JTextFieldLimit(int limit) { super(); this.lim

所以我正在创建一个程序,将二进制、十六进制和十进制值相互转换。三种数据类型的质量转换器。我已经完成了转换的所有算法,但是在我的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");
    }


}



}
类JTextFieldLimit扩展了明文{
私有整数限制;
JTextFieldLimit(整数限制){
超级();
这个极限=极限;
}
JTextFieldLimit(整数极限,布尔上限){
超级();
这个极限=极限;
}
public void insertString(int offset、String str、AttributeSet attr)引发BadLocationException{
如果(str==null)
返回;

如果((getLength()+str.length())请指定不起作用的代码,1)为了更快地获得更好的帮助,请添加or。GUI只需要一个按钮,actionlistener需要做的就是指示它已被单击。2)源代码中只需要一行空白。在
{
或之前的
}
通常也是冗余的。3)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的…PLAF。因此,它们不利于像素完美布局。相反,请使用布局管理器,或与布局填充和边框一起使用。重复问题。请参阅以下链接答案是否定的。