需要多个 JPanel 来响应其自己的鼠标单击事件
我的应用程序上有多个 JPanel,但是我不知道如何检测到底单击了哪一个 JPanel。
在我的 MouseListener 中,我有参数 e 但我的方法不起作用
在我的代码早期,我声明了多个 JPanel 和一个侦听器对象。
PuzzleListener plist = new PuzzleListener();
JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5));
...
Square square1 = new Square("1");
Square square2 = new Square("2");
...
puzzle_board.add(square1);
puzzle_board.add(square2);
...
square1.addMouseListener(plist);
square1.addMouseListener(plist);
类 PuzzleListener 实现 MouseListener
{
public void mouseClicked(MouseEvent e)
{
JPanel pnlClick = (JPanel)(e.getSource());
//System.out.println(pnlClick);
//System.out.println(e.getSource());
//System.out.println(e.getComponent().getClass());
//System.out.println(e.getComponent().getClass().getName());
//problem is here
if(pnlClick == square1)
{
System.out.println("Panel 1 has been clicked");
}
if(pnlClick == square2)
{
System.out.println("Panel 2 has been clicked");
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
//System.out.println("Panel 1 has been clicked");
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
/*
public void actionPerformed(ActionEvent e)
{
JPanel pnlClick = (JPanel)(e.getSource());
}*/
}//end calcListener
I have multiple JPanels on my application, however I cannot figure out how to detect which exactly which one was clicked.
In my MouseListener, I have the argument e but my method isn't working
Early in my code I declare multiple JPanels and a listener object.
PuzzleListener plist = new PuzzleListener();
JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5));
...
Square square1 = new Square("1");
Square square2 = new Square("2");
...
puzzle_board.add(square1);
puzzle_board.add(square2);
...
square1.addMouseListener(plist);
square1.addMouseListener(plist);
class PuzzleListener implements MouseListener {
public void mouseClicked(MouseEvent e) { JPanel pnlClick = (JPanel)(e.getSource()); //System.out.println(pnlClick); //System.out.println(e.getSource()); //System.out.println(e.getComponent().getClass()); //System.out.println(e.getComponent().getClass().getName()); //problem is here if(pnlClick == square1) { System.out.println("Panel 1 has been clicked"); } if(pnlClick == square2) { System.out.println("Panel 2 has been clicked"); } } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { //System.out.println("Panel 1 has been clicked"); } public void mouseReleased(MouseEvent e) { } public void mousePressed(MouseEvent e) { } /* public void actionPerformed(ActionEvent e) { JPanel pnlClick = (JPanel)(e.getSource()); }*/ }//end calcListener如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题似乎是每个方形面板都有一个类变量和一个局部变量。
应该是:
现在你只有一个 PuzzleListener 可以引用的类变量。
The problem would appear to be that you have a class variable and a local variable for each of your square panels.
should be:
now you will only have a class variable which the PuzzleListener can reference.
是否需要使用单个 MouseListener?您可以创建 PuzzleListener 的新实例并将其添加到每个 JPanel。这样就不会混淆哪个面板上的哪个侦听器被触发。
Is there a need to use a single MouseListener? You could create a new instance of your PuzzleListener and add it to each JPanel. That way there is no confusion as to which listener on which panel is getting fired.