java的jpanel上添加背景图片后eclipse不显示背景图片,但运行后却看得到

肯定不是jpanel大小的问题,拉过再大也不显示图片
2024-12-19 19:49:24
推荐回答(1个)
回答1:

 p4=new JPanel(new BorderLayout());
    p2=new JPanel(new CardLayout());
    //处理p2,p3,p4

    p2_lab1=new JLabel(new ImageIcon("image/21.jpg"));
    p2_lab2=new JLabel(new ImageIcon("image/22.jpg"));
    //把以上俩加入到P2中
    p2.add(p2_lab1,"0");
    p2.add(p2_lab2,"1");
    this.cardp3=new CardLayout();
    p3=new JPanel(this.cardp3);
    //先给P3加入一个主界面的卡片
    Image zhu_image=null;
    try {
        zhu_image = ImageIO.read(new File("image/11.jpg"));

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ImagePanel ip=new ImagePanel(zhu_image);

    p3.add(ip,"0");
    //对P3添加几个JLabel
    JLabel rs=new JLabel(new ImageIcon("image/renshi.jpg"));
    p3.add(rs,"1");
    JLabel drgl=new JLabel(new ImageIcon("image/dengru.jpg"));
    p3.add(drgl,"2");

    //把p2 p3装入P4中
    p4.add(p2,"West");
    p4.add(p3,"Center");

    //做一个拆分窗口分别存放P1 P4
    jsp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,p1,p4);
    //指定左边的面板占多大像素
    jsp1.setDividerLocation(120);
    //把边界线设为没有
    jsp1.setDividerSize(0);