关于如何在JSplitPane分隔面板中显示图片

也可直接将图片加载到JSplitPane的分割窗口中

package hecheng;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class t1 extends JFrame{
JPanel jp1,jp2;//声明两个JPanel面板容器

    t1(){
jp1 = new JPanel();//实例
jp2 = new JPanel();//实例
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jp1, jp2);//将面板容器添加到分割面板中
add(jsp);//将分割面板添加到JFrame窗体中
setTitle("拆分窗口");//实例窗体名称
setBounds(300, 500, 1500, 1500);//设置窗体的位置、大小
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗体的关闭方式
setVisible(true);//显示窗体
jsp.setDividerLocation(0.5);// 在1/2处进行拆分

showphoto show1=new showphoto();//声明并实例showphoto类
show1.showphoto_left();//调用函数
show1.showphoto_right();
     }
     class showphoto{
    private JPanel main;//声明JPanel面板容器
    private Icon icon;//声明图标
    private JLabel l;//声明标签
    public void showphoto_left(){
    main = new JPanel();
    l=new JLabel();
    icon=new ImageIcon("E:\\DWTPLKJA04MO1{Y%LS)K2)R.png");//将图标用图片文件实例化
    //在此直接创建对象,注意目录之间的分隔符是双斜线
    l.setIcon(icon);
    l.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
    main.add(l,new Integer(Integer.MIN_VALUE));//将标签添加到main这个面板容器中
    //this.add(main);
    jp1.add(main);//将main面板添加到jp1面板中
    pack();//(调整窗口的大小,使其适应组件的大小和布局)
    }
    public void showphoto_right(){
    main = new JPanel();
    l=new JLabel();
    icon=new ImageIcon("E:\\DWTPLKJA04MO1{Y%LS)K2)R.png");
    //在此直接创建对象,注意目录之间的分隔符是双斜线
    l.setIcon(icon);
    l.setBounds(10, 10, icon.getIconWidth(),icon.getIconHeight());
    main.add(l,new Integer(Integer.MIN_VALUE));
    //this.add(main);
    jp2.add(main);
    pack();
    }
     }
public static void main(String[] args) {
new t1();
}

}


也可直接将图片加载到JSplitPane的分割窗口中,showphoto类修改如下:

class showphoto{
    //private JPanel main;//声明JPanel面板容器
    private Icon icon;//声明图标
    private JLabel l;//声明标签
    public void showphoto_left(){
    l=new JLabel();
    icon=new ImageIcon("E:\\DWTPLKJA04MO1{Y%LS)K2)R.png");//将图标用图片文件实例化
    //在此直接创建对象,注意目录之间的分隔符是双斜线
    l.setIcon(icon);
    l.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
    jp1.add(l,new Integer(Integer.MIN_VALUE));//将标签添加到main这个面板容器中
    pack();//(调整窗口的大小,使其适应组件的大小和布局)
    }
    public void showphoto_right(){
    //main = new JPanel();
    l=new JLabel();
    icon=new ImageIcon("E:\\DWTPLKJA04MO1{Y%LS)K2)R.png");
    //在此直接创建对象,注意目录之间的分隔符是双斜线
    l.setIcon(icon);
    l.setBounds(10, 10, icon.getIconWidth(),icon.getIconHeight());

    jp2.add(l,new Integer(Integer.MIN_VALUE));

                        pack();   

    }      }