简体   繁体   English

通过单击JPanel获取JPanel [] []坐标

[英]Getting JPanel[][] coordinates by clicking on a JPanel

I've created a JPanel[][] Array. 我创建了一个JPanel[][]数组。

private JPanel[][] pnlFeld;

And filled it with panels 并装满面板

for (int i = 0; i < world.getSize(); i++) {
        for (int j = 0; j < world.getSize(); j++) {
            pnlFeld[i][j] = new JPanel();
            pnlFeld[i][j].setBorder(new EtchedBorder());
            pnlFeld[i][j].addMouseListener(ml);
            pnlFeld[i][j].setBackground(off);
            add(pnlFeld[i][j]);
        }
    }

Now I want to get the array coordinates ( [][] ) by clicking on them and I have no clue how to do that. 现在,我想通过单击它们来获取数组坐标( [][] ),但我不知道如何执行此操作。

I've only added methods to change the color of the panel I clicked on, nothing related to my problem. 我仅添加了一些方法来更改单击的面板的颜色,而与我的问题无关。

MouseListener ml = new MouseListener() {

        @Override
        public void mouseEntered(MouseEvent e) {
            if (world.getMode().equals("Malen")) {
                if (e.getSource() instanceof JPanel)
                    e.getComponent().setBackground(on);
            //  check();
            } 
            else if (world.getMode().equals("Radieren")) {
                if (e.getSource() instanceof JPanel)
                    e.getComponent().setBackground(off);
            //  check();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (world.getMode().equals("Setzen")) {
                    if (e.getSource() instanceof JPanel) {
                        if (e.getComponent().getBackground() == off) {
                            e.getComponent().setBackground(on);
                        } else
                            e.getComponent().setBackground(off);
                    }
                //  check();
                }
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}
    };

Actually you can use getBounds() to get component location and size. 实际上,您可以使用getBounds()来获取组件的位置和大小。 If you mean the array indexes there could be multiple solutions. 如果您的意思是数组索引,那么可能有多种解决方案。

  1. Define a Map and place all your panels in the Map with String value eg i+":"+j (or define simple pojo class with 2 fields i and j. 定义一个Map并将所有面板放置在带有String值的Map中,例如i +“:” + j(或定义具有2个字段i和j的简单pojo类。

  2. Create unique listener for each JPanel to keep the i and j. 为每个JPanel创建唯一的侦听器,以保留i和j。

  3. Place the panels in a containr with GridBagLayout then you can use gridBagLayoutInstance.getConstraints(theClickedPanel) and check row column of the constraint 将面板放在带有GridBagLayout的容器中,然后可以使用gridBagLayoutInstance.getConstraints(theClickedPanel)并检查约束的行列

Getting JPanel[][] coordinates by clicking on a JPanel 通过单击JPanel获取JPanel[][]坐标

Use a JButton[][] with ActionListener for easier coding and a better user experience. JButton[][]ActionListener一起使用ActionListener编码并提供更好的用户体验。 The ActionEvent has a getSource() method that will identify the button that was activated. ActionEvent具有getSource()方法,该方法将标识已激活的按钮。

This chess GUI uses buttons for the 64 places on the chessboard. 国际象棋GUI使用按钮在棋盘上的64个位置。

If you create your MouseListener in your loop, you can use i en j within the code of your listener. 如果在循环中创建MouseListener ,则可以在侦听器的代码内使用i en j The drawback is that you'll have a bit more instances of your listener. 缺点是您将有更多的侦听器实例。

I could suggest that you set the name of each JPanel as i and j in the loop where you declare and initialise the panels. 我建议您在声明和初始化面板的循环中将每个JPanel的名称设置为ij As illustrated 如图所示

pnlFeld[i][j].setName(i + "" + j);

And in your mouseClicked event check if the event source is an instance of JPanel and parse the name to get the x and y coordinates like this: 然后在mouseClicked事件中检查事件源是否是JPanel的实例,并解析名称以获取x和y坐标,如下所示:

Integer.parseInt(p.getName().subString(0,1))
Integer.parseInt(p.getName().subString(1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM