誰かが特定の長方形の領域にカーソルを合わせたときにスライド効果を得るためにaJPanel
を使用して aの境界を変更していますが、これは期待どおりに機能しています。Timer
にはボタンがあり、動作JPanel
が異なりMouseEvent
ます。しかし、時々遅れたり、スライドが非常に遅くなったりします。毎回うまく機能するように、誰かが私に何ができるかを提案できますか?
編集:
その中buttonsPanel
にbutton
ある。として境界を持つbuttonsPanel
a に追加されます。JLayeredPane または JButtonの場合、マウスイベントがトリガーされます。次に、マウス イベントによってタイマーがトリガーされ、ビューが表示されます。JLayeredPane
rect
button
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);
}
}
}