关闭其中包含WHILE循环的JFrame

时间:2013-07-14 12:59:00

标签: java swing while-loop window jframe

我是新来的,我有一个问题 几个月前,我制作了一个简单的Color Eyedropper,可以从屏幕上的任何地方挑选颜色 现在我正在制作一个Color Utility应用程序,它具有许多功能,如平均颜色生成和混合颜色。无论如何,我认为添加这个吸管会很酷,所以我的JFrame中有一个叫做

的JButton
DigitalEyedropper.init();

用我的吸管创建另一个JFrame 它打开并运行,但我似乎无法让它关闭。默认关闭按钮不起作用。它似乎没有监听任何JComponents。我试图添加一个JButton来关闭窗口,但它失败了。

有人可以帮助我吗?

public static JFrame window;
private static boolean cancel;

public void startProcess(){
    Thread t = new Thread(this);
    t.start();
}

public DigitalEyedropper(){
    window = new JFrame();
    window.setTitle("Color Utilty");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(640,360);
    window.setVisible(true);
    window.setLocationRelativeTo(null);
}

public static void init() throws InterruptedException, AWTException{
    SwingUtilities.invokeLater(new DigitalEyedropper());
}
@Override
public void run() {
    do{
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        int x = (int) b.getX();
        int y = (int) b.getY();
        try {
            Robot picker = new Robot();
            Color color = picker.getPixelColor(x, y);
            window.setBackground(color);
        }
        catch (AWTException e) {
        }
        if(cancel){
            System.out.println("breaking...");
            //How to close the JFrame?
        } while(/*What to do?*/)
    }
}   

0 个答案:

没有答案