这是我写的一个学员管理的界面程序,有个错误我一直解决不了,希望大家帮忙
就是这个代码,大家帮我看看吧.(有点长...)
谢谢
import javax.swing.*;
import java.awt.*;
class FrameTest extends JFrame
{
public FrameTest()
{
super("学员信息");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(400,300);
}
}
class PanelTest
{
JPanel mp;
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
JPanel p5;
JPanel p6;
JPanel p7;
public PanelTest()
{
mp=new JPanel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();
p7=new JPanel();
getContentPane().add(mp);
}
}
class ConTest extends PanelTest
{
JLabel lH;
JLabel lN;
JLabel lA;
JLabel lS;
JLabel lG;
JLabel lD;
JLabel lT;
JCheckBox cBox;
JTextField tN;
JTextField tA;
JRadioButton r1;
JRadioButton r2;
JList iG;
JScrollPane sc; //列表滚动条
JComboBox cDep;
JTable ta;
JScrollPane st; //表格滚动条
JButton b1;
JButton b2;
public ConTest()
{
//主要面板
lH=new JLabel("----------学员信息编辑器------------"); //标题行控件
mp.add(lH);
cBox=new JCheckBox("是否注册",false);
mp.add(cBox); //复选框
//GridBag布局 添加子面板1上的姓名标签和文本框
GridBagLayout lay1=new GridBagLayout();
GridBagConstraints gbc1=new GridBagConstraints();
p1.setLayout(lay1);
lN=new JLabel("姓名:");
gbc1.anchor=GridBagConstraints.EAST;
lay1.setConstraints(lN,gbc1);
tN=new JTextField(15);
tN.setEnabled(false);
gbc1.anchor=GridBagConstraints.WEST;
lay1.setConstraints(tN,gbc1);
p1.add(lN);
p1.add(tN);
mp.add(p1);
//GridBag布局 添加子面板2上的年龄标签和文本框
GridBagLayout lay2=new GridBagLayout();
GridBagConstraints gbc2=new GridBagConstraints();
p2.setLayout(lay2);
lA=new JLabel(" 年龄:");
gbc2.anchor=GridBagConstraints.EAST;
lay2.setConstraints(lA,gbc2);
tA=new JTextField(3);
tA.setEnabled(false);
gbc2.anchor=GridBagConstraints.WEST;
lay2.setConstraints(tA,gbc2);
p2.add(lA);
p2.add(tA);
mp.add(p2);
//GridBag布局 添加子面板3上的性别标签和单选按钮
GridBagLayout lay3=new GridBagLayout();
GridBagConstraints gbc3=new GridBagConstraints();
p3.setLayout(lay3);
lS=new JLabel("性别:");
gbc3.anchor=GridBagConstraints.EAST;
gbc3.gridx=0;
gbc3.gridy=0;
lay3.setConstraints(lS,gbc3);
r1=new JRadioButton("男 ",true);
r1.setEnabled(false);
gbc3.anchor=GridBagConstraints.WEST;
gbc3.gridx=1;
gbc3.gridy=0;
lay3.setConstraints(r1,gbc3);
r2=new JRadioButton("女 ",false);
r2.setEnabled(false);
gbc3.anchor=GridBagConstraints.WEST;
gbc3.gridx=1;
gbc3.gridy=1;
lay3.setConstraints(r2,gbc3);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
p3.add(lS);
p3.add(r1);
p3.add(r2);
mp.add(p3);
//GridBag布局 添加子面板4上的年级标签和列表框
GridBagLayout lay4=new GridBagLayout();
GridBagConstraints gbc4=new GridBagConstraints();
p4.setLayout(lay4);
lG=new JLabel("年级:");
gbc4.anchor=GridBagConstraints.EAST;
gbc4.gridheight=1;
lay4.setConstraints(lG,gbc4);
String[] str1={"一年级","二年级","三年级","四年级"};
iG=new JList(str1);
iG.setEnabled(false);
iG.setFixedCellWidth(80);
iG.setVisibleRowCount(4);
sc=new JScrollPane(iG,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gbc4.anchor=GridBagConstraints.WEST;
gbc4.weightx=1.0;
gbc4.weighty=0.0;
gbc4.gridheight=4;
lay4.setConstraints(sc,gbc4);
p4.add(lG);
p4.add(sc);
mp.add(p4);
//GridBag布局 添加子面板5上的系别标签和下拉列表框
GridBagLayout lay5=new GridBagLayout();
GridBagConstraints gbc5=new GridBagConstraints();
p5.setLayout(lay5);
lD=new JLabel("系别:");
gbc5.anchor=GridBagConstraints.EAST;
lay5.setConstraints(lD,gbc5);
String[] str2={"计算机系","英语系","机械系","建筑系"};
cDep=new JComboBox(str2);
cDep.setEnabled(false);
gbc5.anchor=GridBagConstraints.WEST;
lay5.setConstraints(cDep,gbc5);
p5.add(lD);
p5.add(cDep);
mp.add(p5);
//边界布局 添加子面板6上的表格标签和表格
BorderLayout blay=new BorderLayout();
p6.setLayout(blay);
lT=new JLabel("显示已注册学员的所有信息:");
String[][] data=
{
{"阿龙","35","男","一年级","计算机系"},
{" "," "," "," ","计算机系"},
{" "," "," "," ","计算机系"},
{" "," "," "," ","计算机系"},
{" "," "," "," ","计算机系"},
{" "," "," "," ","计算机系"}
};
String[] names={"姓名","年龄","性别","年级","系别"};
ta=new JTable(data,names);
ta.setEnabled(false);
Dimension dim=new Dimension(380,60);
ta.setPreferredScrollableViewportSize(dim);
st=new JScrollPane(ta);
p6.add("North",lT);
p6.add("Center",st);
mp.add(p6);
//流布局P7上的两个按钮
FlowLayout flay;
flay=new FlowLayout(FlowLayout.RIGHT,10,15);
p7.setLayout(flay);
b1=new JButton("注册(Z)");
b1.setEnabled(false);
b2=new JButton("保存(S)");
b2.setEnabled(false);
p7.add(b1);
p7.add(b2);
mp.add(p7);
//GridBag布局 添加主面板上的两个控件和容器面板
GridBagLayout glay=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
mp.setLayout(glay);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor=GridBagConstraints.NORTHWEST;
glay.setConstraints(lH,gbc);
gbc.gridwidth=1;
glay.setConstraints(cBox,gbc);
glay.setConstraints(p1,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
glay.setConstraints(p2,gbc);
gbc.gridwidth=1;
glay.setConstraints(p3,gbc);
glay.setConstraints(p4,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
glay.setConstraints(p5,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
glay.setConstraints(p5,gbc);
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.gridwidth=GridBagConstraints.REMAINDER;
}
}
public class XueYuan
{
public static void main(String[] args)
{
new FrameTest();
ConTest f=new ConTest();
}
}