簡體   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