Swing的GridLayout示例
说明
GridLayout类将组件排列在一个矩形网格中。
代码示例
package com.yiidian;
import javax.swing.*;
import java.awt.*;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("一点教程网:Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(560, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(final JFrame frame){
JPanel panel = new JPanel();
GridLayout layout = new GridLayout(0,3);
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
panel.add(new JButton("Button 4"));
panel.add(new JButton("Button 5"));
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
执行效果如下:
热门文章
优秀文章