0

我正在实现一个 javax.swing.timer 来调用 GUI 上的同步操作。但是,该操作需要一些时间才能完成,我想为该计时器调用提供一个中止选项。那可能吗?

谢谢!

import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import org.omg.CosNaming.IstringHelper;


public class vtm extends JPanel implements ActionListener{

    private JFrame frame;
    private String children[];
    private ConsoleApp hawk[];
    public static String data[][];
    public Timer timer;
    private int count1, count2;
    private int i;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    vtm window = new vtm();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public vtm() {
        timer = new Timer(25000, this);
        //timer.setInitialDelay(190);
        timer.start();
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frame = new JFrame("VTM");
        frame.setSize(1024, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon("config.png"));
        lblNewLabel_1.setBounds(970, 0, 51, 55);
        frame.getContentPane().add(lblNewLabel_1);
        lblNewLabel_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                config x = new config();
                x.main(null);
                timer.stop();
                frame.dispose();
            }
            });

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon("bg.png"));
        lblNewLabel.setBounds(0, 0, 1024, 720);
        frame.getContentPane().add(lblNewLabel);


        String dir = System.getProperty("user.dir");

        File df = new File(dir);

        FilenameFilter fi = new FilenameFilter(){
            public boolean accept(File df, String name){
                return name.startsWith("D-");
            }
        };

        children = df.list(fi);
        hawk=new ConsoleApp[children.length];

        EventQueue.invokeLater(new Runnable(){
            public void run(){

                int count1 = 0; count2 = 0;

                for(i=0;i<children.length;i++)
                {


                hawk[i]=new ConsoleApp(children[i]);

                Iterator<String> it = hawk[i].p.iterator();

                System.out.println("Hi:"+hawk[i].p.size());

                while(it.hasNext())
                {
                    hawk[i].call(it.next());

                    if(count1 == 3)
                    {
                        count2++;
                        count1=0;
                    }

                    System.out.println("Look: " + hawk[i].l.getText().length());
                    hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
                    hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
                    frame.getContentPane().add(hawk[i].c,0);
                    frame.getContentPane().add(hawk[i].l,0);

                    count1++;
                }
            }
            }
        });
    }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("lol");

        EventQueue.invokeLater(new Runnable(){
                public void run(){

                    int count1 = 0; count2 = 0;

                    for(i=0;i<children.length;i++)
                    {


                    hawk[i]=new ConsoleApp(children[i]);

                    Iterator<String> it = hawk[i].p.iterator();

                    System.out.println("Hi:"+hawk[i].p.size());

                    while(it.hasNext())
                    {
                        hawk[i].call(it.next());

                        if(count1 == 3)
                        {
                            count2++;
                            count1=0;
                        }

                        hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
                        hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
                        frame.getContentPane().add(hawk[i].c,0);
                        frame.getContentPane().add(hawk[i].l,0);

                        count1++;
                    }
                }
                }
            });


    }


}
4

3 回答 3

2

您的 actionPerformed 触发了一个 Runnable,我认为这需要很长时间并且您希望中止。在 Runnable 的 run 方法中,您有两个循环,for(i=0;i<children.length;i++)并且while(it.hasNext()). 如果向这些循环添加停止条件,则可以通过从外部控制该条件使 Runnable 提前完成。

例如,abort在 vtm 中添加一个字段,并将两个循环分别更改为for(i=0;i<children.length && !abort;i++)while(it.hasNext() && !abort)。您现在可以设置abort为 true,Runnable 将在下次尝试再次循环时停止。

于 2012-07-18T12:25:59.967 回答
2

使用 Swingworker 而不是 Runnable,这是实现取消机制的方式:http: //docs.oracle.com/javase/tutorial/uiswing/concurrency/cancel.html

另请参阅此问题:如何在 Java 中使用 SwingWorker?

于 2012-07-18T12:26:48.523 回答
0

我正在实现一个 javax.swing.timer 来调用 GUI 上的同步操作。但是,该操作需要一些时间才能完成,我想为该计时器调用提供一个中止选项。那可能吗?

如果您想向您的用户提供 UI 以取消Runnable您在事件调度线程 (EDT) 运行时发布的内容,那么不,这是不可能的。

当您的Runnable代码开始执行时,您的 UI 将无响应,因为 UI 线程(EDT)被您的Runnable. 因此,在完成之前,任何取消按钮或您想到的任何界面都不会对任何点击做出反应Runnable

You can cancel it before it starts running, but a better investment of your time might be to investigate whether you really have to perform heavy work on the EDT (for which normally the answer is no). It would be better to do all the heavy calculations/operations on a worker Thread, and update the UI during/afterwards. The SwingWorker class was designed for this, and many examples of its usage can be found online (in the javadoc, the official Swing tutorials and on this site)

于 2012-07-18T21:28:39.817 回答