Java 当我按下按钮时,Jframe冻结
我正在学习Java入门,所以我只知道Java的基本知识。我已经搜索了答案,但每当我发现有人有同样的问题,他们的代码,我不明白。如果有人能尽可能简单地向我解释,我将不胜感激。我的问题是,每当我输入我的do while循环(在代码的底部)时,我的Jframe就会冻结。通过研究,我发现我应该找一个荡秋千的,但我不知道怎么找,也不知道在哪里找。这是我的密码Java 当我按下按钮时,Jframe冻结,java,swing,Java,Swing,我正在学习Java入门,所以我只知道Java的基本知识。我已经搜索了答案,但每当我发现有人有同样的问题,他们的代码,我不明白。如果有人能尽可能简单地向我解释,我将不胜感激。我的问题是,每当我输入我的do while循环(在代码的底部)时,我的Jframe就会冻结。通过研究,我发现我应该找一个荡秋千的,但我不知道怎么找,也不知道在哪里找。这是我的密码 import java.awt.*; import java.awt.event.*; import javax.swing.*; import j
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ATMSwingPanel extends JPanel
{
public int count, Balance;
public String total, Input = "";
public JButton withdraw1,withdraw2,withdraw3,deposit,quit,num0,
num1,num2,num3,num4,num5,num6,num7,num8,num9,clear,enter;
public JLabel label, pinLabel, TextLabel;
public JTextField InputDisplay;
public ATMSwingPanel()
enter code here
{
setLayout(new GridLayout(5, 6));;
TextLabel = new JLabel("Please enter your pin");
add(TextLabel);
InputDisplay = new JTextField();
InputDisplay.addActionListener(new ButtonListener());
withdraw1 = new JButton("Withdraw $20");
withdraw1.addActionListener(new ButtonListener());
withdraw2 = new JButton("Withdraw $50");
withdraw2.addActionListener(new ButtonListener());
withdraw3 = new JButton("Withdraw $100");
withdraw3.addActionListener(new ButtonListener());
deposit = new JButton("Deposit");
deposit.addActionListener(new ButtonListener());
quit = new JButton("Quit");
quit.addActionListener(new ButtonListener());
num0 = new JButton("0");
num0.addActionListener(new ButtonListener());
num1 = new JButton("1");
num1.addActionListener(new ButtonListener());
num2 = new JButton("2");
num2.addActionListener(new ButtonListener());
num3 = new JButton("3");
num3.addActionListener(new ButtonListener());
num4 = new JButton("4");
num4.addActionListener(new ButtonListener());
num5 = new JButton("5");
num5.addActionListener(new ButtonListener());
num6 = new JButton("6");
num6.addActionListener(new ButtonListener());
num7 = new JButton("7");
num7.addActionListener(new ButtonListener());
num8 = new JButton("8");
num8.addActionListener(new ButtonListener());
num9 = new JButton("9");
num9.addActionListener(new ButtonListener());
clear = new JButton("Clear");
clear.addActionListener(new ButtonListener());
enter = new JButton("Enter");
enter.addActionListener(new ButtonListener());
// Allows the labels and JButtons to appear when compiled
add(TextLabel);
add(InputDisplay);
add(withdraw1);
add(num1);
add(num2);
add(num3);
add(withdraw2);
add(num4);
add(num5);
add(num6);
add(withdraw3);
add(num7);
add(num8);
add(num9);
add(deposit);
add(num0);
add(clear);
add(enter);
add(quit);
// Background and window dimension sizes
setBackground(Color.gray);
setPreferredSize(new Dimension(720, 300));
}
//表示按钮按下(操作)事件的侦听器。
公共类ButtonListener实现ActionListener
{
//按下按钮时更新计数器和标签。
已执行的公共无效操作(操作事件)
{
总数=”
当(Event.getSource()!=quit);时,您正在用
阻塞事件调度线程,这不是事件驱动框架的工作方式,事件发生,您响应,就是这样
ETD负责处理和调度事件队列中的事件,ETD会通知注册到按钮的ActionListener
,但在actionPerformed
方法存在之前,ETD无法继续处理事件队列,这会使您的程序看起来“挂起”,因为它已挂起
移除而循环
基本上,当调用actionPerformed
时,您不需要while
循环,而是需要一个额外的if
语句来检查quit
按钮。帧冻结的原因很简单:您阻塞了事件调度线程(某个点的所有swing事件都通过该线程),因此您阻止程序处理任何其他事件,因此帧冻结。由于while循环是endlessloop(如果满足循环条件,并且event.getSource()
的结果不会更改,则仅输入while循环),因此帧冻结。
对于SwingWorker
:
public void actionPerformed(final ActionEvent e){
new SwingWorker<String , Object>(){
public String doInBackground(){
//create String for the label
return labelText;
}
public void done(){
TextLabel.setText(get());
}
}.execute();
}
public void action已执行(最终action事件e){
新SwingWorker(){
公共字符串doInBackground(){
//为标签创建字符串
返回标签文本;
}
公众假期结束(){
setText(get());
}
}.execute();
}
将创建并执行一个SwingWorker
,尽管这对于像您这样简单且快速完成的任务来说不是必需的。只需删除EndlessLop即可
public void actionPerformed(final ActionEvent e){
new SwingWorker<String , Object>(){
public String doInBackground(){
//create String for the label
return labelText;
}
public void done(){
TextLabel.setText(get());
}
}.execute();
}