嗨,我
试图实现组布局,我的赋值要求下面是我的代码片段,但是当我做pack()时,我在这个代码片段中遇到了一个问题; 它也抛出异常,我不知道如何使其可见,请指导我在哪里错误 代码建议会有所帮助
提前致谢
public class AMS_GUI extends JFrame
{
private JFrame frame;
public AMS_GUI()
{
makeFrame();
}
public void makeFrame()
{
JLabel unitLabel = new JLabel("Units"); // units label
JComboBox unitCombo = new JComboBox(); // units empty combo box
JButton addUnit = new JButton("Add"); // add units button for adding units
JLabel AssessmentLabel = new JLabel("Assessments"); // assessments Label
JComboBox AssessmentCombo = new JComboBox(); // assessments empty combo box
JButton addAssessment = new JButton("Add"); // assessments add button
JLabel TasksLabel = new JLabel("Tasks"); // tasks Label
JComboBox TasksCombo = new JComboBox(); // tasks empty combo box
JButton addTasks = new JButton("Add"); // tasks add button
JButton editTasks = new JButton("Edit");// tasks Edit button
JLabel planLabel = new JLabel("Plans");
JButton makePlan = new JButton("MakePlan");
JButton showPlan = new JButton("ShowPlan");
JButton savePlan = new JButton("SavePlan");
//Set up the content pane.
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(unitLabel)
.addComponent(AssessmentLabel)
.addComponent(TasksLabel)
.addComponent(planLabel)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(unitCombo)
.addComponent(AssessmentCombo)
.addComponent(TasksCombo)
.addComponent(makePlan)
.addComponent(showPlan)
.addComponent(savePlan))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(addUnit)
.addComponent(addAssessment)
.addComponent(addTasks)
.addComponent(editTasks)
)
);
setTitle("AMS_GUI");
pack();
例外
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,
border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@bb6ab6,flags=296,
maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,
margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,
paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,
selectedIcon=,text=Edit,defaultCapable=true]
is not attached to a vertical group
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
[..]
is not attached to a vertical group
添加垂直组并向其中添加组件。
来自JavaDocs:
GroupLayout
独立处理每个轴。也就是说,有一个组代表水平轴,一个组代表垂直轴。水平组负责确定沿水平轴的最小、首选和最大尺寸,以及设置其中包含的组件的 x 和宽度。垂直组负责确定沿垂直轴的最小、首选和最大尺寸,以及设置其中包含的组件的 y 和高度。每个Component
必须同时存在于水平和垂直组中,否则在布局期间或请求最小、首选或最大大小时会引发IllegalStateException
。