Java Window脉冲推子工作一次然后中断

时间:2014-02-11 18:08:40

标签: java freeze jwindow

好的,我已经设置了一些代码来创建一个简单的小覆盖窗口,用作我正在处理的程序的警报消息。第一次运行时一切正常,但尝试再次运行它会冻结整个事情,迫使我通过调试器或任务管理器终止它。我知道我做错了什么,我只是不确定是什么,因为我对Java的经验有限。

以下是我用来设置窗口并将其放在任务栏上方右下角的代码:

private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public static JWindow alertWindow() {
    JWindow newWin = new JWindow();

    JPanel panel = new JPanel();

    BufferedImage img = null;
    try {
        img = ImageIO.read(Main.class.getResource("/images/test.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    JLabel imgLbl = new JLabel(new ImageIcon(img));

    panel.add(imgLbl);
    newWin.setContentPane(panel);
    newWin.pack();

    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(newWin.getGraphicsConfiguration());
    int taskBar = scnMax.bottom;
    int x = screenSize.width - newWin.getWidth();
    int y = screenSize.height - taskBar - newWin.getHeight();
    newWin.setLocation(x,y);
    newWin.setVisible(true);

    final PulseWindow pulseWin = new PulseWindow(newWin);
    pulseWin.getWindow().addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent click) {
            if(SwingUtilities.isRightMouseButton(click)) {
                pulseWin.stopPulsing();
                pulseWin.destroyPulse();
            } else {
                System.out.println(pulseWin.isPulsing());
                if(pulseWin.isPulsing()) {pulseWin.stopPulsing();}
                else {pulseWin.startPulse();}
            }
        }
        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}
    });
    pulseWin.startPulsing();

    return newWin;
}

以下是我设置的代码,以引起用户的注意:

import javax.swing.JWindow;

public class PulseWindow {

private boolean pulse = true;
private boolean doPulse = true;
private Float floor = 0.50f;
private JWindow win;

public PulseWindow(JWindow win) {
    this.win = win;
}

public void startPulsing() {
    pulse = true;
    boolean decreasing = true;
    double inc2 = 0.03;
    double current = win.getOpacity();

    while(pulse) {
        if(doPulse) {
            if(decreasing) {
                current = current - inc2;

                if((float) current <= floor) {
                    current = floor;
                    win.setOpacity((float) current);
                    decreasing = false;
                } else {
                    win.setOpacity((float) current);
                }
            } else {
                current = current + inc2;

                if((float) current >= 1.0f) {
                    current = 1.0;
                    win.setOpacity((float) current);
                    decreasing = true;
                } else {
                    win.setOpacity((float) current);
                }
            }
        } else {
            current = 1.0;
            win.setOpacity(1.0f);
            decreasing = true;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    win.setOpacity(1.0f);
}

public void destroyPulse() {
    pulse = false;
    win.dispose();
}

public boolean isPulsing() { return doPulse; }
public void setFloor(float floor) { this.floor = floor; }
public void stopPulsing() { doPulse = false; }
public void startPulse() { doPulse = true; }
public JWindow getWindow() { return win; }
}

无论如何,正如我所提到的,它在第一次使用时工作正常,但是只要您通过右键单击关闭窗口然后尝试稍后重新运行它(无论是通过调用startPulsing()方法还是通过再次调用alertWindow()使用新的JWindow完全重新初始化整个类,整个程序冻结。任何想法为什么会这样?

就像我说的那样,我仍然是Java的新手,所以如果你看到其他任何我做错了/效率低下的话,请随意指出它以便我能正确地做到这一点。 / p>

修改

我现在开始认为问题出在JWindows上。我为显示警报的不同方法设置了一些其他代码,虽然这次没有冻结,但它也无法正常工作。

public class AlertWindow extends JWindow {

private static Border compound = BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder());
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

public AlertWindow() {      
    JPanel panel = new JPanel();
        panel.setBorder(compound);
        panel.setBackground(Color.RED);

    JLabel imgLbl = new JLabel("Enter Alert Msg Here!");
        imgLbl.setFont(new Font(null,Font.BOLD,16));

    panel.add(imgLbl);
    setContentPane(panel);
    pack();


    this.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent click) {
            if(SwingUtilities.isLeftMouseButton(click)) {
                scrollOff();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                scrollOn();
            }
        }
        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}
    });
    scrollOn();
}

