单击后,JButton和GUI冻结(JButton and GUI freezes after click)

编程入门 行业动态 更新时间:2024-10-15 10:21:45
单击后,JButton和GUI冻结(JButton and GUI freezes after click)

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

When Eclipse compiles this code everything works fine except the GUI freezes after the user clicks the "add" button. The answer is displayed and the work is shown. Can anyone shine some light on this problem and maybe give me some advice for the layout as well?

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

关于revalidate()的Java文档

您的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 。

You have to revalidate your JFrame after you change the content pane.

After

this.setContentPane(answerPanel);

add

revalidate();

Java docs about revalidate()

Your add() method should now look like:

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

When I tested this it works. By works I mean did not freeze and set the content pane to the answerPanel.

更多推荐

本文发布于:2023-08-03 20:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   JButton   GUI   freezes   click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!