SplitPane
类允许在两个面板之间进行可移动分割。
示例
package com.yiibai.swingdemo;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Swing拆分水平分割的窗格示例");
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 buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("确定"));
buttonPanel.add(new JButton("取消"));
JPanel textBoxPanel = new JPanel(new FlowLayout());
textBoxPanel.add(new JLabel("名称:"));
textBoxPanel.add(new JTextField(20));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,buttonPanel, textBoxPanel);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
frame.getContentPane().add(splitPane, BorderLayout.CENTER);
}
}
执行上面示例代码,得到以下结果: