单击后,JButton和GUI冻结

时间:2015-05-23 23:35:31

标签: java swing jpanel jbutton

当Eclipse编译此代码时,一切正常,除非用户单击“添加”按钮后GUI冻结。显示答案并显示工作。任何人都可以对这个问题有所了解,并且可能会给我一些关于布局的建议吗?

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);
    }
}

1 个答案:

答案 0 :(得分:0)

更改内容窗格后,您必须重新验证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