Java GroupLayout定位
Java GroupLayout positioning
我在尝试设置 GroupLayout 时遇到了麻烦。我正在尝试从 oracle 的网站上删除教程,但我终究无法弄清楚。下面是我当前的布局设计和代码,不确定我做错了什么。
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))
);
我的目标是让它看起来像下面的图片...
我启动了 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
}
我在尝试设置 GroupLayout 时遇到了麻烦。我正在尝试从 oracle 的网站上删除教程,但我终究无法弄清楚。下面是我当前的布局设计和代码,不确定我做错了什么。
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))
);
我的目标是让它看起来像下面的图片...
我启动了 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
}