English 中文(简体)
Java Swing:在返回Int [复制品]之前放弃使用Clicked活动
原标题:Java Swing: Waiting for Mouse Clicked event before returning an Int [duplicate]

我有一张卡片游戏,其基础是利用ArrayList把卡储存在人民手中。 我有一条主要方法,通过游戏,利用主要班级中使用Invoke Later的read子,更新GUI班。

此时,我正在玩弄青 con和投入,并装入扫描器,以便从参与者手中挑选卡片。 我想做的是,在参与者必须作出选择时,也许会随着援引和等待而走到一起。 然后等待参与者点击德国马克的对应卡,并返回卡片的适当位置。 然后由我的游戏处理这一选择。

现在我如何更新我的全球倡议。

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

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

            }
        });

给听众贴上卡片,把他们的名字贴在他们相应的位置上。

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

在烟火被点击时,储存供选择的斜体。

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

我想将这一英寸归还给我的主班“游戏”方法,用于计算的其他部分。 我可以说明如何做到这一点,特别是等待选择。 我听到,这可以做到,但我对如何在我的工作中执行这一点感到不快。 如果任何人能够帮助,那将是巨大的。

问题回答

你们不想在这里穿透镜或任何这样的东西。 相反,根据用户在方案中所处的位置,你改变了方案的状况,而根据用户的回复和方案的行为,视国家不同而有所不同。

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.

然后,主要的霍恩语班将提升目前Player变量,供下级角色使用,并等待他们的答复。

Edit 1
You state:

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.

请理解,你重新进行大规模过渡,从一个基于青少年的线性方案转向一个基于GUIevent的方案,关键是新方案是以活动为基础的。 如果你目前的法典写得好业绩不佳的OOPs级,加上最低的静态变量和方法,以及具有明确定义的等级,那么过渡应当顺利进行。 另一方面,如果你目前的代码基数只有少数几类,大多数以线虫用户界面为中心,那么你可能需要重新制定你的代码。

你不妨向我们介绍一下你目前的一些法典,并描述其设计。

下面请说明:

我将如何保证该方案不会对除无外的任何东西作出反应?

请澄清你所说的话,即方案仅对暗中作出反应。 什么地方? 在JText 现场 什么地方? 我认为,这可能需要某种类型的投入验证。

你们想要的是习惯组成部分的模式,而这种模式不可能实现(至少没有执行你自己的模式的努力手段)。 你们的方案应该有国家,而且应该以活动为基础,就像“手工艺”的回答。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签