为什么在添加两个面板时拆分面板分隔线不会移动?

Why split pane divider does not move when two panels are added in it?

我写了一个简单的程序。我在 JFrame 的中心使用 JSplitPane,我想在它的两侧显示两张图片,所以我使用两个 JLabel 组件并将它们放在 JPanel 中。但是当我 运行 代码时,divder 不会移动。

public class A extends JFrame
{
private static final long serialVersionUID = 1L;

public A() throws IOException
{
    getContentPane().setLayout(new BorderLayout(0, 0));

    JSplitPane splitPane = new JSplitPane();
    getContentPane().add(splitPane, BorderLayout.CENTER);

    JPanel left = new JPanel();
    BufferedImage myPicture = ImageIO.read(new File("a.jpg"));
    JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
    left.add(lblNewLabel);

    splitPane.setLeftComponent(left);

    JPanel right = new JPanel();
    JLabel label = new JLabel(new ImageIcon(myPicture));
    right.add(label);

    splitPane.setRightComponent(right);
}

public static void main(String[] args) throws IOException
{
    A a = new A();
    a.setSize(700, 700);
    a.show();
}
}

与jlabel上的图片有关

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class A extends JFrame {

    private static final long serialVersionUID = 1L;

    public A() {
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        JPanel left = new JPanel();
        BufferedImage myPicture = null;
        try {
            myPicture = ImageIO.read(new File("a.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
        }
        JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
        left.add(lblNewLabel);

        splitPane.setLeftComponent(left);

        JPanel right = new JPanel();
        JLabel label = new JLabel(new ImageIcon(myPicture));
        right.add(label);

        splitPane.setRightComponent(right);

        add(splitPane, BorderLayout.CENTER);
        setSize(700, 700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        splitPane.setDividerLocation(0.5);

    }

    public static void main(String[] args) {
        new A();
    }
}

此代码启用 分隔线移动而不管照片的大小 :

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;

public class Test extends JFrame {

    public Test () throws IOException {
        getContentPane().setLayout(new BorderLayout(0, 0));

        JSplitPane splitPane = new JSplitPane();
        getContentPane().add(splitPane, BorderLayout.CENTER);
        splitPane.setOneTouchExpandable(true);
        splitPane.setContinuousLayout(true);
        splitPane.resetToPreferredSizes();

        BufferedImage myPicture = ImageIO
                .read(new URL("http://freecodebank.com/wp-content/uploads/2016/07/joinus-java.png"));
        JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
        lblNewLabel.setMinimumSize(new Dimension(100, 80));
        splitPane.setLeftComponent(lblNewLabel);

        JLabel label = new JLabel(new ImageIcon(myPicture));
        label.setMinimumSize(new Dimension(100, 80));
        splitPane.setRightComponent(label);
    }

    public static void main(String[] args) throws IOException {
        Test a = new Test();
        a.setPreferredSize(new Dimension(1400, 900));
        a.pack();
        a.setVisible(true);
    }
}

祝你好运。