3

我正在尝试在另一个 JPanel 中设置 Jpanel 的 Boxlayout。但我不知道如何解决这个错误

    Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    at javax.swing.BoxLayout.checkContainer(BoxLayout.java:464)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:248)
at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:281)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:410)
at chatmsg1.ProfilePanel.<init>(ProfilePanel.java:63)
at chatmsg1.ProfilePanel.main(ProfilePanel.java:108)
    Java Result: 1

这是代码!任何帮助将不胜感激 !!

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.management.ImmutableDescriptor;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

 class ProfilePanel extends JPanel{
 JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10;
 ImagePanel ip= new ImagePanel();
 JPanel jp1= new JPanel(new BorderLayout());
 JPanel labelpanel;
 static int textwidth=100; 
 static String uname, uage, usex, udept,   uheight="jjhkjhiuhiuhiuhiuhihihuihiuhiuhuhiuhiuhihihiuhhihhuhih",ubatch, ucontact, uemail;
public ProfilePanel() {
    this.labelpanel = new JPanel(new BoxLayout(labelpanel, BoxLayout.PAGE_AXIS));


    String html1 = "<html><body style='width: ";
    String html2 = "px'>";

    labelpanel.setSize(400, 400);
    jl1= new JLabel(""+html1+""+textwidth+html2+"<h1>"+uname+"</h1>");
//jl1.setSize(jl1.getPreferredSize());

    jl2= new JLabel(""+html1+""+textwidth+html2+uage);
//jl2.setSize(jl2.getPreferredSize());

    jl3= new JLabel(""+html1+""+textwidth+html2+usex);
//jl3.setSize(jl3.getPreferredSize());

    jl4= new JLabel(""+html1+""+textwidth+html2+udept);
 // jl4.setSize(jl4.getPreferredSize());

    jl5= new JLabel(""+html1+""+textwidth+html2+uheight);
//jl5.setSize(jl5.getPreferredSize());

    jl6= new JLabel(""+html1+""+textwidth+html2+ubatch);
//jl6.setSize(jl6.getPreferredSize());

    jl7= new JLabel(""+html1+""+textwidth+html2+ucontact);
//jl7.setSize(jl7.getPreferredSize());

  jl8= new JLabel(""+html1+""+textwidth+html2+uemail);
//jl8.setSize(jl8.getPreferredSize());

/*
labelpanel.add(jl1,BorderLayout.CENTER);
labelpanel.add(jl2,BorderLayout.SOUTH);
labelpanel.add(jl3,BorderLayout.SOUTH);
labelpanel.add(jl4,BorderLayout.SOUTH);
labelpanel.add(jl5,BorderLayout.SOUTH);
labelpanel.add(jl6,BorderLayout.SOUTH);
labelpanel.add(jl7,BorderLayout.SOUTH);
labelpanel.add(jl8,BorderLayout.SOUTH);

*/
labelpanel.add(jl1);
labelpanel.add(jl2);
labelpanel.add(jl3);
labelpanel.add(jl4);
labelpanel.add(jl5);
labelpanel.add(jl6);
labelpanel.add(jl7);
labelpanel.add(jl8);


jl1.setAlignmentX(0);
jl2.setAlignmentX(0);
jl3.setAlignmentX(0);
jl4.setAlignmentX(0);
jl5.setAlignmentX(0);
jl6.setAlignmentX(0);
jl7.setAlignmentX(0);


ip.setImage("icons/1.jpg");
ip.setSize(200, 200);
//jl1.setSize(jl1.getPreferredSize());
jp1.add(ip, BorderLayout.CENTER);
jp1.setPreferredSize(jp1.getPreferredSize());
jp1.setBounds(20, 20, ip.getwidth(), ip.getheight());

//add(jp1,BorderLayout.CENTER );
//add(jl1,BorderLayout.WEST);
add(jp1);
add(labelpanel);
labelpanel.setLocation(jp1.getX()+jp1.getWidth()+20,0 );
labelpanel.setBounds(jp1.getX()+jp1.getWidth()+20,0 ,300, 400);
setLayout(null);
setPreferredSize(getPreferredSize());
setLocation(0, 0);
//setBounds(10, 10, 600, 600);
setVisible(true);

    }





    public static void main(String args[]){
        ProfilePanel p1= new ProfilePanel();
        p1.setSize(600, 600);
        JFrame jf= new JFrame("propanel");

        jf.setBounds(100, 100, 600, 600);
        jf.setLayout(null);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

        jf.getContentPane().add(p1);
   // jf.pack();

    }
    }

ImagePanel 是另一个将图像添加到此面板的 JPanel。另外,如果有任何方法可以通过调整主 Jframe 及其所有组件的大小来自动调整此面板,请告诉我。我有点学习Java,作为一个新手,我不太了解它。我将非常感谢任何帮助!!!谢谢

4

2 回答 2

9

BoxLayout需要通过容器,但 labelpanel 是null在您创建布局管理器时。因此,您需要先创建面板,然后才能创建布局管理器:

this.labelpanel = new JPanel();
labelpanel.setLayout(new BoxLayout(labelpanel, BoxLayout.PAGE_AXIS));

顺便说一句,摆脱null你在另一部分代码中使用的布局——这是个坏主意,所以最好从一开始就养成始终使用布局管理器的习惯。

于 2013-07-15T07:56:14.827 回答
4

尝试更多类似...

this.labelpanel = new JPanel();
this.labelpanel.setLayout(new BoxLayout(labelpanel, BoxLayout.PAGE_AXIS));

在您的代码中,您传递给的引用BoxLayout将是null尚未labelpanel实例化的

于 2013-07-15T07:52:51.957 回答