我正在介绍Java,所以我只知道Java的基础知识。我已经找到了答案,但每当我找到有同样问题的人时,他们就会找到我不理解的代码。如果有人可以用尽可能简单的术语向我解释,那将非常感激。每当我进入我的do while循环(在我的代码底部)时,我的问题是我的Jframe冻结。通过研究我发现我应该放一个SwingWorker,但我不知道如何或在哪里。继承我的代码
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
{ //按下按钮时更新计数器和标签。 public void actionPerformed(ActionEvent事件) { total =“”;
Balance = 100;
if(event.getSource() == num0)
Input += "0";
if(event.getSource() == num1)
Input += "1";
if(event.getSource() == num2)
Input += "2";
if(event.getSource() == num3)
Input += "3";
if(event.getSource() == num4)
Input += "4";
if(event.getSource() == num5)
Input += "5";
if(event.getSource() == num6)
Input += "6";
if(event.getSource() == num7)
Input += "7";
if(event.getSource() == num8)
Input += "8";
if(event.getSource() == num9)
Input += "9";
InputDisplay.setText(" " + Input);
if(Input.equals("7777") && event.getSource() == enter)
do
{
Input ="";
TextLabel.setText("Your balance is " + Balance);
}
while ( event.getSource() != quit);
TextLabel.setText("Goodbye.");
}
答案 0 :(得分:1)
您正在使用while ( event.getSource() != quit);
阻止事件调度线程,这不是事件驱动框架的工作方式,事件发生,您回复,这就是它。
ETD负责处理和调度事件队列中的事件,ETD已通知注册到您的按钮的ActionListener
,但在actionPerformed
方法存在之前,它无法继续处理事件队列,它使你的程序看起来有"挂起",因为它有。
删除while
循环
基本上,您不需要while
循环,但在调用if
时需要额外的quit
语句来检查actionPerformed
按钮
答案 1 :(得分:0)
你的框架冻结的原因很简单:你阻止事件调度线程(某些点上的所有挥动事件都通过这个线程),所以你阻止程序处理任何其他事件,因此框架冻结。由于while循环是一个endlessloop(你只输入while循环,如果满足循环的条件,并且event.getSource()
的结果不会改变),你的帧会冻结。
对于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();
}
会创建并执行SwingWorker
,但这对于像您这样简单快速完成的任务来说并不是必需的。只需删除endlessloop。