gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:17:09 25 4
gpt4 key购买 nike

我有一个问题,因为我试图让我的程序将一个特定的 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);

}

最佳答案

"GridLayout with Icons of playing cards on top, and text indicating which player's card is it on bottom,"

您可以将文本放在同一个标​​签中,只需使用setXxxTextPosition()设置文本位置即可。也许您更喜欢这个。在我看来,它看起来比将图标和文本分开那么远要干净得多。

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com