SwingTimer更新方法停止Action Listener

时间:2016-06-10 15:14:48

标签: java swing jframe multiple-instances

package finalproje;

import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

class GUIMover {
    private ArrayList<JFrame> frameList = new ArrayList<JFrame>();
    private int count, max, matchNumber;
    private Random random = new Random();
    private Timer update;
    private boolean matchFound = false;
    long startTime = System.currentTimeMillis();

    public GUIMover(int max) {
        this.max = max;
        matchNumber = random.nextInt(max);
        update = new Timer(500, updateAction);
        update.setCoalesce(true);
    }

    public void startGUI() {
        JOptionPane.showMessageDialog(null, "Find window " + matchNumber);
        for (; count < max;) {
            final JFrame frame = new JFrame(count + "");
            ++count;
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// exits
                                                                    // windows
                                                                    // when
                                                                    // closed.
            frame.setVisible(true);
            frame.setBackground(new Color(random.nextFloat(), random
                    .nextFloat(), random.nextFloat()));
            frame.setBounds(random.nextInt(500), random.nextInt(500),
                    random.nextInt(500), random.nextInt(500));
            frame.addMouseListener(new MouseListener() {
                @Override
                public void mouseReleased(MouseEvent e) {

                }

                @Override
                public void mousePressed(MouseEvent e) {
                }

                @Override
                public void mouseExited(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {

                }

                @Override
                // check if you clicked your match
                public void mouseClicked(MouseEvent e)
                        throws NumberFormatException {
                    if (!matchFound
                            && Integer.parseInt(frame.getTitle()) == matchNumber) {
                        matchFound = true;

                        frameList.get(0).setTitle(
                                "You won after " + getPrintableGameTime()/*
                                                                         * conversion
                                                                         * from
                                                                         * milliseconds
                                                                         * to
                                                                         * seconds
                                                                         */
                                        + " seconds!");
                        for (int q = frameList.size() - 1; q > 0; q--) {
                            frameList.get(q).dispose();
                        }
                        frameList.get(0).setSize(500, 500);
                    }
                }
            });
            frameList.add(frame);
        }
        update.start();
    }
    boolean hasIntersections(JFrame f) {
        for (JFrame x : frameList)
            if (f != x && f.getBounds().intersects(x.getBounds())) {
                return true;
            }
        return false;
    }
    String getPrintableGameTime() {
        if (matchFound) {
            long endTime = System.currentTimeMillis();
            long totalTime = endTime - startTime;
            return String.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toSeconds(totalTime) / 60,
                    TimeUnit.MILLISECONDS.toSeconds(totalTime) % 60);
        } else
            return "You shouldn't see this.";
    }
    @SuppressWarnings("serial")
    /* serves as update method */
    Action updateAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (!matchFound) {
                for (JFrame i : frameList) {
                    i.setLocation(
                            (MouseInfo.getPointerInfo().getLocation().x + random
                                    .nextInt(1000)), (MouseInfo
                                    .getPointerInfo().getLocation().y + random
                                    .nextInt(1000)));
                    i.setLocation(random.nextInt(900), random.nextInt(800));
                }
                for (int j = 0; j < frameList.size(); j++) {
                    while (hasIntersections(frameList.get(j))) {
                        frameList.get(j).setLocation(
                                frameList.get(j).getX() + 10,
                                frameList.get(j).getY());
                        if (frameList.get(j).getX() > 1200) {
                            frameList.get(j).setLocation(0,
                                    frameList.get(j).getY() + 100);
                            frameList.get(j).setSize(
                                    frameList.get(j).getWidth() - 10,
                                    frameList.get(j).getHeight() - 10);

                        } else if (frameList.get(j).getY() > 800) {
                            frameList.get(j).setSize(
                                    frameList.get(j).getWidth() - 10,
                                    frameList.get(j).getHeight() - 10);
                            frameList.get(j).setLocation(0,
                                    frameList.get(j).getX() + 100);
                        }

                    }
                }

            }
        }
    };

}

是否可以分离鼠标侦听器侦听的线程?

当GUI正在移动时,任何输入都会被忽略,这非常令人沮丧,因为这包括单击关闭按钮(红色x),这将需要我SIGKILL进程。

先谢谢。

0 个答案:

没有答案