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

时间:2014-02-19 08:57:17

标签: java swing

我有一个问题,我正试图让我的程序打印出一个特定的JPanel作为2x2 GridLayout,上面有扑克牌图标,文字表明哪个玩家的牌位于底部,但不管是什么我这样做,结果是相反的,就像这样。我试过改变添加元素的顺序无济于事。

My current result

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

}

1 个答案:

答案 0 :(得分:-1)

  

“GridLayout上面有扑克牌图标,文字表明哪个玩家的牌位于底部,”

您可以将文字放在同一标签中,只需使用setXxxTextPosition()设置文字位置即可。也许你更喜欢这个。 IMO看起来比将图标和文本分开到目前为止要清晰得多。

enter image description here

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,你需要确保首先添加,然后添加文字标签。

enter image description here

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