Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组件侦听器正在被禁用_Java_Swing_Mouselistener_Jwindow_Componentlistener - Fatal编程技术网

Java 组件侦听器正在被禁用

Java 组件侦听器正在被禁用,java,swing,mouselistener,jwindow,componentlistener,Java,Swing,Mouselistener,Jwindow,Componentlistener,我遇到了一个不寻常的问题,每当mouseListener/适配器检测到事件时,componentListener/适配器就会被禁用。在下面的代码中,componentMoved()方法重写工作正常,直到在MouseAdapter()中触发mouseClicked()方法重写。有没有办法解决这个问题 public AlertScroller(String msg,Color col) { addComponentListener(new ComponentAdapter() {

我遇到了一个不寻常的问题,每当mouseListener/适配器检测到事件时,componentListener/适配器就会被禁用。在下面的代码中,
componentMoved()
方法重写工作正常,直到在
MouseAdapter()中触发
mouseClicked()
方法重写。有没有办法解决这个问题

public AlertScroller(String msg,Color col) {
    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentMoved(ComponentEvent e) {
            setShape(new Rectangle2D.Double(0, 0, getWidth(), newHeight));
            if(!isVisible())
                setVisible(true);
        }
    });

    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent click) {
            if(SwingUtilities.isLeftMouseButton(click)) {
                autoClear = false;
                scrollOff();
            }
        }
    });
    setAlwaysOnTop(true);
    JPanel panel = new JPanel();
        panel.setBorder(compound);
        panel.setBackground(col);

    JLabel imgLbl = new JLabel(msg);
        imgLbl.setFont(new Font("Arial",Font.BOLD,30));

    panel.add(imgLbl);
    setContentPane(panel);
    pack();
}
此方法所在的类扩展了
JWindow

编辑:添加了
scrollOff()
方法的代码

public void scrollOff() {
    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBar = scnMax.bottom;
    int x = screenSize.width - getWidth();
    int yEnd = screenSize.height - taskBar;
    int yStart = this.getBounds().y;
    setLocation(x,yStart);
    int current = yStart;
    newHeight = this.getBounds().height;
    while(current < yEnd) {
        current+=2;
        newHeight = yEnd - current;
        setLocation(x,current);
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    dispose();
    Main.alertControl.setAlertActive(false);
}
我还使用
else if
更新了
AlertScroller()
构造函数方法,以在完成时正确隐藏窗口:

    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentMoved(ComponentEvent e) {
            setShape(new Rectangle2D.Double(0, 0, getWidth(), newHeight));
            if(!isVisible())
                setVisible(true);
            else if(getBounds().y == screenSize.height - taskBar)
                setVisible(false);
        }
    });

setVisible(false)
放置在其他任何位置都会导致窗口再次可见。

while循环
是危险的,
线程。sleep
是危险的,dispose只是简单的吓人

您违反了Swing的单线程规则并阻塞了事件调度线程

有关更多详细信息,请参阅

dispose
可能正在处理与该窗口关联的本机对等方,从而导致问题不断

有关更多详细信息,请参阅


假设您试图在屏幕上/下滑动窗口,则应使用Swing
定时器,该定时器在触发时将更新窗口的位置,直到到达其目标点,此时您只需更改窗口的可见性。这假定您希望重用该窗口的实例


有关详细信息,请参阅。

方法的代码是什么
scrollOff()
?添加了
scrollOff()
代码。但请注意;当被其他监听器以外的任何其他监听器触发时,此方法可以正常工作。
循环是危险的,
线程.sleep
是危险的,
dispose
非常可怕……您违反了Swing的单线程规则
dispose
可能正在处理与该窗口关联的本机对等方,导致了无休止的问题…@MadProgrammer:那么,请解释一下我如何正确地执行该操作?我还在学习Java,所以我不确定这样做的“正确”方式。假设您尝试在屏幕上/下滑动窗口,您应该使用Swing
计时器,该计时器在触发时将更新窗口的位置,直到它到达目标点,并且您只需更改窗口的可见性。这假设您想要重用窗口的实例…太棒了!谢谢你澄清这件事。。。帮助我成为一名更好的程序员!英雄联盟事实上,我最初是一个艺术家,开始编程是因为没有其他人来做。。。即使它是错的,代码也会按要求执行。有趣的是,我有一段时间是以相反的方式执行的;)在OP的“编辑2:”下发布了我的更新代码。如果我犯了任何其他错误,请告诉我!注意:我将多个变量更改为字段而不是局部变量,因此我只需初始化它们一次,而不必将它们设置为
final
。只要确保不再使用计时器,就可以停止计时器;)我最终会抓住那个(我希望是x.x),哈哈,谢谢你指出那个!现在修好它。
    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentMoved(ComponentEvent e) {
            setShape(new Rectangle2D.Double(0, 0, getWidth(), newHeight));
            if(!isVisible())
                setVisible(true);
            else if(getBounds().y == screenSize.height - taskBar)
                setVisible(false);
        }
    });