本文介绍了JPanel的addMouseListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Today I have a problem..My program make a 8x8 grid and show the coord when I click on a JButton.

但是我拒绝使用JButton,所以我需要使用JPanel.但是我的addMouseListener无法正常工作,所以我不知道如何修复自4小时以来的问题.....

BUT I refuse to use JButton and I need to go for JPanel.. But my addMouseListener isn't working so I don't know how is it possible to fix that I'm searching since 4h.....

    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

JPanel doesn't have ActionListener capabilities. Instead, you need to use a 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();
            }
        });
    }
}

看看如何编写鼠标侦听器更多详细信息...

Take a look at How to Write Mouse Listeners for more details...

这篇关于JPanel的addMouseListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:19