简体   繁体   English

线程中断和ActionListener Java

[英]Thread interruption and ActionListener Java

I have a function graphics() that creates my JFrame and two JRadioButtons and adds ActionListeners to them. 我有一个函数graphics()创建我的JFrame和两个JRadioButtons并向它们添加ActionListeners。 This graphics is called from main() and graphics itself calls game(). 这个图形从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;    
    }

After displaying "This is private information." 显示“这是私人信息”后。 in the text Area, I want the program execution to pause for 40 seconds, or until the user presses the JRadioButton, whichever is earlier. 在文本区域中,我希望程序执行暂停40秒,或者直到用户按下JRadioButton,以较早者为准。 So I added an ActionListener and called t.interrupt() inside it. 所以我添加了一个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...");

    }

However, even after choosing the JRadioButton which should trigger the interrupt, that does not happen and t.interrupted returns false. 但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false。

Any help would be appreciated. 任何帮助,将不胜感激。

Never, ever call Thread.sleep(...) on the Swing event thread as you will freeze the thread and effectively freeze your program. 永远不要在Swing事件线程上调用Thread.sleep(...) ,因为你将冻结线程并有效地冻结你的程序。 The solution is to consider use of a Swing Timer for the time-dependent portion of your requirement and using a SelectionListener for the JCheckBox or JRadioButton requirement. 解决方案是考虑使用Swing Timer作为需求的时间相关部分,并将SelectionListener用于JCheckBox或JRadioButton要求。

For example: 例如:

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();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM