单击后 JButton 和 GUI 冻结

JButton and GUI freezes after click

当 Eclipse 编译此代码时,除了 GUI 在用户单击 "add" 按钮后冻结外,一切正常。显示答案并显示工作。任何人都可以阐明这个问题,也可以给我一些布局方面的建议吗?

import Aritmathic.MathEquation;

public class GUI extends JFrame implements ActionListener{
    private JTextField field1;
    private JTextField field2;
    private JButton add, subtract, multiply, divide;
    private JLabel lanswer, label1, label2;
    private String input1, input2, sanswer;
    private int answer = 0;

    JPanel contentPanel, answerPanel;

    public GUI(){
        super("Calculator");

        field1 = new JTextField(null, 15);
        field2 = new JTextField(null, 15);

        add = new JButton("add");
        subtract = new JButton("subtract");
        multiply = new JButton("multiply");
        divide = new JButton("divide");

        lanswer = new JLabel("", SwingConstants.CENTER);
        label1 = new JLabel("Value 1:");
        label2 = new JLabel("Value 2:");

        add.addActionListener(this);

        Dimension opSize = new Dimension(110, 20);
        Dimension inSize = new Dimension(200, 20);

        lanswer.setPreferredSize(new Dimension(200,255));

        field1.setPreferredSize(inSize);
        field2.setPreferredSize(inSize);
        add.setPreferredSize(opSize);
        subtract.setPreferredSize(opSize);
        multiply.setPreferredSize(opSize);
        divide.setPreferredSize(opSize);

        contentPanel = new JPanel();
        contentPanel.setBackground(Color.PINK);
        contentPanel.setLayout(new FlowLayout());

        answerPanel = new JPanel();
        answerPanel.setPreferredSize(new Dimension(230, 260));
        answerPanel.setBackground(Color.WHITE);
        answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.Y_AXIS));

        contentPanel.add(answerPanel);
        contentPanel.add(label1); contentPanel.add(field1); 
        contentPanel.add(label2); contentPanel.add(field2);
        contentPanel.add(add); contentPanel.add(subtract); contentPanel.add(multiply); contentPanel.add(divide);


        this.setContentPane(contentPanel);
    }

    public void actionPerformed(ActionEvent e) {
        JButton src = (JButton)e.getSource();

        if(src.equals(add)){
            add();
        } 
    }

    private void add(){
        input1 = field1.getText();
        input2 = field2.getText();

        MathEquation problem = new MathEquation(input1, input2);

        Dimension lineSize = new Dimension(10, 10);

        JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE); 
        JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE); 
        JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE); 
        JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE); 
        JPanel line5 = new JPanel(); line4.setBackground(Color.WHITE); 

        JLabel[] sumLabels = problem.getSumLabels();
        JLabel[] addend1Labels = problem.getAddend1Labels();
        JLabel[] addend2Labels = problem.getAddend2Labels();
        JLabel[] carriedLabels = problem.getCarriedLabels();

        for(int i = 0; i < carriedLabels.length; i++){
            line1.add(carriedLabels[i]);
        }

        for(int i = 0; i < addend1Labels.length; i++){
            line2.add(addend1Labels[i]);
        }

        for(int i = 0; i < addend2Labels.length; i++){
            line3.add(addend2Labels[i]);
        }

        String answerLine = "__";

        for(int i = 0; i < sumLabels.length; i++){
            answerLine += "__";
        }

        line4.add(new JLabel(answerLine));

        for(int i = 0; i < sumLabels.length; i++){
            line5.add(sumLabels[i]);
        }

        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));
        answerPanel.add(line1);
        answerPanel.add(line1);
        answerPanel.add(line2);
        answerPanel.add(line3);
        answerPanel.add(line4);
        answerPanel.add(line5);
        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));
        answerPanel.add(new JLabel(" "));

        this.setContentPane(answerPanel);
    }
}

更改内容窗格后必须重新验证 JFrame。

之后
this.setContentPane(answerPanel);

添加

revalidate();

Java docs about revalidate()

您的 add() 方法现在应该如下所示:

private void add(){
    input1 = field1.getText();
    input2 = field2.getText();

    MathEquation problem = new MathEquation(input1, input2);

    Dimension lineSize = new Dimension(10, 10);

    JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE); 
    JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE); 
    JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE); 
    JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE); 
    JPanel line5 = new JPanel(); line4.setBackground(Color.WHITE); 

    JLabel[] sumLabels = problem.getSumLabels();
    JLabel[] addend1Labels = problem.getAddend1Labels();
    JLabel[] addend2Labels = problem.getAddend2Labels();
    JLabel[] carriedLabels = problem.getCarriedLabels();

    for(int i = 0; i < carriedLabels.length; i++){
        line1.add(carriedLabels[i]);
    }

    for(int i = 0; i < addend1Labels.length; i++){
        line2.add(addend1Labels[i]);
    }

    for(int i = 0; i < addend2Labels.length; i++){
        line3.add(addend2Labels[i]);
    }

    String answerLine = "__";

    for(int i = 0; i < sumLabels.length; i++){
        answerLine += "__";
    }

    line4.add(new JLabel(answerLine));

    for(int i = 0; i < sumLabels.length; i++){
        line5.add(sumLabels[i]);
    }

    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(line1);
    answerPanel.add(line1);
    answerPanel.add(line2);
    answerPanel.add(line3);
    answerPanel.add(line4);
    answerPanel.add(line5);
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));
    answerPanel.add(new JLabel(" "));

    this.setContentPane(answerPanel);
    this.revalidate();//

    }

当我测试它时它有效。通过作品我的意思是没有冻结并将内容窗格设置为 answerPanel.