【问题标题】:addMouseListener for a JPanel用于 JPanel 的 addMouseListener
【发布时间】:2013-05-02 03:20:28
【问题描述】:

今天我有一个问题.. 当我点击JButton 时,我的程序会创建一个 8x8 网格并显示坐标。

但我拒绝使用 JButton,我需要使用 JPanel.. 但我的 addMouseListener 无法正常工作,所以我不知道如何解决我从 4 小时开始搜索的问题.....

    package coordboutons;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class CoordBoutons extends JFrame {

        CoordBoutons() {
            super("GridLayout");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container contenant = getContentPane();
            contenant.setLayout(new GridLayout(8, 8));

            for (int i = 0; i < 8; i++) 
                for (int j = 0; j < 8; j++)
                    contenant.add(new CaseEchiquier(i, j));

            pack();
            setVisible(true);
        }

        **class CaseEchiquier extends JPanel** {
            private int lin, col;
            CaseEchiquier(int i, int j) {
                lin = i;
                col = j;
                setPreferredSize(new Dimension(80, 75));
                setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
                addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        System.out.println((char)('a' + col) + "" + (8 - lin));

                    }
                });
            }


        }

        public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            CoordBoutons coordBoutons = new CoordBoutons();
        }
    }

【问题讨论】:

  • 我可能在这里遗漏了一些东西,但ActionListener 不是MouseListener
  • 你的意思是addActionListener 不工作。那是因为你不能用这种类型的监听器注册JPanel
  • 您尝试在面板中添加鼠标侦听器,对吗?但是您的代码是 actionListener,JPanel 中没有 actionlistener 方法。我认为拥有 64 个 newMouseListeners 是个坏主意,所有 64 个面板只需要一个。所以在你的 CaseEchiquier(int i, int j, MouseListener m) 中添加一个 mouselistener 作为参数
  • “我拒绝使用 JButton”……为什么?

标签: java swing jpanel awt mouselistener


【解决方案1】:

JPanel 没有ActionListener 功能。相反,您需要使用MouseListener

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CoordBoutons extends JFrame {

    CoordBoutons() {
        super("GridLayout");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contenant = getContentPane();
        contenant.setLayout(new GridLayout(8, 8));

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                contenant.add(new CaseEchiquier(i, j));
            }
        }

        pack();
        setVisible(true);
    }

    class CaseEchiquier extends JPanel {

        private int lin, col;

        CaseEchiquier(int i, int j) {
            lin = i;
            col = j;
            setPreferredSize(new Dimension(80, 75));
            setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
            addMouseListener(new MouseAdapter() {
                private Color background;

                @Override
                public void mousePressed(MouseEvent e) {
                    background = getBackground();
                    setBackground(Color.RED);
                    repaint();
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    setBackground(background);
                }
            });
//            addActionListener(new ActionListener() {
//                public void actionPerformed(ActionEvent evt) {
//                    System.out.println((char) ('a' + col) + "" + (8 - lin));
//
//                }
//            });
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                CoordBoutons coordBoutons = new CoordBoutons();
            }
        });
    }
}

查看How to Write Mouse Listeners了解更多详情...

【讨论】:

  • 哦,伙计们,非常感谢你,我搜索了很长时间......现在我知道它有效,我可以通过点击案例获得坐标 =)
  • 感谢您的帮助!非常有用 ;) 但是,如何在不使用“System.out.println”的情况下从每个案例中获取坐标。我想要 getX() 和 getY() 之类的东西,带有字母 A-H(列)和数字 1-8(行)
  • 这将取决于您实际拥有的更多信息,您需要知道每列的宽度和每行的高度。更好的解决方案可能是使用来自类本身的信息,lincol
【解决方案2】:

问题在于 JPanel 不存在 addActionListener 方法。对于这种情况,您应该使用适当的侦听器 (java.awt.event.MouseListener)。由于MouseListener 是一个接口(并且您不想实现它的所有方法),您可以使用MouseAdapter 并仅覆盖您需要的方法,如下所示:

addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println((char)('a' + col) + "" + (8 - lin));
    }
});

【讨论】:

  • 非常感谢我现在明白了 =) 很大的帮助!
猜你喜欢
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2020-08-03
  • 2012-01-19
  • 2018-07-07
相关资源
最近更新 更多