Java 中的 JSplitPane 有问题?

Having problems with JSplitPane in Java?

我刚开始在 Java 学习摇摆。我在 swing 库中遇到 JSplitPane 问题。我制作了一个有四个 class 的应用程序 Frame(), TablePanel(), TextPanel(), FormPanel() 框架是主要的 class 扩展 JFrame.The 下图更清楚地描述了这些 classes。

现在我的问题是我希望拆分器位于 TablePanel class 和 TextPanel class 之间。我想知道是否有一种方法可以在 TablePanel 中实例化 TextPanel 并在 Table 面板和 TextPanel 之间设置拆分器,例如:

以下仅为伪代码;

TablePanel() {
TextPanel textPanel = new TextPanel();
setLayout(new BorderLayout());
JSplitPane spliter = new JSplitPane(vertical, textPanel, this);

add(spliter);
}

如果我错了请指出。请提出更好的方法来纠正我的错误。

如有帮助,将不胜感激。提前致谢!

我建议您学习以下内容以正确理解 JSplitPane 的工作原理,并获得一些演示示例:

Oracle documentation on the use of JSplitPane

使用 JSplitPane.setDividerLocation(double),但请注意 Java 文档:

Sets the divider location as a percentage of the JSplitPane's size.

This method is implemented in terms of setDividerLocation(int). This method immediately changes the size of the split pane based on its current size. If the split pane is not correctly realized and on screen, this method will have no effect (new divider location will become (current size * proportionalLocation) which is 0).

所以必须在GUI可见后调用

为了在 构建 GUI 的过程中实现这一点,我将使用单次 Swing Timer 将分隔线位置的设置延迟大约半秒,然后在创建组件结束时启动计时器。

实施

这是一个简单的 MCVE 实现。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SplitPaneDivider {

    private JComponent ui = null;

    SplitPaneDivider() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        final JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                new ColoredPanel(Color.GREEN),
                new ColoredPanel(Color.YELLOW));
        ui.add(sp);
        ActionListener dividerListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sp.setDividerLocation(.7d);
            }
        };
        Timer timer = new Timer(500, dividerListener);
        timer.setRepeats(false);
        timer.start();
    }

    class ColoredPanel extends JPanel {

        ColoredPanel(Color color) {
            setBackground(color);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 100);
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                SplitPaneDivider o = new SplitPaneDivider();

                JFrame f = new JFrame("Split Pane Divider");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}