我有一个问题,我正在尝试让我的程序以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);
  }


下面是我创建的类CardTable的扩展JFrame的构造函数。

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

关于java - Java:如何在2x2 GridLayout中更改JLabel的顺序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21875412/

10-13 03:20