简体   繁体   English

swing-在JPanel上单击获取组件

[英]swing - get component clicked on JPanel

OK so I've got a JPanel with a GridLayout. 确定,所以我有一个带有GridLayout的JPanel。 Each cell of the grid then contains another JPanel. 然后,网格的每个单元格都包含另一个JPanel。

What I'd like to be able to do is have a listener on the "underneath" JPanel which then tells me which of the "overlayed" JPanels was clicked - so I can react to it and the surrounding ones, without making the covering JPanels aware of their position (they change!!) 我想做的是在“下方” JPanel上有一个侦听器,然后告诉我单击了“叠加” JPanel中的哪个-这样我就可以对它和周围的JPanels做出反应,而无需覆盖JPanels知道他们的位置(他们改变了!)

Is there a way of doing this - similar to Determine clicked JPanel component in the MouseListener. 有没有一种方法-与在MouseListener中确定单击的JPanel组件类似 Event handling but I couldn't find a way of grabbing the component on top. 事件处理,但是我找不到在最上面抓住组件的方法。

I could probably grab the co-oridnates and work it out using that info - but I'd rather not!! 我可能会抓住这些共同的牧师,并使用该信息来解决它-但我宁愿不要!

Any help/pointers/tips would be appreciated :D 任何帮助/指针/提示将不胜感激:D

Do the same thing but use getParent() on the source. 做同样的事情,但是在源代码上使用getParent() Or you can search up the hierarchy if it is deeper, even some helper methods for that: javax.swing.SwingUtilities.getAncestorOfClass and getAncestorNamed 或者,您可以搜索层次结构是否更深,甚至可以找到一些帮助方法: javax.swing.SwingUtilities.getAncestorOfClassgetAncestorNamed

use putClientProperty / getClientProperty , nothing simplest around ..., you can put endless numbers of ClientProperty to the one Object 使用putClientProperty / getClientProperty ,最简单的方法就是...,您可以将无数的ClientProperty放在一个对象中

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MyGridLayout {

    public MyGridLayout() {
        JPanel bPanel = new JPanel();
        bPanel.setLayout(new GridLayout(10, 10, 2, 2));
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 10; col++) {
                JPanel b = new JPanel();
                System.out.println("(" + row + ", " + col + ")");
                b.putClientProperty("column", row);
                b.putClientProperty("row", col);
                b.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        JPanel btn = (JPanel) e.getSource();
                        System.out.println("clicked column " + btn.getClientProperty("column")
                                + ", row " + btn.getClientProperty("row"));
                    }
                });
                b.setBorder(new LineBorder(Color.blue, 1));
                bPanel.add(b);
            }
        }
        JFrame frame = new JFrame("PutClientProperty Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(bPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MyGridLayout myGridLayout = new MyGridLayout();
            }
        });
    }
}

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

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