Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何在2x2网格布局中更改JLabel的顺序?_Java_Swing - Fatal编程技术网

Java:如何在2x2网格布局中更改JLabel的顺序?

Java:如何在2x2网格布局中更改JLabel的顺序?,java,swing,Java,Swing,我有一个问题,我试图让我的程序打印出一个特定的JPanel作为2x2网格布局,上面有扑克牌图标,底部有文本指示哪位玩家的牌,但无论我做什么,结果都是相反的,就像这样。我试图改变添加元素的顺序,但没有效果 CardTable myCardTable = new CardTable("CS 1B CardTable", NUM_CARDS_PER_HAND, NUM_PLAYERS); myCardTable.setSize(800, 600); myCardTable.se

我有一个问题,我试图让我的程序打印出一个特定的JPanel作为2x2网格布局,上面有扑克牌图标,底部有文本指示哪位玩家的牌,但无论我做什么,结果都是相反的,就像这样。我试图改变添加元素的顺序,但没有效果

  CardTable myCardTable 
     = new CardTable("CS 1B CardTable", NUM_CARDS_PER_HAND, NUM_PLAYERS);
  myCardTable.setSize(800, 600);
  myCardTable.setLocationRelativeTo(null);
  myCardTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // show everything to the user
  myCardTable.setVisible(true);
  myCardTable.getGamePanel().setBorder(new TitledBorder("Playing Area"));
  myCardTable.getGamePanel().add(playerCard, JLabel.CENTER);
  myCardTable.getGamePanel().add(computerCard, JLabel.CENTER);
  for(int i = 0; i < NUM_PLAYERS; i++)
  {
      JLabel temp = new JLabel(GUICard.getIcon(generateRandomCard()));
      myCardTable.getGamePanel().add(temp); 
  }

顶部有扑克牌图标的GridLayout,底部有文本指示哪位玩家的牌

您可以将文本和图标放在同一标签中,只需使用
setxxxtposition()
设置文本位置即可。也许你更喜欢这个。在我看来,它看起来比将图标和文本分隔得如此之远要干净得多


更新

如果您确实必须坚持使用
GridLayout
,则需要确保先添加卡片,然后添加文本标签

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PlayerCard {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ImageIcon playerIcon = new ImageIcon(getClass().getResource("/resources/1.png"));
                ImageIcon compIcon = new ImageIcon(getClass().getResource("/resources/14.png"));
                JLabel playerLabel = new JLabel(playerIcon);
                JLabel compLabel = new JLabel(compIcon);
                JLabel playerText = new JLabel("Player Card");
                JLabel compText = new JLabel("Computer Card");

                playerLabel.setHorizontalAlignment(JLabel.CENTER);
                compLabel.setHorizontalAlignment(JLabel.CENTER);

                JPanel playerCardPanel = new JPanel();
                playerCardPanel.add(playerLabel);

                JPanel compCardPanel = new JPanel();
                compCardPanel.add(compLabel);

                JPanel panel = new JPanel(new GridLayout(2, 2));
                panel.add(playerCardPanel);
                panel.add(compCardPanel);
                panel.add(playerText);
                panel.add(compText);

                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}


这看起来很有趣,如果我不是为了做作业,我肯定会去做的。也就是说,我想我们都记得安克思教授对项目规范的态度,所以我恐怕不能冒险。不过我很感谢你的努力,谢谢你。@user2019645教授有没有特别说过在中心面板上使用2x2
GirsLayout
?没有,给我们的整个作业似乎非常开放,以一种令人担忧的模棱两可的方式,如果有意义的话。作业超过6页是没有帮助的。也就是说,我大约80%确信你的答案会得到他的充分肯定,但我可以清楚地想象他会因为我没有遵守项目规范而指责我。如果没有规定,就这么做吧。它看起来比将文本和标签分隔得如此之远要干净得多。您可以使用1x2网格。在任何给定的时间内,中心是否会有多张玩家卡或计算机卡?很难说原因,因为代码中没有显示原因。你的游戏面板是什么?你有什么布局管理器?GridLayout(如果您正在使用)将从上/左开始添加组件,用所有列填充一整行,直到下一行中断。因此,第一个组件应该是卡片,第二个组件应该是标签。但是,只有在游戏面板是网格布局的情况下……当我阅读你的代码时,还有一件事让我感到困惑:;您已经硬编码了“playerCard”和“computerCard”,这意味着您将始终有两个玩家;你和电脑--然后你添加了一个NUM_播放器的for循环,在那里你(可能)会添加更多的标签。。。?你有,而且永远都会有两名球员,还是我错过了什么?
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class PlayerCard {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ImageIcon playerIcon = new ImageIcon(getClass().getResource("/resources/1.png"));
                ImageIcon compIcon = new ImageIcon(getClass().getResource("/resources/14.png"));
                JLabel playerLabel = new JLabel(playerIcon);
                JLabel compLabel = new JLabel(compIcon);
                playerLabel.setText("Player Card");
                compLabel.setText("Computer Card");
                playerLabel.setVerticalTextPosition(JLabel.BOTTOM);
                compLabel.setVerticalTextPosition(JLabel.BOTTOM);
                playerLabel.setHorizontalTextPosition(JLabel.CENTER);
                compLabel.setHorizontalTextPosition(JLabel.CENTER);

                JPanel playerPanel = new JPanel();
                playerPanel.setBorder(new TitledBorder("Player"));
                playerPanel.add(playerLabel);

                JPanel compPanel = new JPanel();
                compPanel.setBorder(new TitledBorder("Computer"));
                compPanel.add(compLabel);

                JPanel panel = new JPanel();
                panel.add(playerPanel);
                panel.add(compPanel);

                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PlayerCard {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ImageIcon playerIcon = new ImageIcon(getClass().getResource("/resources/1.png"));
                ImageIcon compIcon = new ImageIcon(getClass().getResource("/resources/14.png"));
                JLabel playerLabel = new JLabel(playerIcon);
                JLabel compLabel = new JLabel(compIcon);
                JLabel playerText = new JLabel("Player Card");
                JLabel compText = new JLabel("Computer Card");

                playerLabel.setHorizontalAlignment(JLabel.CENTER);
                compLabel.setHorizontalAlignment(JLabel.CENTER);

                JPanel playerCardPanel = new JPanel();
                playerCardPanel.add(playerLabel);

                JPanel compCardPanel = new JPanel();
                compCardPanel.add(compLabel);

                JPanel panel = new JPanel(new GridLayout(2, 2));
                panel.add(playerCardPanel);
                panel.add(compCardPanel);
                panel.add(playerText);
                panel.add(compText);

                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}