当我按1时文本字段应显示1但它没有。为什么?

时间:2014-02-05 10:01:44

标签: java swing awt

public class Scal {

    JFrame mainframe;
    JPanel controlsPanel;
    JPanel inputPanel;
    JPanel opPanel;
    JTextField inputbox;
    String input1 = "";

    public Scal(){
        prepareCAL();
    }

    public static void main(String[] args) {
        Scal cal = new Scal();
        cal.showcontrols();
    }

    private void prepareCAL() {
        mainframe = new JFrame();
        mainframe.setSize(400, 400);
        mainframe.setBackground(Color.GRAY);        

        inputPanel = new JPanel(); 
        controlsPanel = new JPanel();
        controlsPanel.setSize(new Dimension(200, 250));

        opPanel = new JPanel();
        opPanel.setSize(200, 250);
//      opPanel.setMaximumSize(new Dimension(200, 250));

        mainframe.add(inputPanel, BorderLayout.NORTH);
        mainframe.add(controlsPanel, BorderLayout.CENTER);
        mainframe.add(opPanel, BorderLayout.EAST);
        mainframe.setVisible(true);
    }

    private void showcontrols() {
        inputbox = new JTextField(30);
        inputbox.setText("");

        inputPanel.add(inputbox);

        JButton btn1 = new JButton("1");
        btn1.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("1");

        JButton btn2 = new JButton("2");
        btn2.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("2");

        JButton btn3 = new JButton("3");
        btn3.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("3");

        JButton btn4 = new JButton("4");
        btn4.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("4");

        JButton btn5 = new JButton("5");
        btn5.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("5");

        JButton btn6 = new JButton("6");
        btn6.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("6");

        JButton btn7 = new JButton("7");
        btn7.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("7");

        JButton btn8 = new JButton("8");
        btn8.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("8");

        JButton btn9 = new JButton("9");
        btn9.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("9");

        JButton btnclear = new JButton("C");
        btnclear.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("c");

        JButton btn0 = new JButton("0");
        btn0.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("0");

        JButton btnequal = new JButton("=");
        btnequal.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("=");

        controlsPanel.add(btn1);
        controlsPanel.add(btn2);
        controlsPanel.add(btn3);
        controlsPanel.add(btn4);
        controlsPanel.add(btn5);
        controlsPanel.add(btn6);        
        controlsPanel.add(btn7);
        controlsPanel.add(btn8);
        controlsPanel.add(btn9);
        controlsPanel.add(btnclear);
        controlsPanel.add(btn0);
        controlsPanel.add(btnequal);

        JButton btnPlus = new JButton("+");
        btnPlus.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("+");

        JButton btnSub = new JButton("-");
        btnSub.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("-");

        JButton btnMul = new JButton("*");
        btnMul.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("*");

        JButton btnDiv = new JButton("/");
        btnDiv.setPreferredSize(new Dimension(50, 50));
        btn1.setActionCommand("/");

        btn1.addActionListener(new ClickListener());
        btn2.addActionListener(new ClickListener());
        btn3.addActionListener(new ClickListener());
        btn4.addActionListener(new ClickListener());
        btn5.addActionListener(new ClickListener());
        btn6.addActionListener(new ClickListener());
        btn7.addActionListener(new ClickListener());
        btn8.addActionListener(new ClickListener());
        btn9.addActionListener(new ClickListener());
        btn0.addActionListener(new ClickListener());
        btnPlus.addActionListener(new ClickListener());
        btnSub.addActionListener(new ClickListener());
        btnMul.addActionListener(new ClickListener());
        btnDiv.addActionListener(new ClickListener());
        btnclear.addActionListener(new ClickListener());
        btnequal.addActionListener(new ClickListener());


        opPanel.add(btnPlus);
        opPanel.add(btnSub);
        opPanel.add(btnMul);
        opPanel.add(btnDiv);
        mainframe.setVisible(true);     
    }

    private class ClickListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == "1"){
                inputbox.setText("1");
            }

        }
    }

}

我使用事件来更改文本框中的值。我正在制作计算器。他们的另一个更好的方法是这样做吗? 我怎样才能正确使用这个逻辑为什么它能够正常工作。到目前为止我只实现了1。只有1才能显示在文本字段中。 需要帮助

2 个答案:

答案 0 :(得分:8)

而不是这个

if (e.getSource() == "1")

你想要

JButton source = (JButton)e.getSource();
if ("1".equals(source.getActionCommand()))

或者只是简单地

if ("1".equals(e.getActionCommand()))

另一种选择是检查JButton对象本身

JButton source = (JButton)e.getSource();
if (source == btn1) {}

或者只是

if (e.getSource() == btn1) {}

所以你有选择


注意

getSource()返回的是触发ActionEvent的对象,即JButton对象。 JButton永远不会== "1"。您需要获取返回String的actionCommand并将其与"1"

进行比较

<强>更新

你正在做的是重复这样做

btn1.setActionCommand("1");
btn1.setActionCommand("2");
btn1.setActionCommand("3");   

您始终为btn1设置操作命令。你需要相应地改变它们

答案 1 :(得分:2)

可能是因为您需要使用invokeLater()来启动任何Swing应用程序。

e.getSource()将返回发生操作的按钮的引用。如果你想坚持这一点,那么它应该是== btn1

另外,制定SSCCE的规则规定您将代码剥离到所需的最小值。当你实现的只是listener for.one按钮时,为什么你有额外的代码?