gpt4 book ai didi

Java赔率计算器布局

转载 作者:行者123 更新时间:2023-11-30 11:05:41 25 4
gpt4 key购买 nike

我正在用 Java 制作一个扑克赔率计算器。我想要的布局是这样的(现在显示每个玩家下方显示赔率的标签)

enter image description here

表格上方的矩形都是扩展JButton 的Card 对象。玩家下方和 table 上的矩形也是 Card 对象。新的手形与普通的 JButton 一样。死卡也是卡对象。表本身是一个 JLabel。我了解了不同的布局,但不知道要使用哪一种(对于 JFrame 和 JPanel)。如您所见,我还需要将一些组件放置在彼此之上。在这里设置布局的最佳方法是什么?多个面板,空布局?

在代码下方,您可以看到我如何创建 Card 并添加它(我这样做了 52x)。你可以看到我是如何添加 table 和玩家 1 spot 1 Card

public class OddsCalculator extends JFrame{

JPanel cardsPanel;
JLabel tablePicture;

Card aceClubs;
Card kingClubs;
Card queenClubs;
Card jackClubs;
Card tenClubs;
Card nineClubs;
Card eightClubs;
Card sevenClubs;
Card sixClubs;
Card fiveClubs;
Card fourClubs;
Card threeClubs;
Card twoClubs;

//spades
Card aceSpades;
Card kingSpades;
Card queenSpades;
Card jackSpades;
Card tenSpades;
Card nineSpades;
Card eightSpades;
Card sevenSpades;
Card sixSpades;
Card fiveSpades;
Card fourSpades;
Card threeSpades;
Card twoSpades;

//hearts
Card aceHearts;
Card kingHearts;
Card queenHearts;
Card jackHearts;
Card tenHearts;
Card nineHearts;
Card eightHearts;
Card sevenHearts;
Card sixHearts;
Card fiveHearts;
Card fourHearts;
Card threeHearts;
Card twoHearts;

//diamonds
Card aceDiamonds;
Card kingDiamonds;
Card queenDiamonds;
Card jackDiamonds;
Card tenDiamonds;
Card nineDiamonds;
Card eightDiamonds;
Card sevenDiamonds;
Card sixDiamonds;
Card fiveDiamonds;
Card fourDiamonds;
Card threeDiamonds;
Card twoDiamonds;

Card playerOneCardOne;

public OddsCalculator(){
initUI();
}


public void initUI() {;
setLayout(new FlowLayout(FlowLayout.LEFT));
cardsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
tablePicture = new JLabel(new ImageIcon(this.getClass().getResource(Constants.POKERTABLE_ICON)));

aceClubs=new Card();
kingClubs=new Card();
queenClubs=new Card();
jackClubs=new Card();
tenClubs=new Card();
nineClubs=new Card();
eightClubs=new Card();
sevenClubs=new Card();
sixClubs=new Card();
fiveClubs=new Card();
fourClubs=new Card();
threeClubs=new Card();
twoClubs=new Card();

aceSpades=new Card();
kingSpades=new Card();
queenSpades=new Card();
jackSpades=new Card();
tenSpades=new Card();
nineSpades=new Card();
eightSpades=new Card();
sevenSpades=new Card();
sixSpades=new Card();
fiveSpades=new Card();
fourSpades=new Card();
threeSpades=new Card();
twoSpades=new Card();

aceHearts=new Card();
kingHearts=new Card();
queenHearts=new Card();
jackHearts=new Card();
tenHearts=new Card();
nineHearts=new Card();
eightHearts=new Card();
sevenHearts=new Card();
sixHearts=new Card();
fiveHearts=new Card();
fourHearts=new Card();
threeHearts=new Card();
twoHearts=new Card();

aceDiamonds=new Card();
kingDiamonds=new Card();
queenDiamonds=new Card();
jackDiamonds=new Card();
tenDiamonds=new Card();
nineDiamonds=new Card();
eightDiamonds=new Card();
sevenDiamonds=new Card();
sixDiamonds=new Card();
fiveDiamonds=new Card();
fourDiamonds=new Card();
threeDiamonds=new Card();
twoDiamonds=new Card();

playerOneCardOne=new Card();

//setLayout(new FlowLayout(FlowLayout.LEFT));

cardsPanel.setPreferredSize(new Dimension(1200,800));//was 1000/600

getContentPane().add(cardsPanel);

//setSize(1000,1600);
setDefaultCloseOperation(EXIT_ON_CLOSE);


aceClubs.suit=Constants.CARD_SUIT_CLUBS;
aceClubs.kind=Constants.CARD_KIND_ACE;
aceClubs.iconPath=Constants.ACE_CLUBS_ICON;

aceClubs.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(aceClubs.iconPath)));
aceClubs.setBorder(null);
aceClubs.setContentAreaFilled(false);
aceClubs.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
MouseClicked(evt);
}
});
cardsPanel.add(aceClubs);

cardsPanel.add(tablePicture); //add the table

//PLAYER 1 CARD SPOTS
playerOneCardOne.iconPath=Constants.CARD_BACKSIDE;
playerOneCardOne.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardOne.iconPath)));
playerOneCardOne.setBorder(null);
playerOneCardOne.setContentAreaFilled(false);
playerOneCardOne.setLocation(new Point(100,100));
cardsPanel.add(playerOneCardOne);
}

private void MouseClicked(java.awt.event.MouseEvent evt) {

JButton b=(JButton)evt.getSource();
System.out.println("Location: "+b.getLocation());
b.setVisible(false);

}

public static void main(String[] args) {
OddsCalculator oc=new OddsCalculator();
oc.setVisible(true);
oc.pack();
}
}

卡片.cs

public class Card extends JButton{
int suit;
int kind;
boolean known;
String iconPath;
}

最佳答案

使用 BorderLayoutb1,卡片在顶部 Pane (PAGE_START),死卡片在底部 Pane (PAGE_END) 和中心 (CENTER) Pane 中的 BorderLayout, b2。 b1 的左右 Pane 将为空。

将 table 放在 b2 的中央 Pane 中,将玩家放在顶部、底部、左侧 (LINE_START) 和右侧 (LINE_END >) Pane 。

通过将适当的布局管理器添加到 b2.LINE_START Pane 来放置“New Hand”小部件。 GridLayoutGridBagLayout 可能是不错的选择。

我为布局管理器使用了 AWT/Swing 名称。如果您选择使用 JavaFX,只需切换到 JavaFX 等效项:

AWT:BorderLayout = JavaFX:BorderPane

AWT:GridLayout/GridBagLayout = JavaFX:GridPane

在 JavaFX 中,使用 StackPane 将小部件放置在彼此之上。

链接:

AWT/Swing Layout managers

JavaFX Layout managers

关于Java赔率计算器布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29557523/

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