gpt4 book ai didi

java - JButton 和 GUI 在单击后卡住

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:50 25 4
gpt4 key购买 nike

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

最佳答案

更改内容 Pane 后,您必须重新验证 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();//

}

当我测试它时它有效。我所说的作品是指没有卡住并将内容 Pane 设置为 answerPanel

关于java - JButton 和 GUI 在单击后卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com