控制台程序。
网格布局管理器可以在容器的矩形网格中布局组件。
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder; public class TryGridLayout { 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); GridLayout grid = new GridLayout(3,4,30,20); // Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(grid); // Set the container layout mgr
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border // Now add ten Button components
JButton button = null; // Stores a button
for(int i = 1 ; i <= 10 ; ++i) {
content.add(button = new JButton(" Press " + i)); // Add a Button
button.setBorder(edge); // Set the border
}
aWindow.pack(); // Size for components
aWindow.setVisible(true); // Display the window
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}
应用程序窗口显示的最初大小由传送给JFrame对象的setBounds()方法的值来决定。如果希望窗口的大小能适合包含的组件,可以调用JFrame对象的pack()方法。在调用setVisible()方法之前,添加下面的代码:
aWindow.pack();