首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java窗口脉冲填充器工作一次,然后中断。

Java窗口脉冲填充器工作一次,然后中断。
EN

Stack Overflow用户
提问于 2014-02-11 18:08:40
回答 1查看 130关注 0票数 0

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

下面是设置窗口并将其放置在任务栏右下角的代码:

代码语言:javascript
运行
复制
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;
}

下面是我为吸引用户注意而设置的代码:

代码语言:javascript
运行
复制
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()完全重新初始化整个类,通过调用alertWindow()),整个程序就会冻结。知道为什么会这样吗?

就像我说的,我仍然是Java的新手,所以如果你看到我做错了什么/效率很低的话,你可以自由地指出出来,这样我就可以正确地做了。

编辑:

我开始认为现在的问题是JWindows。我为另一种显示警报的方法设置了其他代码,虽然这次没有冻结,但它也没有按预期工作。

代码语言:javascript
运行
复制
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()命令。它在不可见的情况下继续滚动,然后在到达目的地后变得可见。这个位置的控制台输出清楚地显示它在移动,但是直到它停止移动,您才能看到它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-12 21:15:24

编辑2:

然后又觉得自己很蠢..。我发现了这个问题(实际上是在一段时间前发现的,但忘了更新这个.)。最后的问题是,我只使用runnable,而不把它放在new Thread()对象中。出于某种原因,我认为可运行的对象创建了自己的新线程,但一旦我发现了自己的错误,这是一个简单的修复。显然我在学习Java方面还有很长的路要走.

编辑:

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

代码语言:javascript
运行
复制
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;
}
}

下面是我如何调用警报窗口-重复,如果我愿意,只要它在行动侦听器之外。

代码语言:javascript
运行
复制
PulseAlert alertPulse = new PulseAlert();
alertPulse.runAlert();

上述代码在放置到某种类型的动作侦听器(如:

代码语言:javascript
运行
复制
trayIcon.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            alertPulse.runAlert();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
});

一旦从一个动作侦听器调用了runAlert()方法,整个事情就会像以前一样冻结。在那之前一切都很顺利。有什么原因吗?这是Java中的一个bug,还是我做错了什么?

原始答案:

好吧,我觉得很蠢,现在。为了解决这个问题,我所要做的就是将startPulsing()内容放入一个新的可运行程序中,所有这些都能工作,而且我需要它的次数也一样多。

代码语言:javascript
运行
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21709698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档