62,614
社区成员
发帖
与我相关
我的任务
分享
package com.biao;
import java.awt.event.*;
import javax.swing.*;
public class Test01 extends JPanel {
private static final long serialVersionUID = 8610818494539707828L;
private JButton button;
private static int counter = 0;
private boolean show = true;
public Test01() {
button = new JButton("Button");
this.add(button);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
++counter;
System.out.print("Mouse entered: " + counter);
if (!show) {
show = true;
}
System.out.println(", show " + show);
}
@Override
public void mouseExited(MouseEvent e) {
++counter;
System.out.print("Mouse exited: " + counter + " "
+ Test01.this.contains(e.getPoint()));
if (!Test01.this.contains(e.getPoint())) {
show = false;
}
System.out.println(", show " + show);
}
});
}
private static void createGUIAndShow() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().add(new Test01());
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test01.createGUIAndShow();
}
});
}
}