Java Swing GroupLayout
2018-01-09 19:23 更新
Java Swing教程 - Java Swing GroupLayout
GroupLayout在javax.swing包中。
GroupLayout使用组的概念。 一个组由元素组成。 组的元素可以是组件,间隙或另一组。
间隙是两个组件之间的不可见区域。
有两种类型的组:
- Sequential group
- Parallel group
当组中的元素被依次串联放置时,它被称为顺序组。
当组中的元素并行放置时,它被称为并行组。
GroupLayout类包含三个内部类:Group,SequentialGroup和ParallelGroup。
Group是一个抽象类,其他两个类继承自Group类。
我们可以使用GroupLayout类的工厂方法来创建它们的对象。
我们可以使用GroupLayout类的工厂方法来创建它们的对象。...
以下代码演示了如何使用GroupLayout在JFrame中并排显示两个按钮。
import java.awt.Container; // w w w . ja v a 2s. c o m import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("GroupLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); GroupLayout groupLayout = new GroupLayout(contentPane); contentPane.setLayout(groupLayout); JLabel label = new JLabel("Label"); JButton b2 = new JButton("Second Button"); groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() .addComponent(label).addComponent(b2)); groupLayout.setVerticalGroup(groupLayout .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label) .addComponent(b2)); frame.pack(); frame.setVisible(true); } }
间隙
以下代码演示了如何使用GroupLayout在JFrame中并排显示两个按钮。...
import java.awt.Container; //w ww .j a v a 2 s.c o m import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("GroupLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); GroupLayout groupLayout = new GroupLayout(contentPane); contentPane.setLayout(groupLayout); JLabel label = new JLabel("Label"); JButton b2 = new JButton("Second Button"); groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() .addComponent(label).addGap(5, 10, 50).addComponent(b2)); groupLayout.setVerticalGroup(groupLayout .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label) .addComponent(b2)); frame.pack(); frame.setVisible(true); } }
例子
以下代码在b1和b2之间添加了RELATED首选间隙:
import java.awt.Container; //w w w .java 2s. c om import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.LayoutStyle; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("GroupLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); GroupLayout groupLayout = new GroupLayout(contentPane); contentPane.setLayout(groupLayout); JLabel label = new JLabel("Label"); JButton b2 = new JButton("Second Button"); groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() .addComponent(label).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(b2)); groupLayout.setVerticalGroup(groupLayout .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label) .addComponent(b2)); frame.pack(); frame.setVisible(true); } }
实施例2
import static javax.swing.GroupLayout.Alignment.BASELINE; import static javax.swing.GroupLayout.Alignment.LEADING; import static javax.swing.GroupLayout.Alignment.TRAILING; // ww w . j av a 2 s . c o m import java.awt.Container; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("GroupLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); GroupLayout groupLayout = new GroupLayout(contentPane); groupLayout.setAutoCreateGaps(true); groupLayout.setAutoCreateContainerGaps(true); contentPane.setLayout(groupLayout); JButton b1 = new JButton("Button 1"); JButton b2 = new JButton("Button Second"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); groupLayout.setHorizontalGroup(groupLayout .createSequentialGroup() .addGroup( groupLayout.createParallelGroup(LEADING).addComponent(b1) .addComponent(b3)) .addGroup( groupLayout.createParallelGroup(TRAILING).addComponent(b2) .addComponent(b4))); groupLayout.setVerticalGroup(groupLayout .createSequentialGroup() .addGroup( groupLayout.createParallelGroup(BASELINE).addComponent(b1) .addComponent(b2)) .addGroup( groupLayout.createParallelGroup(BASELINE).addComponent(b3) .addComponent(b4))); frame.pack(); frame.setVisible(true); } }
以上内容是否对您有帮助:
更多建议: