java swing将JPanel与鼠标侦听器的行列值关联

java swing将JPanel与鼠标侦听器的行列值关联,java,swing,grid,jpanel,mouselistener,Java,Swing,Grid,Jpanel,Mouselistener,我正在编写一个带有GUI的棋盘游戏,基本上我有一个10x10的GridLayout JPanel 每个网格单元都是一个正方形的JPanel(我对这些JPanel使用了BorderLayout,因此边框是可见的) 无论如何,我想要它,这样当点击其中一个方块时,它会对boardGameGrid进行更改,boardGameGrid是一个导入到游戏后端GUI的类。说我想用这个方法 boardGameGrid.setCellCross(x,y) 当按下(x,y)位置的电池时 我不知道该怎么做,因为每个J

我正在编写一个带有GUI的棋盘游戏,基本上我有一个10x10的GridLayout JPanel

每个网格单元都是一个正方形的JPanel(我对这些JPanel使用了BorderLayout,因此边框是可见的)

无论如何,我想要它,这样当点击其中一个方块时,它会对boardGameGrid进行更改,boardGameGrid是一个导入到游戏后端GUI的类。说我想用这个方法

boardGameGrid.setCellCross(x,y)
当按下(x,y)位置的电池时

我不知道该怎么做,因为每个JPanel都不包含关于其位置的任何信息


谢谢

为什么不把它作为一个
JTable
而不是JPanel来做呢,你可以在每个单元格中添加一个侦听器。这样,您将知道单击了哪一行和哪一列

您可以使用二维数组存储对面板的引用

例如:

JPanel[][] panels = new JPanel[10][10];

//to store a panel:
JPanel p = new JPanel();
panels[x][y] = p;

//then change your method so that it gets the panel at position (x,y) from the array
public void setCellCross(int x, int y){
     JPanel clickedPanel = panels[x][y];
}

你为什么不让每个小组都知道他的立场:

public MyPanel(int x, int y) {
    this.x = x;
    this.y = y;
}
在鼠标侦听器中:

MyPanel p = (MyPanel) event.getSource();
boardGameGrid.setCellCross(p.getX(), p.getY());
或者,如果您不想更改MyPanel,也可以使用
映射
,并在此映射中存储每个面板的位置:

MyPanel p = (MyPanel) event.getSource();
Point point = panelPositionMap.get(p);
boardGameGrid.setCellCross(p.x, p.y);

你必须准备这个JPanels作为对象,并把它放到地图或列表中,这是一个基于伟大但简单想法的简单示例

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.LineBorder;
//based on @trashgod code from http://stackoverflow.com/a/7706684/714968
public class FocusingPanel extends JFrame {

    private static final long serialVersionUID = 1L;
    private int elements = 10;
    private List<GridPanel> list = new ArrayList<GridPanel>();
    private final JFrame mainFrame = new JFrame();
    private final JPanel fatherPanel = new JPanel();

    public FocusingPanel() {
        fatherPanel.setLayout(new GridLayout(elements, elements));
        for (int i = 0; i < elements * elements; i++) {
            int row = i / elements;
            int col = i % elements;
            GridPanel gb = new GridPanel(row, col);
            list.add(gb);
            fatherPanel.add(gb);
        }
        mainFrame.setLayout(new BorderLayout(5, 5));
        mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mainFrame.add(fatherPanel, BorderLayout.CENTER);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    private GridPanel getGridPanel(int r, int c) {
        int index = r * elements + c;
        return list.get(index);
    }

    private class GridPanel extends JPanel {

        private int row;
        private int col;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(20, 20);
        }

        public GridPanel(int row, int col) {
            this.row = row;
            this.col = col;
            this.setBackground(Color.red);
            this.setBorder(new LineBorder(Color.black,1));
            this.addMouseListener(new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    int r = GridPanel.this.row;
                    int c = GridPanel.this.col;
                    GridPanel gb = FocusingPanel.this.getGridPanel(r, c);
                    System.out.println("r" + r + ",c" + c
                            + " " + (GridPanel.this == gb)
                            + " " + (GridPanel.this.equals(gb)));
                }

                @Override
                public void mousePressed(MouseEvent e) {
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    int r = GridPanel.this.row;
                    int c = GridPanel.this.col;
                    GridPanel gb = FocusingPanel.this.getGridPanel(r, c);
                    System.out.println("r" + r + ",c" + c
                            + " " + (GridPanel.this == gb)
                            + " " + (GridPanel.this.equals(gb)));
                    setBackground(Color.blue);

                }

                @Override
                public void mouseExited(MouseEvent e) {
                    setBackground(Color.red);
                }
            });
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                FocusingPanel focusingPanel = new FocusingPanel();
            }
        });
    }
}

