Swing的GroupLayout示例
说明
GroupLayout 类分级组,才能在容器内放置它们的组件。
代码示例
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();
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(btn2)
.addComponent(btn3))));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addComponent(btn2)
.addComponent(btn3));
panel.setLayout(layout);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
执行效果如下:
热门文章
优秀文章