Java ActionListener不工作

Java ActionListener不工作,java,swing,user-interface,jpanel,actionlistener,Java,Swing,User Interface,Jpanel,Actionlistener,我试图创建一个非常基本的统计计算器程序自定义面板。我有从1到9的按钮来输入数字,还有一个内部类来实现ActionListener来处理它们。这些数字应该显示在名为显示器的JLabel上。程序编译正确,但是,侦听器似乎不工作,当我单击这些按钮时,什么也没有发生。代码如下: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax

我试图创建一个非常基本的统计计算器程序自定义面板。我有从1到9的按钮来输入数字,还有一个内部类来实现
ActionListener
来处理它们。这些数字应该显示在名为
显示器的
JLabel
上。程序编译正确,但是,侦听器似乎不工作,当我单击这些按钮时,什么也没有发生。代码如下:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial") public class StatCalcPanelBeta extends JPanel{

    private class InputHandler implements ActionListener{
        @Override public void actionPerformed(ActionEvent evt){
            String str = evt.toString();
            switch(str){
            case "1":
                inputString.append("1");
                inputField.setText(inputString.toString());
                break;
            case "2":
                inputString.append("2");
                inputField.setText(inputString.toString());
                break;
            case "3":
                inputString.append("3");
                inputField.setText(inputString.toString());
                break;
            case "4":
                inputString.append("4");
                inputField.setText(inputString.toString());
                break;
            case "5":
                inputString.append("5");
                inputField.setText(inputString.toString());
                break;
            case "6":
                inputString.append("6");
                inputField.setText(inputString.toString());
                break;
            case "7":
                inputString.append("7");
                inputField.setText(inputString.toString());
                break;
            case "8":
                inputString.append("8");
                inputField.setText(inputString.toString());
                break;
            case "9":
                inputString.append("9");
                inputField.setText(inputString.toString());
                break;
            }
        }
    }

    private final StatCalc calculator;
    private JLabel display;
    private JPanel displayPanel;
    private JTextField inputField;
    private final StringBuffer inputString;
    private JButton enter;
    private JButton button_1;
    private JButton button_2;
    private JButton button_3;
    private JButton button_4;
    private JButton button_5;
    private JButton button_6;
    private JButton button_7;
    private JButton button_8;
    private JButton button_9;
    private JPanel inputPanel;
    private InputHandler ioListener;
    private JButton count;
    private JButton sum;
    private JButton mean;
    private JButton max;
    private JButton min;
    private JButton standardDeviation;
    private JPanel functionPanel;

    public StatCalcPanelBeta(){
        calculator = new StatCalc();
        inputString = new StringBuffer();
        initialize();
    }

    private void initialize(){
        setLayout(null);

        displayPanel = new JPanel();
        displayPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
                null, null), "Display", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        displayPanel.setBounds(10, 11, 190, 53);
        add(displayPanel);
        displayPanel.setLayout(null);

        display = new JLabel("Display");
        display.setBounds(0, 11, 180, 37);
        displayPanel.add(display);
        display.setHorizontalAlignment(SwingConstants.CENTER);

        inputPanel = new JPanel();
        inputPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
                "Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        inputPanel.setBounds(0, 72, 211, 145);
        this.add(inputPanel);
        inputPanel.setLayout(null);

        ioListener = new InputHandler();

        inputField = new JTextField(inputString.toString());
        inputField.setEditable(false);
        inputField.setBounds(9, 16, 86, 20);
        inputPanel.add(inputField);
        inputField.setColumns(10);

        enter = new JButton("Enter");
        enter.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                try{
                    calculator.enter(Double.parseDouble(inputString.toString()));
                } catch(NumberFormatException e){
                    e.printStackTrace();
                }
                display.setText("");
            }
        });
        enter.setToolTipText("Enters the number displayed on the input field");
        enter.setBounds(110, 16, 89, 23);
        inputPanel.add(enter);

        button_1 = new JButton("1");
        button_1.setBounds(6, 47, 65, 23);
        button_1.addActionListener(ioListener);
        inputPanel.add(button_1);

        button_2 = new JButton("2");
        button_2.setBounds(73, 47, 65, 23);
        button_2.addActionListener(ioListener);
        inputPanel.add(button_2);

        button_3 = new JButton("3");
        button_3.setBounds(140, 47, 65, 23);
        button_3.addActionListener(ioListener);
        inputPanel.add(button_3);

        button_4 = new JButton("4");
        button_4.setBounds(6, 81, 65, 23);
        button_4.addActionListener(ioListener);
        inputPanel.add(button_4);

        button_5 = new JButton("5");
        button_5.setBounds(73, 81, 65, 23);
        button_5.addActionListener(ioListener);
        inputPanel.add(button_5);

        button_6 = new JButton("6");
        button_6.setBounds(140, 81, 65, 23);
        button_6.addActionListener(ioListener);
        inputPanel.add(button_6);

        button_7 = new JButton("7");
        button_7.setBounds(6, 115, 65, 23);
        button_7.addActionListener(ioListener);
        inputPanel.add(button_7);

        button_8 = new JButton("8");
        button_8.setBounds(73, 115, 65, 23);
        button_8.addActionListener(ioListener);
        inputPanel.add(button_8);

        button_9 = new JButton("9");
        button_9.setBounds(140, 115, 65, 23);
        button_9.addActionListener(ioListener);
        inputPanel.add(button_9);

        functionPanel = new JPanel();
        functionPanel.setBorder(new TitledBorder(
                new EtchedBorder(EtchedBorder.LOWERED, null, null), "Functions",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        functionPanel.setBounds(0, 228, 211, 113);
        this.add(functionPanel);
        functionPanel.setLayout(null);

        count = new JButton("Count");
        count.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Integer.toString(calculator.getCount()));
            }
        });
        count.setBounds(6, 16, 65, 23);
        functionPanel.add(count);

        sum = new JButton("Sum");
        sum.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getSum()));
            }
        });
        sum.setBounds(73, 16, 65, 23);
        functionPanel.add(sum);

        mean = new JButton("Mean");
        mean.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMean()));
            }
        });
        mean.setBounds(140, 16, 65, 23);
        functionPanel.add(mean);

        max = new JButton("Maximum");
        max.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMax()));
            }
        });
        max.setBounds(10, 49, 89, 23);
        functionPanel.add(max);

        min = new JButton("Minimum");
        min.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMin()));
            }
        });
        min.setBounds(111, 49, 89, 23);
        functionPanel.add(min);

        standardDeviation = new JButton("Standard Deviation");
        standardDeviation.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getStandardDeviation()));
            }
        });
        standardDeviation.setBounds(33, 83, 147, 23);
        functionPanel.add(standardDeviation);
    }
}
与此相关联的所有其他类都正常工作,其他按钮的侦听器也正常工作

String str = evt.toString();
这将永远不会返回“1”(或任何其他数字)。如果要检索按钮,请使用
evt.getSource()
并将其转换为
JButton
。然后,您可以从
JButton
中检索正确的信息

另一个选项是使用action命令。请参见
JButton#setActionCommand
ActionEvent#getActionCommand
打印输出evt.toString()


我怀疑它只是“1”、“2”之类的…

我得到了解决方案,谢谢@Robin。我使用<代码> .GETTEX/<代码>方法从按钮中获取字符串。您应该认真考虑使用适当的LayOutMeor而不是手动设置边界。