[英]Swing mouseEntered strange behavior
I have a jframe with some JComponents, with some mouseListener. 我有一个带有一些JComponents和一些mouseListener的jframe。 My aim is to show a little jframe with a specified text, when the mouse enter on a jlabel, and to show off it when the mouse is exited.
我的目的是在鼠标进入jlabel时显示带有指定文本的小jframe,并在退出鼠标时显示它。 Jframe is supposed to be shown near the mouse.
应该将Jframe显示在鼠标附近。 Anyway that does not happen, and the programm behaves in a very strange way.
无论如何都不会发生,并且程序的行为非常奇怪。 Why?
为什么? the bug That's my code
错误是我的代码
package finestrina;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class finestra implements MouseListener{
private JFrame finestra = new JFrame();
private JFrame pagina = new JFrame();
private JButton submit1 = new JButton("press");
private JTextField text = new JTextField();
finestra(){
pagina.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pagina.setSize(500, 500);
JPanel cont = new JPanel();
cont.setLayout(new GridLayout(3,4));
JLabel label = new JLabel();
label.setText("ON MOUSEROVER THIS");
cont.add(label);
label.addMouseListener(this);
submit1.addMouseListener(this);
text.addMouseListener(this);
cont.add(submit1);
cont.add(text);
pagina.add(cont);
pagina.setVisible(true);
finestra.setUndecorated(true);
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent event) {
if(event.getSource() instanceof JLabel){
JLabel event_casted = (JLabel)event.getSource();
if(event_casted.getText().equals("ON MOUSEROVER THIS")){
Point punto = event.getLocationOnScreen();
punto.setLocation(punto.getX()+20, punto.getY()+20);
JLabel littlelabel = new JLabel();
littlelabel.setText("your mouse is on the jlabel");
finestra.add(littlelabel);
finestra.setLocation(punto);
finestra.setSize(100,100);
finestra.setVisible(true);
}
}
}
@Override
public void mouseExited(MouseEvent event) {
if(event.getSource() instanceof JLabel){
JLabel event_casted = (JLabel)event.getSource();
if(event_casted.getText().equals("ON MOUSEROVER THIS")){
finestra.setVisible(false);
}
}
}
@Override
public void mousePressed(MouseEvent event) {
}
@Override
public void mouseReleased(MouseEvent arg0) {}
public static void main(String[] args0){
new finestra();
};
}
There are a number of (possible) issues 有很多(可能的)问题
GridLayout
will cause the component to occupy most of the space of the container, which might cause the window to "appear" to popup earlier then you expect GridLayout
将导致组件占据容器的大部分空间,这可能会导致窗口“出现”在您期望的更早时间弹出 finestra.add(event_casted);
is causing the label to be removed from it's current parent container (the main window), as a component can only belong to a single container It would, generally be better to use the tooltip support provided by the API. 通常,最好使用API提供的工具提示支持。 Maybe have a look at How to Use Tool Tips , remember, they can also support HTML.
也许看看如何使用工具提示 ,记住,它们也可以支持HTML。
If that's not functionality what you want, then maybe a JPopupMenu
might be better 如果那不是您想要的功能,那么也许
JPopupMenu
可能更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.