Java计算器将数字添加到文本字段

yve*_*sup 5 java calculator button textfield

我正在制作一个计算器来测试我的java技能.如何在jTextfield中显示数字,直到我按下一个按钮来计算数字; 我希望每个数字都显示在文本字段中.例如,如果我按1和零,我希望文本字段有10.

int num;
JTextField in = new JTextField(20); // input field where numbers will up;

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == bouttons.get(0)) {
        num = 0;
        in.setText("" + num);
    }
    if (e.getSource() == bouttons.get(1)) {
        int num = 1;
        in.setText("" + num);
    }
}
Run Code Online (Sandbox Code Playgroud)

截图

rah*_*rgi 1

你应该追加in.getText()而不是空字符串

int num ;
JTextField in = new JTextField(20); // input field where numbers will up;
public void actionPerformed(ActionEvent e) {



    if (e.getSource() == bouttons.get(0)) {

        num =0;

        in.setText(in.getText() + num);

    }

    if (e.getSource() == bouttons.get(1)) {

        int num = 1;
        in.setText(in.getText() + num);

    }

}
Run Code Online (Sandbox Code Playgroud)