线程中断和ActionListener Java

时间:2012-10-25 03:20:36

标签: java swing concurrency jtextarea event-dispatch-thread

我有一个函数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;    
    }

显示“这是私人信息”。在文本区域中,我希望程序执行暂停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...");

    }

但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:8)

永远不要在Swing事件线程上调用Thread.sleep(...),因为你将冻结线程并有效地冻结你的程序。解决方案是考虑使用Swing Timer作为需求的时间相关部分,并使用SelectionListener来满足JCheckBox或JRadioButton要求。

例如:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class PausingExecution extends JPanel {
   private static final String SELECTED_TEXT = "Snafus are Better!!!";
   private static final String UNSELECTED_TEXT = "Fubars Rule!!";
   private static final String TIMES_UP = "Time's Up!!!!";
   private static final int TIMER_DELAY = 10 * 1000;

   private JTextField messageField = new JTextField(UNSELECTED_TEXT, 10);
   private JCheckBox checkBox = new JCheckBox("Click Me");

   public PausingExecution() {
      add(messageField);
      add(checkBox);

      checkBox.addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent iEvt) {
            if (iEvt.getStateChange() == ItemEvent.SELECTED) {
               messageField.setText(SELECTED_TEXT);
            } else {
               messageField.setText(UNSELECTED_TEXT);
            }
         }
      });

      Timer mySwingTimer = new Timer(TIMER_DELAY, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            messageField.setText(TIMES_UP);
            checkBox.setEnabled(false);
         }
      });

      mySwingTimer.setRepeats(false);
      mySwingTimer.start();
   }

   private static void createAndShowGui() {
      PausingExecution mainPanel = new PausingExecution();

      JFrame frame = new JFrame("PausingExecution");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}