import java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
//基于来自的@trashgod代码http://stackoverflow.com/a/7706684/714968
公共类聚焦面板扩展JFrame{
私有静态最终长serialVersionUID=1L;
私有int元素=10;
私有列表=新的ArrayList();
私有最终JFrame大型机=新JFrame();
private final JPanel fatherPanel=新JPanel();
公众焦点小组(){
setLayout(新的GridLayout(元素,元素));
对于(int i=0;i
每个JPanel都知道它的位置,您可以使用panel.getX()/getY()获得它。看起来你想要的是:

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

public class PanelLocation extends JFrame implements MouseListener
{
    private JPanel mainPanel, footerPanel;
    private JPanel[] panel = new JPanel[9];
    private JTextField tfield;

    public PanelLocation()
    {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3, 3));

        for (int i = 0; i < 9; i++)
        {
            panel[i] = new JPanel();
            panel[i].addMouseListener(this);
            panel[i].setBorder(BorderFactory.createLineBorder(Color.BLUE));
            mainPanel.add(panel[i]);
        }   

        footerPanel = new JPanel();

        tfield = new JTextField(10);
        footerPanel.add(tfield);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        add(mainPanel, BorderLayout.CENTER);
        add(footerPanel, BorderLayout.PAGE_END);

        pack();     
        setVisible(true);
    }



     private void getWhichButtonGotPressed(int x, int y)
     {
         if ((x == panel[0].getX()) && (y == panel[0].getY()))
         {
            panel[0].setBackground(Color.MAGENTA);
            tfield.setText("You Clicked Panel : " + panel[0].getToolTipText());
         }
         else if ((x == panel[1].getX()) && (y == panel[1].getY()))
         {
            panel[1].setBackground(Color.YELLOW);
            tfield.setText("You Clicked Panel : " + panel[1].getToolTipText());
         }
         else if ((x == panel[2].getX()) && (y == panel[2].getY()))
         {
            panel[2].setBackground(Color.RED);
            tfield.setText("You Clicked Panel : " + panel[2].getToolTipText());
         }
         else if ((x == panel[3].getX()) && (y == panel[3].getY()))
         {
            panel[3].setBackground(Color.DARK_GRAY);
            tfield.setText("You Clicked Panel : " + panel[3].getToolTipText());
         }
         else if ((x == panel[4].getX()) && (y == panel[4].getY()))
         {
            panel[4].setBackground(Color.ORANGE);
            tfield.setText("You Clicked Panel : " + panel[4].getToolTipText());
         }
         else if ((x == panel[5].getX()) && (y == panel[5].getY()))
         {
            panel[5].setBackground(Color.PINK);
            tfield.setText("You Clicked Panel : " + panel[5].getToolTipText());
         }
         else if ((x == panel[6].getX()) && (y == panel[6].getY()))
         {
            panel[6].setBackground(Color.BLUE);
            tfield.setText("You Clicked Panel : " + panel[6].getToolTipText());
         }
         else if ((x == panel[7].getX()) && (y == panel[7].getY()))
         {
            panel[7].setBackground(Color.CYAN);
            tfield.setText("You Clicked Panel : " + panel[7].getToolTipText());
         }
         else if ((x == panel[8].getX()) && (y == panel[8].getY()))
         {
            panel[8].setBackground(Color.GRAY);
            tfield.setText("You Clicked Panel : " + panel[8].getToolTipText());
         }
    }

    public void mouseClicked(MouseEvent ae)
    {
        JPanel panel = (JPanel) ae.getSource();

        getWhichButtonGotPressed(panel.getX(), panel.getY());
    }

    public void mousePressed(MouseEvent ae)
    {
    }

    public void mouseReleased(MouseEvent ae)
    {
    }

    public void mouseEntered(MouseEvent ae)
    {
    }

    public void mouseExited(MouseEvent ae)
    {
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PanelLocation();
                }
            });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类PanelLocation扩展JFrame实现MouseListener
{
专用JPanel主面板、页脚面板;
私有JPanel[]面板=新JPanel[9];
私有JTextField;
公共场所()
{
mainPanel=新的JPanel();
设置布局(新的网格布局(3,3));
对于(int i=0;i<9;i++)
{
panel[i]=新的JPanel();
面板[i]。addMouseListener(本);
面板[i].setBorder(BorderFactory.createLineBorder(Color.BLUE));
主面板。添加(面板[i]);
}   
footerPanel=新的JPanel();
tfield=新的JTextField(10);
footerPanel.add(tfield);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(空);
添加(主面板,BorderLayout.CENTER);
添加(页脚面板,边框布局。第_页结束);
包装();
setVisible(真);
}
私有void GetWhichButtongOverpressed(整数x,整数y)
{
如果((x==面板[0].getX())&&(y==面板[0].getY())
{
面板[0]。背景色(颜色为洋红色);
setText(“您单击了面板:+Panel[0].getToolTiptText());
}
else if((x==面板[1].getX())&&&(y)