简体   繁体   中英

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

I have a problem in that I'm trying to get my program to print out one particular JPanel as a 2x2 GridLayout with Icons of playing cards on top, and text indicating which player's card is it on bottom, but no matter what I do, the result is reverse, like so. I have tried changing the order in which I add the elements to no avail.

我目前的结果

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

Below is the constructor of the class that I made, CardTable, that extends 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 with Icons of playing cards on top, and text indicating which player's card is it on bottom,"

You can put text and an icon in the same label and just set the text positions with setXxxTextPosition() . Maybe you'd prefer this . IMO it looks much cleaner than separating the icon and text so far apart from each other.

在此处输入图片说明

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

UPDATE

If you really must stick with the GridLayout , you need to make sure to add the cards first , then add the text labels.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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