Gridlayout和鼠标侦听器

时间:2015-05-21 10:41:48

标签: java user-interface grid-layout mouselistener

您好我正在尝试使用鼠标侦听器构建网格布局GUI。因此,当在网格中单击特定单元格时,将显示信息。我不知道从哪里开始,任何帮助都会很好 谢谢你

3 个答案:

答案 0 :(得分: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){}

在每种方法中,您都可以获得

的来源
MouseEvent e
使用

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

然后你引用了源代码,所以你可以修改你想要的东西。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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