此面板用于组织此方法执行的所有操作。它在一个JFrame里面,一切都很完美,除了这个JPanel里面的定位,它有交错(从左到右,有showQuestionsPanel,新行与右下角对齐,我们有addQuestionPanel,然后与底部对齐showquestions面板,但在addQuestionsPanel的右边,我们有closeEQPanel,在右侧,但在addQuestionsPanel下面是eqbuttonPanel)。 closeEQPanel应该是右上方的面板,左侧是showQuestionsPanel,右下方应该是左侧的addQuestionPanel,右边是eqbuttonPanel,全部对齐为2x2网格。我在这个布局中有什么问题?
//arrange visual elements/create main panel
JPanel mainEQPanel = new JPanel();
GroupLayout eqLayout = new GroupLayout(mainEQPanel);
mainEQPanel.setLayout(eqLayout);
eqLayout.setAutoCreateGaps(true);
eqLayout.setAutoCreateContainerGaps(true);
eqLayout.setHorizontalGroup(
eqLayout.createSequentialGroup()
.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.TRAILING))
.addComponent(showQuestionsPanel)
.addComponent(addQuestionPanel)
.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.CENTER))
.addComponent(closeEQPanel)
.addComponent(eqbuttonPanel)
);
eqLayout.setVerticalGroup(
eqLayout.createSequentialGroup()
.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.LEADING))
.addComponent(showQuestionsPanel)
.addComponent(closeEQPanel)
.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.LEADING))
.addComponent(addQuestionPanel)
.addComponent(eqbuttonPanel)
);
答案 0 :(得分:2)
你几乎就在那里,你的问题是你从未向ParallelGroup分配任何内容,你的bracer / bracket位于错误的位置:
.addGroup(eqLayout.createParallelGroup(GroupLayout.Alignment.TRAILING))//NOTE THE CLOSE BRACKET HERE
.addComponent(showQuestionsPanel)
.addComponent(addQuestionPanel)
但你需要:
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(showQuestionsPanel)
.addComponent(addQuestionPanel))//NOTE THE CLOSE BRACKET HERE INSTEAD