我想在java中編寫一個表單,但在將JLabels動態插入到當前的JDialog並執行pack()
之後,窗口被調整爲最小。顯示JLabels,但我必須手動調整窗口大小。 這裏是將Jlabel插入部分:JDialog.pack()隱藏JLabel
public void displayQuizz(Test quiz){
int xLable = 44;
int yLable = 41;
int widthLable = 403;
int heightLable = 70;
int noOfQuestion = 1;
for(Question question : quiz.getQuestions()){
JLabel lblNewLabel = new JLabel(Integer.toString(noOfQuestion) + ". " + question.getStatement());
lblNewLabel.setBounds(xLable, yLable, widthLable, heightLable);
contentPanel.add(lblNewLabel);
contentPanel.revalidate();
contentPanel.repaint();
this.pack();
noOfQuestion++;
yLable += heightLable;
}
}
不要使用'setBounds()'。 – trashgod