程序编译通过,但运行错误,错误提示后附程序:
Exception in thread "main"
java.lang.IllegalArgumentException:illegal anchor value
程序如下:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import java.util.Vector;
public class Register_GUI{
public Register_GUI(){
RegisterFrame rf = new RegisterFrame();
rf.setVisible(true);
}
public static void main(String[] args){
new Register_GUI();
}
}
class RegisterFrame extends JFrame{
private Toolkit tool;
public RegisterFrame(){
setTitle("用户名");
tool=Toolkit.getDefaultToolkit();
Dimension ds=tool.getScreenSize();
int w=ds.width;
int h=ds.height;
setBounds((w-300)/2,(h-300)/2,300,300);
setResizable(false);
RegisterPanel rp=new RegisterPanel(this);
add(rp);
}
}
class RegisterPanel extends JPanel implements ActionListener{
private JLabel titlelabel,namelabel,pwdlabel1,pwdlabel2,sexlabel,agelabel,classlabel;
private JTextField namefield,agefield;
private JPasswordField pwdfield1,pwdfield2;
private JButton commitbtn,resetbtn,cancelbtn;
private JRadioButton rbtn1,rbtn2;
private JComboBox combo;
private Vector<String>v;
private GridBagLayout gbl;
private GridBagConstraints gbc;
private JPanel panel;
private Box box;
private JFrame iframe;
public RegisterPanel(JFrame frame){
iframe=frame;
titlelabel=new JLabel("用户注册");
titlelabel.setFont(new Font("隶书",Font.BOLD,24));
namelabel=new JLabel("用户名: ");
pwdlabel1=new JLabel("密码: ");
pwdlabel2=new JLabel("确认密码: ");
sexlabel=new JLabel("性别: ");
agelabel=new JLabel("年龄:");
classlabel=new JLabel("所属班级:");
namefield=new JTextField(16);
pwdfield1=new JPasswordField(16);
pwdfield1.setEchoChar('*');
pwdfield2=new JPasswordField(16);
pwdfield2.setEchoChar('*');
agefield=new JTextField(16);
rbtn1=new JRadioButton("男");
rbtn2=new JRadioButton("女");
rbtn1.setSelected(true);
ButtonGroup bg=new ButtonGroup();
bg.add(rbtn1);
bg.add(rbtn2);
v=new Vector<String>();
v.add("软件英语053");
v.add("软件英语052");
v.add("软件英语051");
v.add("计算机应用051");
v.add("计算机应用052");
combo=new JComboBox(v);
commitbtn=new JButton("注册");
commitbtn.addActionListener(this);
resetbtn=new JButton("重置");
resetbtn.addActionListener(this);
cancelbtn=new JButton("取消");
cancelbtn.addActionListener(this);
panel=new JPanel();
panel.add(rbtn1);
panel.add(rbtn2);
Border border=BorderFactory.createTitledBorder("");
panel.setBorder(border);
box=Box.createHorizontalBox();
box.add(commitbtn);
box.add(Box.createHorizontalStrut(30));
box.add(resetbtn);
box.add(Box.createHorizontalStrut(30));
box.add(cancelbtn);
gbl=new GridBagLayout();
setLayout(gbl);
gbc=new GridBagConstraints();
addCompnent(titlelabel,0,0,4,1);
add(Box.createVerticalStrut(20));
gbc.anchor=GridBagConstraints.CENTER;
gbc.anchor=GridBagConstraints.HORIZONTAL;
gbc.weightx=0;