简体   繁体   English

如何获得从网格单击的组件?

[英]How can I get the component I clicked on from a grid?

I am displaying a grid of objects, and I want to print the name of the one I click on. 我正在显示一个对象网格,我想打印单击的对象的名称。 However the following code only registers a click when I click the top-left-most square, and then every single card prints its name. 但是,下面的代码仅在我单击最左上角的方块时才会注册一次单击,然后每张卡都会打印其名称。

Any idea what I need to do differently? 知道我需要做些什么吗?

public class GHand extends JPanel {

    GCard[] grid; //names the grid of buttons

    public GHand(int width, int height, Hand hand) { //constructor
        this.setLayout(new GridLayout(width, 4));
        grid = new GCard[hand.size()]; //allocate the size of grid
        for (int i = 0; i < grid.length; i++) {
            grid[i] = new GCard(hand.getCard(i)); //creates new card
            this.add(grid[i]); //adds card to grid
        }

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);
                for (GCard g : grid) {

                    if (g.contains(me.getPoint())) {//check if mouse is clicked within card

                        System.out.println("Clicked a " + g.getCardName());

                    }
                }
            }
        });
    }
}

I imagine it would be wasteful to add a listener to every object in the grid right? 我想向网格中的每个对象添加侦听器会很浪费,对吗? What's the best practice for this? 最佳做法是什么?

I think you are on the right track... 我认为您在正确的道路上...

I don't think that you should be using MouseEvent since that tracks the mouse motion and clicking and so you would have to track where exactly your grid item is. 我认为您不应该使用MouseEvent,因为它会跟踪鼠标的运动和单击,因此您必须跟踪网格项的确切位置。 You may want to do this but you will have to create more code in order to solve where your grid item is and that will involve much complicated code to do with the GUI. 您可能想要这样做,但是您必须创建更多代码才能解决网格项的位置,而这将涉及与GUI关联的许多复杂代码。

My opinion is that you should be using an ActionEvent Listener so that you when you do click on an object (I am assuming a JPanel or Button object) you can respond to the click that has happened. 我的意见是,您应该使用ActionEvent Listener,这样当您单击对象(我假设是JPanel或Button对象)时,您可以响应发生的单击。 Through the .getCommand() method. 通过.getCommand()方法。

You will have to change the GHand object or the GCard object (depending on what you are using to show on the grid) to include a name variable that can be used to differentiate between different objects. 您将必须更改GHand对象或GCard对象(取决于您要在网格上显示的内容),以包括可用于区分不同对象的名称变量。 (This can be as simple as a single counter method to determine the ID of the object) OR you could just leave it as a name variable. (这可以像确定对象ID的单一计数器方法一样简单),也可以将其保留为名称变量。 You will have to override the .toString() method (inside the GHand/GCard object) in order to allow the .getCommand() method to respond with a reader friendly response so that you can direct your program flow according to what has been clicked. 您将必须重写.toString()方法(在GHand / GCard对象内部),以便允许.getCommand()方法以读者友好的响应进行响应,以便您可以根据单击的内容来定向程序流。


This should not be wasteful to adding a listener since you should be adding already to the single object class. 这对添加侦听器应该不会造成浪费,因为您应该已经将其添加到单个对象类中。 I have assumed that you are using a grid to allow the user to click on the object (I don't know if were using many GHands with multiple GCards or just the one GHand with multiple GCards to display on the screen). 我假设您使用的是网格以允许用户单击对象(我不知道是使用多个GCard的多个GHands还是仅将多个GCard的一个GHand显示在屏幕上)。 Because of this you can create simple objects and spread them over a grid, where you can click on the grid object and it should respond to the click, therefore grabbing your method and then calling it. 因此,您可以创建简单的对象并将其分布在网格上,您可以在其中单击网格对象,并且该网格对象应响应单击,因此可以获取您的方法,然后调用它。

Hope this helps! 希望这可以帮助!

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

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