是的,这个问题已经发布在 JavaRanch 上,但我不太了解它们。
我有 JDialog,其中添加了 MouseListener,如下所示:
super.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
if (e.getSource() instanceof ConnectionTreeTooltip) {
System.out.println("mouse exited!!!!!!!!!!!!!!!!!!=" + e);
// hideTooltip();
}
}
@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource() instanceof ConnectionTreeTooltip) {
System.out.println("mouse entered!!!!!!!!!!!!!!!!!!=" + e);
// hideTooltip();
}
}
});
ConnectionTreeTooltip
这是jdialog吗?问题是当鼠标进入或退出 JDialog 时,这两种方法都会被调用。他们只是一起跑。
我的 JDialog 的代码:
public class ConnectionTreeTooltip extends JDialog {
...........
public ConnectionTreeTooltip(ConnectionsTree connectionsTree) {
super(connectionsTree.getMainFrame(), "", false);
super.setUndecorated(true);
super.setFocusableWindowState(false);
.............
super.getContentPane().add(scrollPane);
super.pack();
}
connectionsTree.getMainFrame()
返回 JFrame 的实例。
编辑这是完整的代码:
public ConnectionTreeTooltip(ConnectionsTree connectionsTree) {
super(connectionsTree.getMainFrame(), "", false);
super.setUndecorated(true);
super.setFocusableWindowState(false);
this.connectionsTree = connectionsTree;
JPanel contentPane = (JPanel) super.getContentPane();
contentPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.blue, Color.red));
tipLabel = new JLabel();
// by default, JLabel is not focusable.
tipLabel.setFocusable(true);
tipLabel.setBackground(Color.WHITE);
scrollPane = new JScrollPane(tipLabel) {
@Override
public Dimension getPreferredSize() {
return preferredSizeOfScrollPane;
}
};
scrollPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 1, 1));
super.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
if (e.getSource() instanceof ConnectionTreeTooltip) {
System.out.println("mouse exited!!!!!!!!!!!!!!!!!!=" + e);
// hideTooltip();
}
}
@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource() instanceof ConnectionTreeTooltip) {
System.out.println("mouse entered!!!!!!!!!!!!!!!!!!=" + e);
// hideTooltip();
}
}
});
tipLabel.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// tipLabel can key events only if it is visible and focused
if (e.getKeyCode() == 32) {
unFocusTipLabel();
}
}
});
super.getContentPane().add(scrollPane);
super.pack();
}
为什么会这样,我的 JDialog 或 MouseListener 有什么问题?
谢谢!