Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)

时间:2022-10-12 16:34:10

控制台程序。

java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂。基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和宽度。

GridBagLayout中的每个组件都有自己的约束,在把组件添加到容器中之前,这些约束在GridBagConstrains类型的对象中指定,这种对象将关联到每个组件上。每个组件的位置、相对大小以及在网格中占据的区域都由与之关联的GridBagConstrains对象决定。

 import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border; public class TryGridBagLayout { public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size
aWindow.setSize(wndSize.width/2, wndSize.height/2); // Set window size
aWindow.setLocationRelativeTo(null); // Center window
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout gridbag = new GridBagLayout(); // Create a layout manager
GridBagConstraints constraints = new GridBagConstraints();
aWindow.getContentPane().setLayout(gridbag); // Set the container layout mgr // Set constraints and add first button
constraints.weightx = constraints.weighty = 10.0;
constraints.fill = GridBagConstraints.BOTH; // Fill the space
addButton(" Press ", constraints, gridbag, aWindow); // Add the button // Set constraints and add second button
constraints.gridwidth = GridBagConstraints.REMAINDER; // Rest of the row
addButton("GO", constraints, gridbag, aWindow); // Create and add button aWindow.setVisible(true); // Display the window
} static void addButton(String label,
GridBagConstraints constraints,
GridBagLayout layout,
JFrame window) {
// Create a Border object using a BorderFactory method
Border edge = BorderFactory.createRaisedBevelBorder(); JButton button = new JButton(label); // Create a button
button.setBorder(edge); // Add its border
layout.setConstraints(button, constraints); // Set the constraints
window.getContentPane().add(button); // Add button to content pane
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}

当调用setConstrains()方法,使GridBagConstrains对象关联按钮对象时,会为传送为参数的constrains对象传送副本,并把对副本的引用存储在布局中,而不是存储对所创建对象的引用。这样就可以修改创建的constrains对象,将它用于第二个按钮,而不会影响用于第一个按钮的约束。

可以将第二个按钮的宽度设置为第一个的一半:

 // Set constraints and add second button
constraints.weightx = 5.0; // Weight half of first
constraints.insets = new java.awt.Insets(10, 30, 10, 20); // Left 30 & right 20
constraints.gridwidth = GridBagConstraints.RELATIVE; // Rest of the row
addButton("GO", constraints, gridbag, aWindow); // Create and add button

现在,第二个按钮在x方向上占据了1/3的空间-对应于x方向上总可用空间的5/(5+10)。
假定要添加第三个按钮,宽度与Press按钮相同并且放在Press按钮的下面。可以在第二个按钮的下面添加如下代码:

 // Set constraints and add third button
constraints.insets = new java.awt.Insets(0,0,0,0); // No insets
constraints.gridx = 0; // Begin new row
constraints.gridwidth = 1; // Width as "Press"
addButton("Push", constraints, gridbag, aWindow); // Add button to content pane

把gridx约束重置为0,从而把按钮放在下一行的开头。

如果GO按钮的高度是Press和Push按钮的高度之和,效果会更好。为此,需要使GO按钮的高度是另外两个按钮的高度的两倍。

  // Set constraints and add second button
constraints.weightx = 5.0; // Weight half of first
constraints.insets = new java.awt.Insets(10, 30, 10, 20); // Left 30 & right 20
constraints.gridwidth = GridBagConstraints.RELATIVE; // Rest of the row
constraints.gridheight = 2; // Height 2x "Press"
addButton("GO", constraints, gridbag, aWindow); // Create and add button // Set constraints and add third button
constraints.insets = new java.awt.Insets(0,0,0,0); // No insets
constraints.gridx = 0; // Begin new row
constraints.gridwidth = 1; // Width as "Press"
constraints.gridheight = 1; // Height as "Press"
addButton("Push", constraints, gridbag, aWindow); // Add button to content pane