我有一个函数graphics()创建我的JFrame和两个JRadioButtons并向它们添加ActionListeners.这个图形从main()调用,图形本身调用game().
public void game() throws Exception
{
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setText("This is private information.");
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
try {
t.sleep(40000);
repaint();
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
Run Code Online (Sandbox Code Playgroud)
显示"这是私人信息"后.在文本区域中,我希望程序执行暂停40秒,或者直到用户按下JRadioButton,以较早者为准.所以我添加了一个ActionListener并在其中调用了t.interrupt().
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
t.interrupt();
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
//System.out.println(t.interrupted());
jTextArea1.setText("Please wait...");
}
Run Code Online (Sandbox Code Playgroud)
但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false.
任何帮助,将不胜感激.
我是Swing的新手,所以这似乎是一个非常天真的问题.
我有一个JFrame,它显示一个初始语句和两个radiobuttons.RadioButton1是Accept,RadioButton2是Reject.如果用户选择"接受",则程序继续.所以我为Accept创建了一个ActionListener,以便我的其余代码都在这个ActionListener中.但是,只要用户按下Accept,GUI就会冻结.按Reject只是出现在程序之外.
public void game() throws Exception
{
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setText("Please choose Accept or Reject");
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
repaint();
//more code
});
}
Run Code Online (Sandbox Code Playgroud)
在此之后,我连接到服务器,这工作正常,因为当我使用System.out.println()时,所有输出都很好,只是GUI被冻结.
当用户按下Accept时,有更好的方法可以继续吗?