java - JPanel 的 addMouseListener

标签 java swing jpanel awt mouselistener

今天有个问题.. 我的程序制作了一个 8x8 网格并在我单击 JButton 时显示坐标。

但我拒绝使用 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();
        }
    }

最佳答案

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更多详情...

关于java - JPanel 的 addMouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16431455/

相关文章:

java - 为什么阻塞 IO API 在早期的 Java 中不是问题?

java - MVP、JFrame、JDialog : GUI is freezing

java - 在一个 jframe 中的两个 jpanel 中使用两种绘制方法

java - 调整大小是重新绘制 JFrame 的唯一方法。重绘();似乎不起作用

java Jbutton数组阻止构造函数完成运行

java - 如何通过代码向 Facebook 好友发送电子邮件或消息

java - ANTLR4 -> 检查文字是 STRING_LITERAL 还是 NUMBER_LITERAL

java - Derby DB - 在 Java 中将 String 保存为 CLOB 时要转义的字符列表

java - 将包含 Mysql 表中数据的行添加到 jtable

java - 向一个 JFrame 添加多种类型的背景