嗨我已尝试实现组布局,我的作业要求下面是我的代码片段,但是当我执行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
答案 0 :(得分:3)
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
[..]
is not attached to a vertical group
添加一个垂直组并将组件添加到其中。
来自JavaDocs:
GroupLayout
独立地处理每个轴。也就是说,有一个表示水平轴的组和一个表示垂直轴的组。水平组负责确定沿水平轴的最小,最佳和最大尺寸,以及设置其中包含的组件的x和宽度。垂直组负责确定沿垂直轴的最小,最佳和最大尺寸,以及设置其中包含的组件的y和高度。 每个Component
必须同时存在于水平和垂直组中,否则在布局期间或者在请求最小,首选或最大大小时会抛出IllegalStateException
。