componentListener被禁用

时间:2014-03-06 23:42:11

标签: java swing mouselistener jwindow componentlistener

我有一个不寻常的问题,即只要mouseListener / Adapter检测到事件,就会禁用componentListener / Adapter。在下面的代码中,componentMoved()方法覆盖完全有效,直到mouseClicked()中触发MouseAdapter()方法覆盖为止。任何想法如何解决这个问题?

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);
}

这基本上与scrollOn()方法完全相反,只要通过Listener以外的任何方式触发,它都可以正常工作。

编辑2:由于MadProgrammer的建议,下面修改了代码

public void scrollOff() {
    x = screenSize.width - getWidth();
    yEnd = screenSize.height - taskBar;
    yStart = this.getBounds().y;
    setLocation(x,yStart);
    current = yStart;
    newHeight = this.getBounds().height;

    ActionListener action = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(current < yEnd) {
                current+=2;
                newHeight = yEnd - current;
                setLocation(x,current);
            } else {
                timer.stop();
            }
        }
    };

    timer = new Timer(30, action);
    timer.setInitialDelay(0);
    timer.start();
    Main.alertControl.setAlertActive(false);
}

我还使用AlertScroller()更新了else if构造函数方法,以便在完成后正确隐藏窗口:

    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)放置在其他位置会导致窗口再次显示。

1 个答案:

答案 0 :(得分:3)

while loop是危险的,Thread.sleep是危险的,处置只是简单的恐怖......

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

有关详细信息,请参阅Concurrency in Swing

dispose可能正在处理与窗口关联的本地对等方,导致问题没有结束......

有关详细信息,请参阅JWindow#dispose

假设您正在尝试滑动窗口开/关屏幕,您应该使用Swing Timer,当触发时,它将更新窗口的位置,直到它到达目标点,以及您何时只会改变窗口的可见度。这假设你想重用窗口的实例..

有关详细信息,请参阅How to Use Swing Timers