展开全部
看代码:import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
public class Main {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("JFrame with JSplitPane");
JLabel label1 = new JLabel("label1");
label1.setBackground(Color.BLUE);
label1.setVerticalAlignment(SwingConstants.TOP);//设置标签内32313133353236313431303231363533e58685e5aeb931333337623462容处于顶端
label1.setHorizontalAlignment(SwingConstants.CENTER);//设置内容居中
// 让组件变成不透明,这样在JLabel上所设置的颜色才能显示出来
label1.setOpaque(true);
JLabel label2 = new JLabel("label2");
label2.setBackground(Color.LIGHT_GRAY);
label2.setVerticalAlignment(SwingConstants.TOP);
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setOpaque(true);
JLabel label3 = new JLabel("label3");
label3.setBackground(Color.RED);
label3.setVerticalAlignment(SwingConstants.TOP);
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setOpaque(true);
//加入 label1,label2 到 splitPane1 中,并设置 splitPane1 为水平分割
JSplitPane splitPane1 = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT, false, label1, label2);
//设置 splitPane1 的分隔线位置,0.4 是相对于 splitPane1 的大小而定
splitPane1.setDividerLocation(0.4);
splitPane1.setResizeWeight(0.4);
JSplitPane splitPane2 = new JSplitPane(
JSplitPane.VERTICAL_SPLIT, false, splitPane1, label3);
splitPane2.setDividerLocation(0.5);
splitPane2.setResizeWeight(0.5);
frame.add(splitPane2);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null); // 设置居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行结果: