Java 为什么拆分窗格分隔器在添加两个面板时不移动?
我写了一个简单的程序。我在Java 为什么拆分窗格分隔器在添加两个面板时不移动?,java,swing,jsplitpane,Java,Swing,Jsplitpane,我写了一个简单的程序。我在JFrame的中心使用了JSplitPane,我想在它的两面显示两幅图片,所以我使用了两个JLabel组件,并将它们放在JPanel中。但是当我运行代码时,divder不会移动 public class A extends JFrame { private static final long serialVersionUID = 1L; public A() throws IOException { getContentPane().setLayout(new
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);
}
}
祝你好运。谢谢你的回复,但你的程序和我的一样。我不能移动隔板,我看见了。谢谢你的评论。哇,真的很管用,谢谢。但是你能告诉我吗why@Hosseinmp76是的,因为图片大小是固定的,JSplitpane无法更改它,所以我添加了标签的最小大小。当你想移动拆分时,不看标签大小。(小哈克)。请不要忘记将答案设置为正确。“所以我添加了标签的最小大小…(小哈克)。”这是一个巨大的黑客,它会回来咬程序员的后腿。请看(是的。)注意OP似乎试图使用背景图像,这表明他们的整个方法是错误的。“我真的想在背景中放一张图片”是什么?镜框?拆分窗格的每一侧?@andrewth在拆分窗格的左侧显示一张图片,另一侧显示一张JPanel