JTextField没有响应JButtons

时间:2016-11-11 14:56:16

标签: java swing

我正在为学校的一个项目制作一个计算器,我遇到了一个重大问题。计算器工作,然后我添加了一个方法,使计算更容易,并使其接受两个以上的数字。之后我的按钮停止工作。我尝试了一些事情,我知道代码是可以访问的。显示也没有错误。

问题:数字按钮拒绝将数字打印到JTextField。

代码:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

    JLabel lbl1;
    JTextField tf;
    JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bAdd, bEqu, bSub, bMult, bDiv, bC, bBS, bDot;
    static double a=0, b=0, c;
    static int lastOperator = 0; // 1 = +, 2 = -, 3 = *, 4 = /.

    // Display everything in the window
    public Calculator() {
        lbl1 = new JLabel();    tf = new JTextField(10);b0 = new JButton("0");
        b1 = new JButton("1");  b4 = new JButton("4");  b7 = new JButton("7");
        b2 = new JButton("2");  b5 = new JButton("5");  b8 = new JButton("8");
        b3 = new JButton("3");  b6 = new JButton("6");  b9 = new JButton("9");
        bAdd = new JButton("+");    bSub = new JButton("-"); bMult = new JButton("*");  bDot = new JButton(".");
        bEqu = new JButton("=");    bC = new JButton("C"); bBS = new JButton("CE");     bDiv = new JButton("/");

        tf.setBounds(30,40,350,30);     bBS.setBounds(40,100,50,100);   bC.setBounds(40,220,50,100);
        b7.setBounds(110,100,50,40);    b8.setBounds(180,100,50,40);    b9.setBounds(250,100,50,40);    bDiv.setBounds(320,100,50,40);  
        b4.setBounds(110,160,50,40);    b5.setBounds(180,160,50,40);    b6.setBounds(250,160,50,40);    bMult.setBounds(320,160,50,40); 
        b1.setBounds(110,220,50,40);    b2.setBounds(180,220,50,40);    b3.setBounds(250,220,50,40);    bSub.setBounds(320,220,50,40);  
        bDot.setBounds(110,280,50,40);  b0.setBounds(180,280,50,40);    bEqu.setBounds(250,280,50,40);  bAdd.setBounds(320,280,50,40);

        add(tf);    add(b7);    add(b8);    add(b9);    add(b4);    add(b5);    add(b6);    add(b1);    add(b2);    add(b3);    add(b0);
        add(bDot);  add(bAdd);  add(bSub);  add(bMult); add(bDiv);  add(bEqu);  add(bC);    add(bBS);       

        b0.addActionListener(this); b4.addActionListener(this); b8.addActionListener(this);
        b1.addActionListener(this); b5.addActionListener(this); b9.addActionListener(this);
        b2.addActionListener(this); b6.addActionListener(this); bAdd.addActionListener(this);
        b3.addActionListener(this); b7.addActionListener(this); bEqu.addActionListener(this);
        bSub.addActionListener(this);   bMult.addActionListener(this);  bC.addActionListener(this);
        bBS.addActionListener(this);    bDiv.addActionListener(this); bDiv.addActionListener(this);
        bDot.addActionListener(this);
    }

    // Metod for calculating
    public void operatorMethod(int lastOperator) {
        if (lastOperator > 0) {
            b = Double.parseDouble(tf.getText());
        }

        switch (lastOperator) {
        case 0: 
            a = Double.parseDouble(tf.getText());
            break;
        case 1: 
            a = a +b;
            break;
        case 2: 
            a = a-b;
            break;
        case 3:
            a = a * b;
            break;
        case 4:
            a = a / b;
            break;
        default:
            a = 0;
            break;
        }
        tf.setText("");
    }

    // Action Listener
        public void actionPerformed(ActionEvent e) {

            // Input
            if (e.getSource() == b0) {
                tf.setText(tf.getText().concat("0"));
            }
            if (e.getSource() == b1) { 
                tf.setText(tf.getText().concat("1"));
            }
            if (e.getSource() == b2) {
                tf.setText(tf.getText().concat("2"));
            }
            if (e.getSource() == b3) {
                tf.setText(tf.getText().concat("3"));
            }
            if (e.getSource() == b4) {
                tf.setText(tf.getText().concat("4"));
            }
            if (e.getSource() == b5) {
                tf.setText(tf.getText().concat("5"));
            }
            if (e.getSource() == b6) {
                tf.setText(tf.getText().concat("6"));
            }
            if (e.getSource() == b7) {
                tf.setText(tf.getText().concat("7"));
            }
            if (e.getSource() == b8) {
                tf.setText(tf.getText().concat("8"));
            }
            if (e.getSource() == b9) {
                tf.setText(tf.getText().concat("9"));
            }
            if (e.getSource() == bDot) {
                tf.setText(tf.getText().concat("."));   
            }

        // Counting
        // Add
        if (e.getSource() == bAdd) {
            operatorMethod(lastOperator);
            lastOperator = 1;
        }

        // Subtract
        if (e.getSource() == bSub) {
            operatorMethod(lastOperator);
            lastOperator = 2;       }

        // Mutliply
        if (e.getSource() == bMult); {
            operatorMethod(lastOperator);
            lastOperator = 3;
        }

        // Divide
        if (e.getSource() == bDiv) {
            operatorMethod(lastOperator);
            lastOperator = 4;
        }

        // Different operations E.g =.
        if (e.getSource() == bEqu) {
            operatorMethod(lastOperator);
            lastOperator = 0;
            tf.setText("" + a);
        }

        if (e.getSource() == bBS) {
            String s = tf.getText();
            tf.setText("");
            for (int i = 0; i < s.length()-1; i++) {
                tf.setText(tf.getText()+s.charAt(i));
            }
        }
        if (e.getSource() == bC) {
            tf.setText("");
        }

    }

    // Create window
    public static void main(String[] args) {
        Calculator f = new Calculator();
        f.setLayout(null);
        f.setLocation(1100,300);
        f.setTitle("Calculatoren!!!!");
        f.setVisible(true);
        f.setSize(410,410);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
    }
}

1 个答案:

答案 0 :(得分:1)

此行中有一个不必要的;if (e.getSource() == bMult); {

使用这个额外字符,if包含一个空语句,并且每按一次按钮后执行以下代码块({ operatorMethod(lastOperator); lastOperator = 3; }),operatorMethod总是删除文本字段的内容。

删除;,问题就会消失。