gpt4 book ai didi

java - BoxLayout 无法共享。尝试将数组中的多个标签添加到 BoxLayout 面板中

转载 作者:行者123 更新时间:2023-11-30 03:58:03 26 4
gpt4 key购买 nike

我知道这里有很多 BoxLayout 问题,但是我找不到可以解决我的问题的问题。我需要我的 ScoreDescPanel 来在彼此正下方显示每个标签(如列表),但是我在使用 BoxLayout 时遇到问题。该问题出现在底部的 scoreDescPanel.add(lblScoreDesc[i]); 行上。

private JFrame frame;
private JPanel panel;
private JPanel dicePanel;
private JButton btnRoll;
private JButton[] btnDice = new JButton[5];

private JPanel mainPanel;
private JPanel scoreDescPanel;
private JPanel scoreBtnPanel;

private JLabel[] lblScoreDesc = new JLabel[20];
private JButton[] btnScore = new JButton[20];

private Yahtzee y = new Yahtzee();

public YahtzeeGUI(){
createWindow();
addButtonRoll();
addButtonDice();
addMainPanel();
addScoreDesc();
//addScoreCardUpper();
//addScoreCardLower();


frame.add(panel);
frame.setVisible(true);
}

public void createWindow(){
frame = new JFrame();
frame.setTitle("Yahtzee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,700);
panel = new JPanel(new BorderLayout());
dicePanel = new JPanel();

mainPanel = new JPanel();
scoreDescPanel = new JPanel();

}

public void addButtonRoll(){
btnRoll = new JButton ("Roll the Dice");
btnRoll.addActionListener(new RollHandler());

dicePanel.add (btnRoll);
panel.add(dicePanel, BorderLayout.SOUTH);
}

public void addButtonDice(){

for (int i = 0; i < btnDice.length; i++){
btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
btnDice[i].addActionListener(new HoldHandler());
dicePanel.add (btnDice[i]);
}
panel.add(dicePanel, BorderLayout.SOUTH);
}

public void addMainPanel(){
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.red);
panel.add(mainPanel, BorderLayout.CENTER);
}

public void addScoreDesc(){
lblScoreDesc[0] = new JLabel("UPPER SECTION");
lblScoreDesc[1] = new JLabel("Aces");
lblScoreDesc[2] = new JLabel("Twos");
lblScoreDesc[3] = new JLabel("Threes");
lblScoreDesc[4] = new JLabel("Fours");
lblScoreDesc[5] = new JLabel("Fives");
lblScoreDesc[6] = new JLabel("Sixes");
lblScoreDesc[7] = new JLabel("TOTAL SCORE");
lblScoreDesc[8] = new JLabel("BONUS");
lblScoreDesc[9] = new JLabel("TOTAL UPPER");
lblScoreDesc[10] = new JLabel("LOWER SECTION");
lblScoreDesc[11] = new JLabel("3 of a Kind");
lblScoreDesc[12] = new JLabel("4 of a Kind");
lblScoreDesc[13] = new JLabel("Full House");
lblScoreDesc[14] = new JLabel("Small Straight");
lblScoreDesc[15] = new JLabel("Large Straight");
lblScoreDesc[16] = new JLabel("Yahtzee!");
lblScoreDesc[17] = new JLabel("Chance");
lblScoreDesc[18] = new JLabel("TOTAL LOWER");
lblScoreDesc[19] = new JLabel("GRAND TOTAL");

mainPanel.add(scoreDescPanel, BorderLayout.WEST);
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));

for(int i = 0; i < lblScoreDesc.length; i++){
scoreDescPanel.add(lblScoreDesc[i]);
}
}

最佳答案

BoxLayout 不允许使用与设置布局的目标容器不同的目标容器,即

scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));

应该是

scoreDescPanel.setLayout(new BoxLayout(scoreDescPanel, BoxLayout.Y_AXIS));

阅读:How to Use BoxLayout

关于java - BoxLayout 无法共享。尝试将数组中的多个标签添加到 BoxLayout 面板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622107/

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