java swing将鼠标监听器的JPanel与行列值相关联 [英] java swing associating JPanel with row col values for mouse listener

查看:136
本文介绍了java swing将鼠标监听器的JPanel与行列值相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每个网格单元都是一个正方形JPanel(我使用BorderLayout来实现这些功能) JPanels使边框可见)。

无论如何,我希望它能够在单击其中一个方块时,对boardGameGrid进行更改,这是一个导入到游戏后端的GUI的类。假设我想要使用该方法

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $

当位置(x,y)处的单元格被按下时。

我无法



感谢

如何做到这一点,因为每个JPanel都不包含任何有关其位置的信息。解决方案

您必须将此JPanels作为Object准备,并将其放到Map或List中,这个简单的示例基于伟大而简单的想法

< img src =https://i.stack.imgur.com/Uuvwx.jpgalt =here>

  import java.awt。*; 
import java.awt.event。*;
import java.util.ArrayList;
import java.util.List;
import javax.swing。*;
import javax.swing.border.LineBorder;
//基于@trashgod代码http://stackoverflow.com/a/7706684/714968
公共类FocusingPanel扩展JFrame {

私有静态最终长序列版本号= 1L ;
private int elements = 10;
私人列表< 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);


私有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 = Grid Panel.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();
}
});
}
}


I'm writing a board game with a GUI and basically I have a 10x10 GridLayout JPanel.

Each grid cell is a square JPanel (I used BorderLayout for these JPanels so the borders are visible).

Anyway I want it so that when one of these squares is clicked, it makes a change to the boardGameGrid which is a class imported to the GUI for the backend of the game. Say I want to use the method

boardGameGrid.setCellCross(x,y)

when the cell at position (x,y) is pressed.

I can't figure out how to do this since each JPanel does not contain any information about its position.

thanks

解决方案

You have to prepare this JPanels as Object and put that to the Map or List, simple example based on great but simple idea

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();
            }
        });
    }
}

这篇关于java swing将鼠标监听器的JPanel与行列值相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