0

好的,我已经设置了一些代码来创建一个简单的小覆盖窗口,用作我正在处理的程序的警报消息。第一次运行一切正常,但试图再次运行它,它冻结了整个事情,迫使我通过调试器或任务管理器终止它。我知道我做错了什么,但由于我对 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()方法还是通过完全重新初始化整个类) new JWindowalertWindow()再次调用),整个程序冻结。任何想法为什么会这样?

就像我说的,我还是 Java 的新手,所以如果你发现我做错了/效率低下的其他任何事情,请随时指出,以便我能正确地做到这一点。

编辑:

我现在开始认为问题出在 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() 命令。它在不可见时滚动,然后在到达目的地后变为可见。该位置的控制台输出清楚地显示它正在移动,但直到它停止移动才能看到它。

4

1 回答 1

0

编辑2:

回到愚蠢的状态......我发现了这个问题(实际上是前一段时间发现的,但忘了更新这个......)。问题最终是我只使用了可运行文件,而不是把它放在一个new Thread()对象内。出于某种原因,我认为可运行对象创建了它们自己的新线程,但是一旦我发现我的错误,这很容易解决。显然我在学习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()内容放入一个新的可运行文件中,并且一切正常,并且可以根据需要多次运行。

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();
}
于 2014-02-12T21:15:24.033 回答