我写了一个简单的程序。我在JSplitPane
的中心使用了JFrame
,我希望在其两侧显示两张图片,因此我使用了两个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();
}
}
答案 0 :(得分:1)
此代码可以 分割器移动,无论照片大小 :
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);
}
}
祝你好运。
答案 1 :(得分:0)
它与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();
}
}