我遇到了Swing中的无限循环问题。完成一些研究并遇到SwingWorker线程,但不确定如何实现它们。我把一个显示问题的简单程序组合在一起。一个按钮启动无限循环,我希望另一个按钮停止它,但当然由于Swing单线程问题,另一个按钮已冻结。下面的代码,并帮助赞赏: -
public class Model
{
private int counter;
private boolean go = true;
public void go()
{
counter = 0;
while(go)
{
counter++;
System.out.println(counter);
}
}
public int getCounter()
{
return counter;
}
public void setGo(boolean value)
{
this.go = value;
}
}
public class View extends JFrame
{
private JPanel topPanel, bottomPanel;
private JTextArea messageArea;
private JButton startButton, cancelButton;
private JLabel messageLabel;
private JScrollPane scrollPane;
public View()
{
setSize(250, 220);
setTitle("View");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topPanel = new JPanel();
bottomPanel = new JPanel();
messageArea = new JTextArea(8, 20);
messageArea.setEditable(false);
scrollPane = new JScrollPane(messageArea);
messageLabel = new JLabel("Message Area");
topPanel.setLayout(new BorderLayout());
topPanel.add(messageLabel, "North");
topPanel.add(scrollPane, "South");
startButton = new JButton("START");
cancelButton = new JButton("CANCEL");
bottomPanel.setLayout(new GridLayout(1, 2));
bottomPanel.add(startButton);
bottomPanel.add(cancelButton);
Container cp = getContentPane();
cp.add(topPanel, BorderLayout.NORTH);
cp.add(bottomPanel, BorderLayout.SOUTH);
}
public JButton getStartButton()
{
return startButton;
}
public JButton getCancelButton()
{
return cancelButton;
}
public void setMessageArea(String message)
{
messageArea.append(message + "\n");
}
}
public class Controller implements ActionListener
{
private Model theModel;
private View theView;
public Controller(Model model, View view)
{
this.theModel = model;
this.theView = view;
view.getStartButton().addActionListener(this);
view.getCancelButton().addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object buttonClicked = ae.getSource();
if(buttonClicked.equals(theView.getStartButton()))
{
theModel.go();
}
else if(buttonClicked.equals(theView.getCancelButton()))
{
theModel.setGo(false);
}
}
}
public class Main
{
public static void main(String[] args)
{
Model model = new Model();
View view = new View();
Controller controller = new Controller(model, view);
view.setVisible(true);
}
}
答案 0 :(得分:3)
您正在屏蔽Event Dispatch Thread
(EDT)。该线程负责处理绘画和其他UI相关请求。一旦EDT被阻止,UI将被冻结,因为它无法处理任何事件。有关更多详细信息,请参阅The Event Dispatch Thread教程。
考虑使用计时器(How to Use Swing Timers),SwingWorker
或辅助后台线程。后台线程可以使用SwingUtilities.invokeLater()
与EDT通信。此机制已在SwingWorker
中实现,因此可能更容易使用它。这取决于所需的功能。
答案 1 :(得分:3)
在事件处理中使用javax.swing.Timer
和[{1}},使用go()
工作start()
一次(有一些可选的延迟)。
答案 2 :(得分:3)
您无需实施任何计时器即可轻松完成,只需在 actionPerformed 方法中添加两行:
public void actionPerformed(ActionEvent ae)
{
Object buttonClicked = ae.getSource();
if(buttonClicked.equals(theView.getStartButton()))
{
theModel.setGo(true); //make it continue if it's just stopped
Thread t = new Thread(new Runnable() { public void run() {theModel.go();}}); //This separate thread will start the new go...
t.start(); //...when you start the thread! go!
}
else if(buttonClicked.equals(theView.getCancelButton()))
{
theModel.setGo(false);
}
}
当 Model.go()在一个单独的线程中运行时,事件调度线程可以自由地执行其操作,例如再次绘制按钮,而不是按下按钮。
有一个问题! 但是,因为运行 Model.go()的线程会疯狂地运行 !< / em> ,它几乎被称为系统可以每秒多次。
如果您计划实施某些动画等,则需要:
或
如果您使用线程示例:
public void go()
{
counter = 0;
while(go)
{
counter++;
System.out.println(counter);
try {
Thread.sleep(1500); //Sleep for 1.5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
正如您所看到的,我将 Thread.sleep(1500)添加为1500毫秒(1.5秒)。由于某些原因,Thread.sleep可能会被中断,因此您必须捕获 InterruptedException 。
在这种特殊情况下,没有必要更深入地处理 InterruptedException ,但如果您对此感到好奇,可以阅读nice article。
答案 3 :(得分:0)
我决定使用SwingWorker线程,下面是更新的Controller类。它做了我需要做的事情,但我的问题是,它是正确的方式,它是干净的代码吗?另外,我已经尝试将model.go()方法的输出按照注释掉的行添加到视图的textarea中,但没有成功,任何人都知道如何?
public class Controller implements ActionListener
{
private Model theModel;
private View theView;
private SwingWorker<Void, Void> worker;
public Controller(Model model, View view)
{
this.theModel = model;
this.theView = view;
view.getStartButton().addActionListener(this);
view.getCancelButton().addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object buttonClicked = ae.getSource();
if(buttonClicked.equals(theView.getStartButton()))
{
theModel.setGo(true);
worker = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground()
{
// theView.setMessageArea(theModel.getCounterToString());
return theModel.go();
}
@Override
protected void done()
{
// theView.setMessageArea(theModel.getCounterToString());
}
};
worker.execute();
}
else if(buttonClicked.equals(theView.getCancelButton()))
{
theModel.setGo(false);
}
}
}
public class Model
{
public Void go()
{
counter = 0;
while(go)
{
counter++;
System.out.println(counter);
}
return null;
}