我知道这里有很多BoxLayout
个问题,但我找不到解决问题的方法。我需要我的scoreDescPanel
将每个标签直接显示在彼此之下(如列表)但是我遇到了BoxLayout
的问题。 priblem出现在底部的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]);
}
}
答案 0 :(得分:2)
BoxLayout
不允许与设置布局的目标容器不同,即
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
应该是
scoreDescPanel.setLayout(new BoxLayout(scoreDescPanel, BoxLayout.Y_AXIS));