这个面板用来组织这个方法所做的一切。在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)
);
你几乎在那里,你的问题是,你从来没有分配任何东西给你的ParallelGroup,你的支架/括号是在错误的地方:
.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