我正在尝试在JApplet
上的不同位置添加两个JPanel
对象。
但是这段代码会抛出异常:
java.lang.IllegalArgumentException: illegal component position
有人可以纠正这段代码吗?
import java.awt.FlowLayout;
import javax.swing.*;
public class TwoPanel extends JApplet {
JPanel p1,p2;
JLabel l1,l2;
@Override
public void init()
{
p1=new JPanel();
p2=new JPanel();
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
l1=new JLabel("Panel 1");
l2=new JLabel("panel 2");
p1.add(l1,0,0);
p2.add(l2,100,100);
add(p1);
add(p2);
p1.setFocusable(true);
p2.setFocusable(true);
}
}
Exception
java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Container.java:1080)
at java.awt.Container.add(Container.java:998)
at TwoPanel.init(TwoPanel.java:25)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:744)