package demo_layout;
//import javax.swing.JFrame;
//import javax.swing.JLabel;
//import javax.swing.JPasswordField;
//import javax.swing.JTextField;
import java.awt.*;
import javax.swing.*;
public class Registers extends JFrame{
//定义组件
Jpanel jp1,jp2,jp3;//面板组件
JLabel jl1,jl2;//标签组件
JButton jButtons1,jButtons2;
JTextField jTextField;//文本框
JPasswordField jPasswordField;//密码框
public static void main(String[] args){
Registers register=new Registers();
}
public Registers(){
//创建组件
jp1=new Jpanel();
jp2=new Jpanel();
jp3=new Jpanel();
jl1=new JLabel("用户名");
jl2=new JLabel("密码");
jButtons1=new JButton("登录");
jButtons2=new JButton("取消");
jTextField=new JTextField(10);
jPasswordField=new JPasswordField(10);
//设置布局管理器
this.setLayout(new GridLayout(3,1));
//添加组件
this.add(jp1);
this.add(jp2);
this.add(jp3);
jp1.add(jl1);
jp1.add(jTextField);
jp2.add(jl2);
jp2.add(jPasswordField);
jp3.add(jButtons1);
jp3.add(jButtons2);
//设置组件
this.setSize(300, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
自己跟着视频敲的代码,为什么会报错?
错误是:Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at demo_layout.Registers.<init>(Registers.java:37)
at demo_layout.Registers.main(Registers.java:19)
错误在哪儿呢?请指教
------解决方案--------------------
把程序里面的
Jpanel全部改为
JPanel