需要多个 JPanel 来响应其自己的鼠标单击事件

发布于 2024-09-24 09:09:33 字数 1404 浏览 6 评论 0原文

我的应用程序上有多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2024-10-01 09:09:33

问题似乎是每个方形面板都有一个类变量和一个局部变量。

Square square1 = new Square("1"); 

应该是:

square1 = new Square("1"); 

现在你只有一个 PuzzleListener 可以引用的类变量。

The problem would appear to be that you have a class variable and a local variable for each of your square panels.

Square square1 = new Square("1"); 

should be:

square1 = new Square("1"); 

now you will only have a class variable which the PuzzleListener can reference.

若水般的淡然安静女子 2024-10-01 09:09:33

是否需要使用单个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文