public void scrollOn() {
    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBar = scnMax.bottom;
    int x = screenSize.width - getWidth();
    int yEnd = screenSize.height - taskBar - getHeight();
    int yStart = screenSize.height;
    setLocation(x,yStart);
    setVisible(true);
    int current = yStart;
    while(current > yEnd) {
        current-=2;
        System.out.println(current);
        setLocation(x,current);
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public void scrollOff() {
    int x = screenSize.width - getWidth();
    int yEnd = screenSize.height;
    int yStart = this.getBounds().y;
    setLocation(x,yStart);
    int current = yStart;
    while(current < yEnd) {
        current+=2;
        setLocation(x,current);
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    setVisible(false);
}
}

就像脉冲窗口问题一样,它第一次按预期工作,然后在后续使用中中断。在这种情况下,唯一中断的是scrollOn()命令。它在不可见时滚动,然后在到达目的地后变为可见。位置的控制台输出清楚地显示它正在移动,但在它停止移动之前你无法看到它。

1 个答案:

答案 0 :(得分:0)

编辑2:

回到感觉愚蠢......我发现了这个问题(实际上是在前一段时间找到它但忘了更新这个......)。最后问题是我只使用了runnable而没有将它放在new Thread()对象中。出于某种原因,我认为runnable对象创建了自己的新线程,但是一旦我弄清楚我的错误,这是一个简单的修复。显然,我还有很长的路要走Java ...


修改

好的,现在我很生气......显然,如果你试图从某种动作监听器中运行它,它仍然会中断。我最新版本的PulseAlert类(如下所示)调用PulseWindow类,原始答案如下所示:

public class PulseAlert {

private static Border compound = BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder());
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

public void runAlert() throws InterruptedException {
    final PulseWindow pulseWin = new PulseWindow(alertWindow());
    pulseWin.getWindow().addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent click) {
            if(SwingUtilities.isRightMouseButton(click)) {
                pulseWin.stopPulsing();
                pulseWin.destroyPulse();
            } else if(SwingUtilities.isLeftMouseButton(click) && pulseWin.isPulsing()) {
                pulseWin.stopPulsing();
            } else if(SwingUtilities.isLeftMouseButton(click) && !pulseWin.isPulsing()) {
                pulseWin.startPulsing();
            }
        }
        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}
    });
    try {
        pulseWin.startPulse();
    } catch (Exception e) {
        e.printStackTrace();
    }
    while(pulseWin.pulserActive()) {
        Thread.sleep(100);
    }
    System.out.println("done with second SW");
}

public static JWindow alertWindow() {
    System.out.println("Start");
    JWindow newWin = new JWindow();

    JPanel panel = new JPanel();
        panel.setBorder(compound);
        panel.setBackground(Color.RED);

    JLabel imgLbl = new JLabel("Enter Alert Msg Here!");
        imgLbl.setFont(new Font(null,Font.BOLD,16));

    panel.add(imgLbl);
    newWin.setContentPane(panel);
    newWin.pack();

    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(newWin.getGraphicsConfiguration());
    int taskBar = scnMax.bottom;
    int x = screenSize.width - newWin.getWidth();
    int y = screenSize.height - taskBar - newWin.getHeight();
    newWin.setLocation(x,y);
    newWin.setVisible(true);

    return newWin;
}
}

以下是我如何调用警报窗口 - 如果我愿意,可以反复调用,只要它在动作监听器之外。

PulseAlert alertPulse = new PulseAlert();
alertPulse.runAlert();

上面的代码可以完美地运行,直到放入某种动作监听器,例如:

trayIcon.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            alertPulse.runAlert();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
});

从动作监听器调用runAlert()方法后,整个事情就像以前一样冻结。直到那时运行完美。是什么原因引起了这个?这是Java中的错误还是我做错了什么?


原始答案:

好的,我现在觉得很傻。我需要做的就是解决这个问题,将startPulsing()内容放入一个新的runnable中,一切正常,并且我需要多次。

public void startPulsing() throws Exception {
    new Runnable() {
        @Override
        public void run() {
            pulse = true;
            win.setVisible(true);
            boolean decreasing = true;
            double inc = 0.05;
            double current = win.getOpacity();

            while(pulse) {
                if(doPulse) {
                    if(decreasing) {
                        current = current - inc;

                        if((float) current <= floor) {
                            current = floor;
                            win.setOpacity((float) current);
                            decreasing = false;
                        } else {
                            win.setOpacity((float) current);
                        }
                    } else {
                        current = current + inc;

                        if((float) current >= 1.0f) {
                            current = 1.0;
                            win.setOpacity((float) current);
                            decreasing = true;
                        } else {
                            win.setOpacity((float) current);
                        }
                    }
                } else {
                    current = 1.0;
                    win.setOpacity(1.0f);
                    decreasing = true;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            win.setOpacity(1.0f);
        }
    }.run();
}