Java GroupLayout定位

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java GroupLayout定位相关的知识,希望对你有一定的参考价值。

我在尝试设置GroupLayout时遇到了并发症。我试图从oracle的网站上删除一个教程但是因为我的生活无法弄明白。下面是我当前的布局设计,后面是代码,不知道我做错了什么。

enter image description here

GroupLayout layout = new GroupLayout(mainPanel);
        mainPanel.setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                .addComponent(launchButton)
                .addComponent(modeComboBox)
                .addComponent(difficultyComboBox)
                .addComponent(mapComboBox)
                .addComponent(startButton)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))    
        );

        layout.setVerticalGroup(
                layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(launchButton)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
                            .addComponent(modeComboBox)
                            .addComponent(difficultyComboBox)
                            .addComponent(mapComboBox)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
                            .addComponent(startButton))
        );

我的目标是让它最终看起来像下面的图片......

enter image description here

答案

我启动了NetBeans并展示了你的图片。试试这个,看看它是不是你要找的东西。

package quicktest;

public class ChaosLayout extends javax.swing.JPanel {

   /**
    * Creates new form ChaosLayout
    */
   public ChaosLayout() {
      initComponents();
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings( "unchecked" )
   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
   private void initComponents() {

      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();
      jButton5 = new javax.swing.JButton();

      jButton1.setText("jButton1");

      jButton2.setText("jButton2");

      jButton3.setText("jButton3");

      jButton4.setText("jButton4");

      jButton5.setText("jButton5");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(55, 55, 55)
            .addComponent(jButton3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
               .addComponent(jButton1)
               .addComponent(jButton2)
               .addComponent(jButton5))
            .addGap(18, 18, 18)
            .addComponent(jButton4)
            .addContainerGap(84, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jButton2)
               .addComponent(jButton3)
               .addComponent(jButton4))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
            .addComponent(jButton5)
            .addContainerGap())
      );
   }// </editor-fold>                        


   // Variables declaration - do not modify                     
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JButton jButton5;
   // End of variables declaration                   
}

相同的布局,有合理的变量名称,我添加了一个约束,使按钮的宽度相同,我认为看起来好一点。

package quicktest;

public class ChaosLayout extends javax.swing.JPanel {

   /**
    * Creates new form ChaosLayout
    */
   public ChaosLayout() {
      initComponents();
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings( "unchecked" )
   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
   private void initComponents() {

      launch = new javax.swing.JButton();
      chaos = new javax.swing.JButton();
      trials = new javax.swing.JButton();
      map = new javax.swing.JButton();
      start = new javax.swing.JButton();

      launch.setText("Launch");

      chaos.setText("Chaos");

      trials.setText("Trials");

      map.setText("Map");

      start.setText("Start");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(55, 55, 55)
            .addComponent(trials)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
               .addComponent(launch)
               .addComponent(chaos)
               .addComponent(start))
            .addGap(18, 18, 18)
            .addComponent(map)
            .addContainerGap(99, Short.MAX_VALUE))
      );

      layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {chaos, launch, map, start, trials});

      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(launch)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(chaos)
               .addComponent(trials)
               .addComponent(map))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
            .addComponent(start)
            .addContainerGap())
      );
   }// </editor-fold>                        


   // Variables declaration - do not modify                     
   private javax.swing.JButton chaos;
   private javax.swing.JButton launch;
   private javax.swing.JButton map;
   private javax.swing.JButton start;
   private javax.swing.JButton trials;
   // End of variables declaration                   
}

以上是关于Java GroupLayout定位的主要内容,如果未能解决你的问题,请参考以下文章

java swing GroupLayout - 如何交换组件的位置

GroupLayout:值得学习吗?

在 Java 中重写 GroupLayout 构造函数

学习 Java 布局 groupLayout

(转)Java 的swing.GroupLayout布局管理器的使用方法和实例

显示图标