当有人将鼠标悬停在特定矩形区域上时,我正在JPanel使用 a更改 a 的边界Timer以获得滑动效果,这按预期工作。中有按钮,JPanel它们有不同的MouseEvent行为。但有时它开始滞后或滑动非常缓慢。请,任何人都可以建议我该怎么做,以确保它每次都表现良好?
编辑:
有buttonsPanel在button里面。buttonsPanel被添加到JLayeredPane边界为 的 a 中rect。当 JLayeredPane 或 JButtonbutton为hovered鼠标事件时触发。鼠标事件然后触发计时器slide the buttonsPanel并将其带到视图中。
class MyActionListener implements ActionListener {
    private static final int frames = 50;
    int count = 0;
    private final Timer timer = new Timer(1, this);
    public void start() {
        timer.start();
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
        if (count >= frames) {
            timer.stop();
        } else {
            count = count + 1;
            buttonsPanel.setBounds(hidden_width - hidden_width * count / frames, 0, hidden_width, frameHeight);
        }
    }
}
class MyMouseListener extends MouseAdapter {
    private MyActionListener timerListener;
    @Override
    public void mouseEntered(final MouseEvent event) {
        final Color color = new Color(50, 50, 50);
        if (event.getSource() instanceof JButton) {
            final JButton button = (JButton) event.getSource();
            button.setBackground(color);
            buttonsPanel.setVisible(true);
        } else if (!buttonsPanel.isVisible()) {
            buttonsPanel.setVisible(true);
            timerListener = new MyActionListener();
            timerListener.start();
        }
    }
    @Override
    public void mouseExited(final MouseEvent event) {
        Point point = new Point(0, 0);
        if (event.getSource() instanceof JButton) {
            final JButton button = (JButton) event.getSource();
            point = button.getLocation();
            button.setBackground(Windows8GUI.color);
        }
        Rectangle rect = new Rectangle(0, 0, hidden_width, frameHeight);
        if (!rect.contains(point.x + event.getX(), point.y + event.getY())) {
            buttonsPanel.setVisible(false);
            buttonsPanel.setBounds(0, 0, hidden_width, frameHeight);
        }
    }
}