简体   繁体   English

Java Swing:Swing Worker,invokeAndWait和重叠的JLabel

[英]Java Swing: Swing Worker, invokeAndWait and Overlapping JLabels

Hey guys I've come with 2 problems, both being Java Swing related. 大家好,我遇到了2个问题,都与Java Swing有关。 I am developing a card game in Java. 我正在用Java开发纸牌游戏。 I use arreays lists of type to hold the values of each card and I have a main Play() method that calls updates to the GUI using invokeLater and a Singleton approach to my GUI class. 我使用类型的arreays列表来保存每张卡的值,并且我有一个主要的Play()方法,该方法使用invokeLater和对我的GUI类的Singleton方法调用对GUI的更新。

  1. The first question is fairly simple. 第一个问题很简单。 I create the cards on the GUI using JLabels like so; 我使用JLabel在GUI上创建卡,如下所示: attaching relevent listeners and adding them to the appropriate panel, in this case, the 'Human Hand Panel': 附加相关事件侦听器并将其添加到适当的面板(在本例中为“人手面板”):

      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) { e.printStackTrace(); } JLabel picLabel = new JLabel(new ImageIcon( cardImage )); picLabel.addMouseListener((MouseListener) me); HumanHandDisplay.add(picLabel); } //HumanHandDisplay.validate(); HumanHandDisplay.updateUI(); 

The problem I'm having is that when the human hand is more than 7 cards or so, it creates a larger panel area and starts a new row of cards beneath. 我遇到的问题是,当人的手超过7张卡片左右时,它将创建较大的面板区域并在下面开始新的一排卡片。 What I would like to do is, when the hand reaches a certain size, the cards start to overlap eachother (like you would hold cards in your hand). 我想做的是,当手达到一定大小时,卡片开始相互重叠(就像您将卡片握在手中一样)。 I've messed about with .validate() but gotten nowhere. 我搞砸了.validate(),却一无所获。 Any help you can give here would be most welcome. 您在这里可以提供的任何帮助都将受到欢迎。

  1. My second question is about using a Swing Worker to return the Human player's card selection. 我的第二个问题是关于使用Swing Worker返回人类玩家的纸牌选择。 I have read a little bit about Swing workers but I'm unsure as to the best way to implement one in my own game. 我已经阅读了一些有关Swing工作者的信息,但是我不确定在自己的游戏中实现Swing工作者的最佳方法。 At present, using the console I have a scanner than takes the input of an int as the choice (the place of the specific card in the ArrayList). 目前,使用控制台,我有一个扫描器,然后将一个int的输入作为选择(特定卡在ArrayList中的位置)。 I would like this int to be selected by clicking on the card in the players hand. 我希望通过单击玩家手中的卡来选择此int。 At the moment I use: 目前,我使用:

      String name = "" + i; picLabel.setName(name); 

    to set the names of the Card JLabels to the int in the for loop creating them (as shown^^), and I use: 在创建它们的for循环中将Card JLabels的名称设置为int(如图所示^^),我使用:

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

    To return that int when one of the cards is clicked. 单击其中一张卡片时返回该int。 Here is what I've been using to call the GUI methods from Play() aswell: 这也是我一直从Play()调用GUI方法的方法:

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

    My question is, how can I get the Play method to call a method in the GUI class, which sets up the hand with the appropriate listeners (lets call it GUIPlayerSelection() for now) then wait for the player to click a card, which then returns the int to my Play() method in my main class that represents the card selected. 我的问题是,如何获得Play方法来调用GUI类中的方法,该方法将使用适当的侦听器(这时将其称为GUIPlayerSelection())建立起手形,然后等待玩家单击卡,然后将int返回到我的主类中代表所选卡的Play()方法中。 I'm unsure as how to use invoke and wait, as in, if I use invoke and wait, will it just wait for the cards to be set up, or will it wait for the mouseClicked() method to finish aswell? 我不确定如何使用调用和等待,例如,如果我使用调用和等待,它将只是等待卡的设置,还是会等待mouseClicked()方法完成? Or will I have to do something else to make sure it waits for the mouse to be clicked after the hand set-up? 还是我需要做其他事情以确保它在手设置好之后等待鼠标单击? And how then will I return that int? 然后我将如何返回该int? I've been told to use a swing worker but if someone could explain how I can implement that in this case that would be awesome. 我被告知要使用摇摆工人,但如果有人可以解释我在这种情况下如何实现这一点,那就太好了。

Thank-you in advance. 先感谢您。

For your first question, are you having an issue getting the frame to repaint or with the layout of the cards? 对于第一个问题,您是否在获取框架以重新粉刷或卡片布局方面遇到问题?

If the latter, then you may want to have a look at Java Layout Managers , it describes how the content pane inside your JFrame organizes the components added to it. 如果是后者,那么您可能想看看Java Layout Managers ,它描述了JFrame内的内容窗格如何组织添加到其中的组件。

If none of those will work for you (and I don't think they will with that you describe), you can use the setBounds method to manually align your JLabels. 如果这些都不适合您(我认为它们不会与您描述的一样),则可以使用setBounds方法手动对齐JLabel。

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

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