2016-07-28 97 views
0

我试图实现我自己的工具提示。我使用Timer和鼠标监听器(movedexited)执行此操作。当鼠标移动时,定时器被重置,所以弹出窗口只在鼠标静止时显示。当鼠标退出组件时,弹出窗口被隐藏。但是,当弹出窗口显示在光标处时,鼠标位于弹出窗口内,因此调用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叫,但我不知道如何做到这一点,这也将导致问题当弹出窗口延伸到组件的边缘时。我宁愿鼠标监听器忽略弹出。

如果弹出窗口随鼠标移动,这也可以,但我不知道该怎么做。

+0

你为什么不改变颜色的任何原因希望使用[工具提示](https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html)? – copeg

+0

@copeg我有我想要放入它们的自定义组件 – ricky3350

+0

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

回答

2

但是,当弹出窗口显示在光标处时,鼠标位于弹出框内,因此调用了mouseExited。

您可以检查事件的mouseExited坐标JPanel的范围内发生的,只有隐藏Popup如果事件发生的JPanel范围之外。

if (panel.contains(e.getX(), e.getY())){ 
    return; 
} 

考虑使用tool tips - 它的事件处理你和高度可定制的......多线过html是可以实现的,你可以通过changing the look and feel

0

删除在鼠标离开功能关闭弹出窗口代码(主),将其更改为:

@Override 
public void mouseExited(MouseEvent e) { 
    timer.stop(); 
} 

添加新MouseAdaptor的弹出式菜单,用新mouseExited()功能运行相似代码:

popup.addMouseMotionListener(new MouseAdaptor() { 
    @Override 
    public void mouseExited(MouseEvent e) { 
     popup.hide(); 
    } 
} 

这样,当鼠标离开它时,弹出菜单负责关闭自己。