2015-05-21 138 views
0

您好我正在尝试使用鼠标监听器来构建网格布局GUI。所以当一个特定单元格在网格中被点击时,会显示信息。我不知道从哪里开始,任何帮助都会很好 谢谢您Gridlayout和鼠标监听器

回答

0

在您的网格布局中,使用某个组件(如Button或Label)设置所有网格。您可以在添加的组件上设置监听器,并在单击组件时显示信息。

0

要正确使用gridbaglayout,您应该首先处理gridbagconstraints。然后,您应该使用接口来处理鼠标点击。如果单元格的类型为标签,则可以使用myLabel隐藏文本。 setText(“”)并使用myLabel.setText(“要显示的信息”)放置文本。如果你需要更多的帮助,只需要问:D和+1如果有帮助^^

1

我相信你有一个继承自JPanel或JFrame的类,并且它里面有整个GUI。然后,这个类应该实现mouseListener。那么你的类应该有类似的代码:

@override 
public void mouseClicked(MouseEvent e){} 
@override 
public void mousePressed(MouseEvent e){} 
@override 
public void mouseEntered(MouseEvent e){} 
@override 
public void mouseReleased(MouseEvent e){ 
    /*This method is being called when you release your click. It's better 
     then mouseClicked because mouseClicked is only called when you press 
     and release on the same pixel or Object (not sure about it) 
    */ 
} 
@override 
public void mouseExiteded(MouseEvent e){} 

在每一个方法,你可以使用

Object source = e.getSource(); 
if (source == button1){ 
    //Do sth 
}if (source == button2){ 
    //Do sth else 
}if (source == radioButton1){ 
    //Do whatever you want 
} 

然后,你必须参考源得到的

MouseEvent e 

来源,所以你可以修改你想要什么。