繁体   English   中英

Java:如何在2x2 GridLayout中更改JLabel的顺序?

[英]Java: How do I change the order of my JLabels in a 2x2 GridLayout?

我有一个问题,我正在尝试让我的程序以2x2 GridLayout的形式打印一个特定的JPanel,并在顶部显示纸牌图标,并在底部显示指示哪个玩家卡牌的文本,但是无论我做什么,结果是相反的,就像这样。 我尝试更改添加元素的顺序,但无济于事。

我目前的结果

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

以下是我制作的扩展Table的CardTable类的构造函数。

  static int MAX_CARDS_PER_HAND = 57;
static int MAX_PLAYERS = 2;  // for now, we only allow 2 person games
private int numCardsPerHand;
private int numPlayers;
private JPanel computerPanel, playerPanel, gamePanel;
public CardTable(String title, int numCardsPerHand, int numPlayers) 
{
    super(title);

    setComputerPanel(new JPanel(new GridLayout(1 , numCardsPerHand)));
    setPlayerPanel(new JPanel(new GridLayout(1 , numCardsPerHand)));
    setGamePanel(new JPanel(new GridLayout(2 , numPlayers)));

    setLayout (new BorderLayout(20, 10));
    add(getComputerPanel(), BorderLayout.NORTH );
    add(getGamePanel(), BorderLayout.CENTER);
    add(getPlayerPanel(), BorderLayout.SOUTH);

}

“ GridLayout的顶部有纸牌图标,底部有文本指示哪个球员卡。”

您可以将文本图标放在相同的标签中,而只需使用setXxxTextPosition()设置文本位置。 也许你更喜欢这个 IMO看起来比将图标和文本彼此分开要干净得多。

在此处输入图片说明

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

更新

如果你真的必须与坚持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);
            }
        });
    }
}

暂无
暂无

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

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