GridBagLayout在与GridLayout类似的行和列中布置的单元格网格中布置组件。
由GridBagLayout创建的网格的所有单元格不必具有相同的大小。
组件不必准确放置在一个单元格。
组件可以水平和垂直跨越多个单元格。
我们可以指定单元格内的组件应如何对齐。
GridBagLayout和GridBagConstraints类一起使用。这两个类都在java.awt包中。
GridBagLayout类定义了一个GridBagLayout布局管理器。GridBagConstraints类为GridBagLayout中的组件定义约束。
以下代码创建GridBagLayout类的对象并将其设置为JPanel的布局管理器:
JPanel panel = new JPanel(); GridBagLayout gridBagLayout = new GridBagLayout(); panel.setLayout(gridBagLayout);
下面的代码展示了如何使用一个简单的GridBagLayout。
import java.awt.Container; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { String title = "GridBagLayout"; JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridBagLayout()); for (int i = 1; i <= 9; i++) { contentPane.add(new JButton("Button " + i)); } frame.pack(); frame.setVisible(true); } }
这是我们用来创建和使用GridBagConstraints的正常过程。
这是我们用来创建和使用GridBagConstraints的正常过程。...
GridBagConstraints gbc = new GridBagConstraints();
然后,在约束对象中设置gridx和gridy属性
gbc.gridx = 0; gbc.gridy = 0;
之后,添加一个JButton并传递约束对象作为 add()方法的第二个参数。
container.add(new JButton("B1"), gbc);
我们可以将gridx属性更改为1. gridy属性保持为0,如先前设置的。
gbc.gridx = 1;
然后,向容器中添加另一个JButton
container.add(new JButton("B2"), gbc);
以下代码演示了如何为组件设置gridx和gridy值。
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; //from ww w .jav a 2 s. co m import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { String title = "GridBagLayout"; JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); for (int y = 0; y < 3; y++) { for (int x = 0; x < 4; x++) { gbc.gridx = x; gbc.gridy = y; String text = "Button (" + x + ", " + y + ")"; contentPane.add(new JButton(text), gbc); } } frame.pack(); frame.setVisible(true); } }
提示主键生成策略必须使用INPUT支持父类定义@KeySequence子类继承使用支持主键类型指定(3.3.0开始自动识别主键类型)内置...
类型处理器,用于JavaType与JdbcType之间的转换,用于PreparedStatement设置参数值和从ResultSet或CallableStatement中取...
TenantLineInnerInterceptor示例工程:mybatis-plus-sample-tenant属性介绍属性名类型默认值描述tenantLineHandlerTenantLin...
鉴别器discriminator javaType="int" column="draft"case value="1" resultType="DraftPost"//discriminator有时候,一个数据库...