【问题标题】:I'm making a simple calculator and the GUI build keeps freezing我正在制作一个简单的计算器,但 GUI 版本一直冻结
【发布时间】:2014-07-24 00:59:32
【问题描述】:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;

public class Calculator extends JFrame implements ActionListener {

    private JPanel contentPane;
    JButton button, btnNewButton, button_1, button_2, button_3, button_4,
            button_5, button_6, button_7, button_8, btnClear, btnNewButton_1,
            button_9, btnX, button_11;
    JLabel lblNewLabel;
    int v1 = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calculator frame = new Calculator();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Calculator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 250, 415);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnNewButton = new JButton("7");
        btnNewButton.setBounds(10, 130, 52, 37);
        contentPane.add(btnNewButton);
        btnNewButton.setActionCommand("7");
        btnNewButton.addActionListener(this);

        button = new JButton("8");
        button.setBounds(72, 130, 52, 37);
        contentPane.add(button);
        button.setActionCommand("8");
        button.addActionListener(this);

        button_1 = new JButton("9");
        button_1.setBounds(134, 130, 52, 37);
        contentPane.add(button_1);
        button_1.setActionCommand("9");
        button_1.addActionListener(this);

        button_2 = new JButton("6");
        button_2.setBounds(134, 178, 52, 37);
        contentPane.add(button_2);
        button_2.setActionCommand("6");
        button_2.addActionListener(this);

        button_3 = new JButton("5");
        button_3.setBounds(72, 178, 52, 37);
        contentPane.add(button_3);
        button_3.setActionCommand("5");
        button_3.addActionListener(this);

        button_4 = new JButton("4");
        button_4.setBounds(10, 178, 52, 37);
        contentPane.add(button_4);
        button_4.setActionCommand("4");
        button_4.addActionListener(this);

        button_5 = new JButton("3");
        button_5.setBounds(134, 226, 52, 37);
        contentPane.add(button_5);
        button_5.setActionCommand("3");
        button_5.addActionListener(this);

        button_6 = new JButton("2");
        button_6.setBounds(72, 226, 52, 37);
        contentPane.add(button_6);
        button_6.setActionCommand("2");
        button_6.addActionListener(this);

        button_7 = new JButton("1");
        button_7.setBounds(10, 226, 52, 37);
        contentPane.add(button_7);
        button_7.setActionCommand("1");
        button_7.addActionListener(this);

        button_8 = new JButton("0");
        button_8.setBounds(72, 274, 52, 37);
        contentPane.add(button_8);
        button_8.setActionCommand("0");
        button_8.addActionListener(this);

        btnClear = new JButton("Clear ");
        btnClear.setBounds(53, 343, 89, 23);
        contentPane.add(btnClear);
        btnClear.setActionCommand("Clear");
        btnClear.addActionListener(this);

        btnNewButton_1 = new JButton("+");
        btnNewButton_1.setBackground(new Color(255, 255, 224));
        btnNewButton_1.setForeground(Color.DARK_GRAY);
        btnNewButton_1.setBounds(193, 130, 41, 36);
        contentPane.add(btnNewButton_1);
        btnNewButton_1.setActionCommand("+");
        btnNewButton_1.addActionListener(this);

        button_9 = new JButton("-");
        button_9.setBackground(new Color(255, 255, 224));
        button_9.setForeground(Color.DARK_GRAY);
        button_9.setBounds(193, 179, 41, 36);
        contentPane.add(button_9);
        button_9.setActionCommand("-");
        button_9.addActionListener(this);

        btnX = new JButton("x");
        btnX.setBackground(new Color(255, 255, 224));
        btnX.setForeground(Color.DARK_GRAY);
        btnX.setBounds(193, 226, 41, 36);
        contentPane.add(btnX);
        btnX.setActionCommand("x");
        btnX.addActionListener(this);

        button_11 = new JButton("\u00F7");
        button_11.setBackground(new Color(255, 255, 224));
        button_11.setForeground(Color.DARK_GRAY);
        button_11.setBounds(193, 274, 41, 36);
        contentPane.add(button_11);
        button_11.setActionCommand("/");
        button_11.addActionListener(this);

        lblNewLabel = new JLabel("");
        lblNewLabel.setBackground(new Color(255, 255, 255));
        lblNewLabel.setBounds(10, 21, 214, 37);
        contentPane.add(lblNewLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String eventName = e.getActionCommand();
        if (eventName.equals("1")) {
            lblNewLabel.setText("1");
            v1 = 1;
        } else if (eventName.equals("2")) {
            lblNewLabel.setText("2");
            v1 = 2;
        } else if (eventName.equals("3")) {
            lblNewLabel.setText("3");
            v1 = 3;
        } else if (eventName.equals("4")) {
            lblNewLabel.setText("4");
            v1 = 4;
        } else if (eventName.equals("5")) {
            lblNewLabel.setText("5");
            v1 = 5;
        } else if (eventName.equals("6")) {
            lblNewLabel.setText("6");
            v1 = 6;
        } else if (eventName.equals("7")) {
            lblNewLabel.setText("7");
            v1 = 7;
        } else if (eventName.equals("8")) {
            lblNewLabel.setText("8");
            v1 = 8;
        } else if (eventName.equals("9")) {
            lblNewLabel.setText("9");
            v1 = 9;
        } else if (eventName.equals("0")) {
            lblNewLabel.setText("0");
            v1 = 0;
        } else if (eventName.equals("Clear")) {
            lblNewLabel.setText("");
        }
        while (!eventName.equals("+") || !eventName.equals("-")
                || !eventName.equals("x") || !eventName.equals("/")) {
            if (eventName.equals("1")) {
                v1 = (v1 * 10) + 1;
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("2")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("3")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("4")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("5")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("6")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("7")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("8")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("9")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("0")) {
                lblNewLabel.setText(Integer.toString(v1));
            } else if (eventName.equals("Clear")) {
                lblNewLabel.setText("");
            }

        }

    }// overall loop

}// end

每次我运行 GUI 时,按钮在单击后立即冻结。我希望你们能帮助我。我觉得问题出在我的第二种方法的 while 循环中,但我们将不胜感激。

【问题讨论】:

  • 有没有抛出异常?
  • 是的。我的意思是你一直在迭代和设置 Text。
  • 如果eventName是“1”,那么它总是NOT“+”NOT“-”NOT“/”和NOT“x”,所以它会无限循环

标签: java swing user-interface calculator


【解决方案1】:

是的,这个循环将永远存在。使用if 语句代替循环。 Swing 会自动为你循环(每次按下按钮时都会调用该方法)。

【讨论】:

  • 非常感谢@Anubian Noob。我将while更改为if并删除了第一组if else,它工作正常。
  • @user3576336 如果这解决了您的问题,请接受它以将其标记为对未来的观众有用。
【解决方案2】:

正如@Anubian 所说,循环将永远进行,因为每个循环都必须有一些退出条件,而您拥有的while 循环根本不会退出,因为单击按钮时eventname 始终相同。 每次按下button 时都会调用方法actionPerformed。 所以只需将while 替换为if

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 2022-12-03
    • 2022-11-22
    • 2018-01-05
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多