我试图实现我自己的工具提示。我使用Timer
和鼠标监听器(moved和exited)执行此操作。当鼠标移动时,定时器被重置,所以弹出窗口只在鼠标静止时显示。当鼠标退出组件时,弹出窗口被隐藏。但是,当弹出窗口显示在光标处时,鼠标位于弹出窗口内,因此调用mouseExited
。弹出消失,但是,如果鼠标保持静止,它会再次发生,导致弹出窗口闪烁。这可以通过移动弹出窗口1px来防止,以便鼠标不在弹出窗口中,但这并不能解决整个问题,因为将鼠标移动到弹出窗口上会使其消失。防止在退出到弹出窗口时调用mouseExited
我MCVE:
private static Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel pop = new JPanel(new GridLayout(0, 3));
pop.setBackground(Color.BLUE);
// Do calculations similar to what would actually be happening,
// because otherwise the flicker is too fast to demonstrate on my screen
for (double i = Math.random() * 12; i < 40; i++) {
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g = img.createGraphics();
g.drawString(Math.log(Math.sqrt(Math.random())) + "", 0, 32);
g.dispose();
pop.add(new JLabel(new ImageIcon(img)));
}
popup = PopupFactory.getSharedInstance().getPopup(panel, pop, x, y);
popup.show();
}
});
private static JPanel panel;
private static Popup popup;
private static int x, y;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
timer.setRepeats(false);
if (popup == null) timer.restart();
x = e.getXOnScreen(); // Adding one here eliminates the flicker problem,
y = e.getYOnScreen(); // but still calls mouseExited entering the popup
}
@Override
public void mouseExited(MouseEvent e) {
if (popup != null) {
popup.hide();
popup = null;
}
timer.stop();
}
};
panel = new JPanel();
panel.setBackground(Color.GREEN);
panel.addMouseListener(ma);
panel.addMouseMotionListener(ma);
frame.add(panel);
frame.setVisible(true);
}
我在想,也许mouseExited
方法应检查是否弹出的mouseEntered
叫,但我不知道如何做到这一点,这也将导致问题当弹出窗口延伸到组件的边缘时。我宁愿鼠标监听器忽略弹出。
如果弹出窗口随鼠标移动,这也可以,但我不知道该怎么做。
你为什么不改变颜色的任何原因希望使用[工具提示](https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html)? – copeg
@copeg我有我想要放入它们的自定义组件 – ricky3350
FWIW JToolTip扩展了JComponent - 你可以重写'JComponent.createToolTip'来返回你自己定制的JToolTip(应该注意这需要一些布局和大小的调整 - 你必须设置JToolTip的布局,并覆盖它的'getPreferredSize'如[这里]提到的(http://stackoverflow.com/questions/9522475/custom-java-tool-tip-with-swing-components-as-content-does-not -出现) )。 – copeg