JSplitPane分割面板调整

wen java案例 1

我来为您详细介绍Java Swing中的JSplitPane分割面板调整功能。

JSplitPane分割面板调整

JSplitPane 基本使用

JSplitPane 是 Swing 中用于创建可调整大小分割面板的组件。

基本创建

import javax.swing.*;
import java.awt.*;
public class SplitPaneExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JSplitPane示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        // 创建两个面板
        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.LIGHT_GRAY);
        leftPanel.add(new JLabel("左侧面板"));
        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(Color.WHITE);
        rightPanel.add(new JLabel("右侧面板"));
        // 创建水平分割面板
        JSplitPane splitPane = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftPanel,
            rightPanel
        );
        // 设置面板
        frame.add(splitPane);
        frame.setVisible(true);
    }
}

分割方向

// 水平分割(左右)
JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComp, rightComp);
// 垂直分割(上下)
JSplitPane vertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComp, bottomComp);

常用属性设置

// 创建分割面板
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
// 设置初始分割位置(像素)
splitPane.setDividerLocation(200);
// 设置分割位置比例(0.0-1.0)
splitPane.setDividerLocation(0.3);  // 左侧占30%
// 设置分割线宽度
splitPane.setDividerSize(10);
// 设置最小分割位置
splitPane.setMinimumDividerLocation(50);
// 设置最大分割位置
splitPane.setMaximumDividerLocation(500);
// 允许/禁止连续布局(拖动时实时更新)
splitPane.setContinuousLayout(true);
// 设置分割线展开/折叠
splitPane.setOneTouchExpandable(true);
// 设置分割方向
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);

完整示例:可调整的面板

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class AdjustableSplitPane extends JFrame {
    private JSplitPane splitPane;
    private JLabel statusLabel;
    public AdjustableSplitPane() {
        setTitle("可调整分割面板");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 500);
        initComponents();
        setVisible(true);
    }
    private void initComponents() {
        // 左侧:文本区域
        JTextArea leftText = new JTextArea();
        leftText.setBorder(BorderFactory.createTitledBorder("文本编辑器"));
        JScrollPane leftScroll = new JScrollPane(leftText);
        // 右侧:按钮面板
        JPanel rightPanel = new JPanel(new GridBagLayout());
        rightPanel.setBorder(BorderFactory.createTitledBorder("功能面板"));
        rightPanel.setBackground(Color.WHITE);
        // 添加一些按钮
        JButton btn1 = new JButton("功能1");
        JButton btn2 = new JButton("功能2");
        JButton btn3 = new JButton("功能3");
        JButton btn4 = new JButton("功能4");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridx = 0; gbc.gridy = 0;
        rightPanel.add(btn1, gbc);
        gbc.gridy = 1;
        rightPanel.add(btn2, gbc);
        gbc.gridy = 2;
        rightPanel.add(btn3, gbc);
        gbc.gridy = 3;
        rightPanel.add(btn4, gbc);
        // 创建分割面板
        splitPane = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftScroll,
            rightPanel
        );
        // 配置分割面板
        splitPane.setDividerLocation(0.5);  // 初始位置:50%
        splitPane.setDividerSize(8);
        splitPane.setContinuousLayout(true);
        splitPane.setOneTouchExpandable(true);
        // 添加分割线移动监听器
        splitPane.addPropertyChangeListener(
            JSplitPane.DIVIDER_LOCATION_PROPERTY,
            evt -> {
                int location = (int) evt.getNewValue();
                double ratio = (double) location / splitPane.getWidth();
                updateStatus(ratio);
            }
        );
        // 状态面板
        JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        statusLabel = new JLabel("分割位置: 50%");
        statusPanel.add(statusLabel);
        // 主布局
        setLayout(new BorderLayout());
        add(splitPane, BorderLayout.CENTER);
        add(statusPanel, BorderLayout.SOUTH);
    }
    private void updateStatus(double ratio) {
        int percentage = (int) (ratio * 100);
        statusLabel.setText("分割位置: " + percentage + "%");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(AdjustableSplitPane::new);
    }
}

嵌套分割面板

import javax.swing.*;
import java.awt.*;
public class NestedSplitPane extends JFrame {
    public NestedSplitPane() {
        setTitle("嵌套分割面板");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        // 创建三个面板
        JPanel topPanel = createPanel("顶部面板", Color.LIGHT_GRAY);
        JPanel leftPanel = createPanel("左侧面板", Color.CYAN);
        JPanel rightPanel = createPanel("右侧面板", Color.ORANGE);
        // 内部水平分割(左右)
        JSplitPane innerSplit = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftPanel,
            rightPanel
        );
        innerSplit.setDividerLocation(0.5);
        // 外部垂直分割(上下)
        JSplitPane outerSplit = new JSplitPane(
            JSplitPane.VERTICAL_SPLIT,
            topPanel,
            innerSplit
        );
        outerSplit.setDividerLocation(0.3);
        outerSplit.setOneTouchExpandable(true);
        add(outerSplit);
        setVisible(true);
    }
    private JPanel createPanel(String name, Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.setBorder(BorderFactory.createTitledBorder(name));
        panel.add(new JLabel(name));
        return panel;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(NestedSplitPane::new);
    }
}

自定义分割线

import javax.swing.*;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import java.awt.*;
public class CustomDividerSplitPane extends JFrame {
    public CustomDividerSplitPane() {
        setTitle("自定义分割线");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        JPanel leftPanel = createPanel("左侧", Color.CYAN);
        JPanel rightPanel = createPanel("右侧", Color.PINK);
        JSplitPane splitPane = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftPanel,
            rightPanel
        );
        // 自定义分割线
        splitPane.setUI(new BasicSplitPaneUI() {
            @Override
            public BasicSplitPaneDivider createDefaultDivider() {
                return new BasicSplitPaneDivider(this) {
                    @Override
                    public void paint(Graphics g) {
                        // 绘制自定义分割线
                        g.setColor(Color.BLUE);
                        g.fillRect(0, 0, getWidth(), getHeight());
                        // 绘制箭头或装饰
                        g.setColor(Color.WHITE);
                        g.drawLine(getWidth()/2, 20, getWidth()/2, getHeight()-20);
                    }
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(12, 1);
                    }
                };
            }
        });
        splitPane.setDividerSize(12);
        splitPane.setDividerLocation(0.5);
        add(splitPane);
        setVisible(true);
    }
    private JPanel createPanel(String name, Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.add(new JLabel(name));
        return panel;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(CustomDividerSplitPane::new);
    }
}

边界和限制

// 设置边界(防止面板被挤压到看不见)
splitPane.setMinimumSize(new Dimension(400, 300));
// 设置组件最小尺寸
leftComponent.setMinimumSize(new Dimension(100, 100));
rightComponent.setMinimumSize(new Dimension(100, 100));
// 重置分割位置
splitPane.resetToPreferredSizes();
// 获取当前分割位置
int currentLocation = splitPane.getDividerLocation();
// 锁定分割位置(禁止调整)
splitPane.setEnabled(false);
  1. 分割方向:水平(左右)和垂直(上下)
  2. 初始化设置:位置、比例、大小
  3. 交互控制:展开/折叠、连续布局
  4. 事件监听:位置变化、状态更新
  5. 自定义样式:分割线颜色、样式
  6. 嵌套使用:复杂布局设计

JSplitPane 非常适合创建可调整大小的分区布局,常见于IDE、编辑器和各种工具软件中。

抱歉,评论功能暂时关闭!