0

嗨,我已尝试根据我的分配要求实现组布局,下面是我的代码片段,但是当我执行 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
4

1 回答 1

3
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
    [..] 
    is not attached to a vertical group

添加一个垂直组并将组件添加到其中。

JavaDocs

GroupLayout独立处理每个轴即,有表示横轴的组和表示纵轴的组。水平组负责确定沿水平轴的最小、首选和最大尺寸,以及设置其中包含的组件的 x 和宽度。垂直组负责确定沿垂直轴的最小、首选和最大尺寸,以及设置其中包含的组件的 y 和高度。每个都Component必须同时存在于水平和垂直组中,否则IllegalStateException在布局期间或请求最小、首选或最大尺寸时会抛出一个。

于 2012-05-06T16:29:57.393 回答