import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.GroupLayout; public class MyFrame extends JFrame { public MyFrame() { initComponents(); } private void initComponents() { JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); JButton button1 = new JButton("Button 1"); JButton button2 = new JButton("Button 2"); JButton button3 = new JButton("Button 3"); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout.createSequentialGroup() .addComponent(button1) .addComponent(button2) .addComponent(button3) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(button1) .addComponent(button2) .addComponent(button3) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, button1, button2, button3); add(panel); pack(); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.GroupLayout; public class MyFrame extends JFrame { public MyFrame() { initComponents(); } private void initComponents() { JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); JLabel label1 = new JLabel("Label 1:"); JLabel label2 = new JLabel("Label 2:"); JTextField field1 = new JTextField(10); JTextField field2 = new JTextField(10); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(label1) .addComponent(label2)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(field1) .addComponent(field2)) ); layout.setVerticalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(label1) .addComponent(field1)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(label2) .addComponent(field2)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, label1, label2); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, field1, field2); add(panel); pack(); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }