java - 关于集团布局

标签 java swing grouplayout

嗨,我尝试根据我的分配要求实现组布局,下面是我的代码片段,但当我执行 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 treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. The horizontal group is responsible for determining the minimum, preferred and maximum size along the horizontal axis as well as setting the x and width of the components contained in it. The vertical group is responsible for determining the minimum, preferred and maximum size along the vertical axis as well as setting the y and height of the components contained in it. Each Component must exist in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.

关于java - 关于集团布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472044/

相关文章:

java - 无法将 JLabel 与 JCheckBox 垂直对齐,就像单个 JCheckBox 与 GroupLayout 一样

java - 验证自定义 Java 注释

java - 将 byte[] 缓冲区重置为零?

java - 如何在 firebase 中获取所有 child 的数据并将其显示到我的 Android 应用程序中?

java - 为什么 @TableGenerator(name ="tabgen1",table ="cID",allocationSize=1) 创建一个新表

Java GroupLayout 异常

java - 如何在 Java 小程序中将 GroupLayout 的内容居中

java - 如果我通过调用外部 API 获取流,是否应该关闭流?

Java:JScrollPane 覆盖 JFrame 光标设置,但仅在启动时

java - 按像素在其图标上移动 JButton 文本