Java Swing:在返回Int之前,等待Mouse Clicked事件 [英] Java Swing: Waiting for Mouse Clicked event before returning an Int

查看:168
本文介绍了Java Swing:在返回Int之前,等待Mouse Clicked事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个纸牌游戏,它是基于使用ArrayLists将纸牌存储在人们手中的.我有一个主要的方法,可以在游戏中进行游戏,并使用在主类中使用InvokeLater调用的线程来更新GUI类

I have a card game and which is built on using ArrayLists to store the cards in peoples hands. I have a main method which plays through the game and updates the GUI class using threads called using InvokeLater in the main class

此刻,我正在控制台上玩游戏,然后将其输入并插入扫描仪,以便从玩家手中选择纸牌.我想做的是运行一个线程,可能带有调用和等待,该线程在玩家必须做出选择时才运行.然后等待玩家在GUI中单击相应的卡并返回卡位置的相应整数.然后由我的游戏处理此选择.

At the moment I'm playing the game on the console and inputting and int into a scanner to make the selection of card from the players hand. What I want to do is to run a thread, probably with invoke and wait, that runs when the player has to make a selection. Then waits for the player to click on the corresponding card in the GUI and return the appropriate int of the place of the card. This selection is then handled by my game.

此刻我如何更新GUI.

How I'm updating my GUI at the moment.

      public static void Play(ArrayList<Card> ...){
        UpdateHand(player1, deck, deckused);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUI.getInstance().UpdateHand();

            }
        });

为卡片分配听众并将其名称设置为手中相应位置的整数.

Attaching listeners to cards and setting their names to the int of their corresponding place in the hand.

   public void PlayerSelection()
    {
        selection = -1;
        Information = new JLabel("Please Select Your Card");
        ButtonDisplay.add(Information);
        ButtonDisplay.updateUI();
        for (int i = 0; i < (HumanHand.size()); i++)
           {
               Card card = HumanHand.get(i);

               BufferedImage cardImage = null;

                try {

                    cardImage = ImageIO.read(new File("card/" + card + ".jpg"));
                    } catch (IOException e) {
            // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    JLabel picLabel = new JLabel(new ImageIcon( cardImage ));
                    String name = "" + i;
                    picLabel.setName(name);
                    picLabel.addMouseListener((MouseListener) this);
                    HumanHandDisplay.add(picLabel);
           }
    }   

存储int以供单击鼠标时选择.

storing the int for selection when the mouse is clicked.

    public void mouseClicked(MouseEvent e) {
        String name = ((JLabel)e.getSource()).getName();
        System.out.println("Working " + name);
        selection = Integer.parseInt(name);         
    }

我想将此int返回到我的主类Play()方法中,以在其余计算中使用.我不知道该怎么做,特别是等待选择.我听说可以做到这一点,但是我不确定如何在我的工作中实现这一点.如果有人可以帮助,那就太好了.

I want to return this int to my main class Play() method for use in the rest of the computation. I can't work out how to do this, specifically waiting for the selection to be made. I've heard that this can be done but I'm unsure as to how to implement this in my work. If anyone could help that would be great.

推荐答案

否,您不想在此处暂停线程或任何此类线程.取而代之的是,您将根据用户在程序中的位置以及用户的响应和状态来更改程序的状态,具体取决于用户的响应和程序的行为.

No, you don't want to pause threads or any of that sort here. Instead you'll change the state of your program depending on where in the program the user is, and depending on his responses and the behavior of the program will vary depending on that state.

例如,如果用户需要等待用户玩纸牌,则可以给主类一个currentPlayer变量,并在轮到该用户时将其分配给该用户.然后对该程序进行编码,以使该程序除了适合其转弯的有效输入外,不响应任何其他用户输入.

For instance, if the user needs to wait for a user to play a card, you give the main class a currentPlayer variable, and assign it to the user when its his turn. Then code the program so that it doesn't respond to any user input except for valid input that is appropriate for his turn.

然后,GameEngine主类将把currentPlayer变量前进到下一个玩家,并等待他们的响应.

Then the main GameEngine class will advance the currentPlayer variable to the next player and await their responses.

编辑1
您声明:

Edit 1
You state:

我不确定我是否真的了解.我已经实现了游戏及其所有规则和要与之对抗的AI玩家,目前我已经在控制台上进行了操作.我只需要将使用扫描仪进行选择的位置扩展到GUI上,以等待Mouse Clicked事件.

I'm not sure I really understand. I've already implemented the game and all it's rules and AI players to play against, which I do on the console for now. I just need to extend where I'm making selections using a scanner out on to the GUI to wait for a Mouse Clicked event.

请理解,您正在做一个很大的过渡,从基于控制台的线性程序过渡到基于GUI event 的GUI程序,关键是新程序是基于事件的.如果您当前的代码是用行为良好的OOP兼容类,最少的静态变量和方法以及状态明确的类编写的,那么过渡应该顺利进行.另一方面,如果您当前的代码库只有几个类,而大多数类都集中在线性控制台用户界面上,那么您可能需要从头开始重新编写代码.

Please understand that you're making a large transition, moving from a console based linear program to a GUI event-based program, the key being that the new program is event-based. If your current code is written with well-behaved OOPs-compliant classes, with minimal static variables and methods, and with classes that have well-defined states, then the transition should go smoothly. If on the other hand your current code base has only few classes, most centered around the linear console user interface, you may need to re-write your code from scratch.

您可能希望向我们展示一些当前代码并描述其设计.

You may wish to show us some of your current code and describe its design.

接下来您要声明:

我将如何确保该程序除对int以外的其他任何响应?

How would I go about makeing sure that the program doesn't respond to anything other than an int?

请弄清楚程序仅响应int的含义.一个int在哪里?在JTextField中?别的地方?我认为,这可能需要某种类型的输入验证.

Please clarify what you mean by having the program only respond to an int. An int where? in a JTextField? Somewhere else? This may likely require some type of input validation, I think.

这篇关于Java Swing:在返回Int之前,等待Mouse Clicked事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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