📄 test.java
字号:
/**
*@用户注册系统
*@版本 V1.0
*@二00六年4月22日
*@小刘制作
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//使用JFrame面板并实现坚听
class test extends JFrame implements ActionListener
{
//定义各组件
JPanel p1;JPanel p2;JPanel p3;JPanel p4;
JButton bt1;JButton bt2;
Checkbox r1;Checkbox r2;CheckboxGroup cg;
JCheckBox c1;JCheckBox c2;JCheckBox c3;
JLabel j1;JLabel j2;JLabel j3;JLabel j4;JLabel j5;JLabel j6;
JTextField t1;JTextField t3;JPasswordField t2;
JComboBox Income;
//给各组件赋值
public test(String title)
{
super(title);
Container c = getContentPane();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
cg = new CheckboxGroup();
r1 = new Checkbox("GG", cg,true);
r2 = new Checkbox("MM", cg, false);
bt1 = new JButton("注册");
bt2 = new JButton("重置");
c1 = new JCheckBox("PK");
c2 = new JCheckBox("泡MM");
c3 = new JCheckBox("自恋");
j1 = new JLabel("姓名:");
j2 = new JLabel("密码:");
j3 = new JLabel("伊妹儿:");
j4 = new JLabel("性别:");
j5 = new JLabel("爱好:");
j6 = new JLabel("月收入:");
t1 = new JTextField(20);
t2=new JPasswordField(20);
t3 = new JTextField(20);
String m[] = {"1000以下","2000~3000","3000~4000","4000以上"};
Income = new JComboBox(m);
//把Panel加到JFrame中,并用网格布局排列
p1.setLayout(new GridLayout(3,2,10,10));
p1.add(j1);p1.add(t1);p1.add(j2);p1.add(t2);p1.add(j3);p1.add(t3);
p2.setLayout(new GridLayout(1,1,50,0));
p2.add(j4);p2.add(r1);p2.add(r2);
p3.setLayout(new GridLayout(1,1,50,0));
p3.add(j5);p3.add(c1);p3.add(c2);p3.add(c3);
p4.setLayout(new GridLayout(2,2,2,2));
p4.add(j6);p4.add(Income);p4.add(bt1);p4.add(bt2);
c.setLayout(new GridLayout(4,1));
c.add(p1);c.add(p2);c.add(p3);c.add(p4);
//对按钮进行监听
bt1.addActionListener(this);
bt2.addActionListener(this);
}
//对事件进行处理
public void actionPerformed(ActionEvent e)
{
int ok1=0,ok2=0,ok3=0;
String str1=new String();String str2=new String();
String str3=new String();String str4=new String();
String str5=new String();
//判断用户名是否为空
if(e.getSource()==bt1)
{
str1=t1.getText();
if(str1.length()==0)
{
JOptionPane.showMessageDialog(this,"姓名不能为空");
}
else
{
ok1=1;
}
}
//判断密码是否为空并且是否为8位
if(e.getSource()==bt1)
{
char s[];
s=t2.getPassword();
str2=new String(s);
if(str2.length()==0)
{
JOptionPane.showMessageDialog(this,"您没有输入密码");
}
else if(str2.length()<8)
{
JOptionPane.showMessageDialog(this,"密码不满8位");
}
else
{
ok2=1;
}
}
//判断邮件是否为空或是否合法
if(e.getSource()==bt1)
{
int a1=0,a2=0,a3=0;
int b1=0;
boolean red=true;
str3=t3.getText();
int len=str3.length();
System.out.println("len="+len);
int i=0,j=0,k=0;
while(red)
{
if(str3.length()==0)
{
JOptionPane.showMessageDialog(this,"邮件没写,傻瓜");
red=false;
break;
}
else
{
a1=1;
}
for(i=0;i<len;i++)
{
if(str3.charAt(i)=='@')
{
j=i;break;
}
}
for(i=0;i<len;i++)
{
if(str3.charAt(i)=='.')
{
k=i;break;
}
}
System.out.println("j="+j);
System.out.println("k="+k);
if(j>=k||j==0||k-j<=2)
{
JOptionPane.showMessageDialog(this,"邮件格式错误");
red=false;
break;
}
else
{
a2=1;
}
for(i=j+1;i<len;i++)
{
System.out.println("i="+i);
if(i!=k)
{
if(str3.charAt(i)=='.'||str3.charAt(i)=='@')
{
JOptionPane.showMessageDialog(this,"邮件格式错误");
b1=1;
red=false;
break;
}
else
{
a3=1;
}
}
}
if(b1==1)
{
break;
}
if(a1==1&&a2==1&&a3==1)
{
ok3=1;
red=false;
}
}
}
//把性别与爱好分别赋给字符串str3和str4
if(e.getSource()==bt1)
{
Checkbox s=cg.getSelectedCheckbox();
str4=s.getLabel();
if(c1.isSelected())
{
str5=str5+" "+ c1.getText();
}
else if(c2.isSelected())
{
str5=str5+" "+c2.getText();
}
else if(c3.isSelected())
{
str5=str5+" "+c3.getText();
}
else
{
str5="无";
}
}
//最后将用户的输入与选择打印出来
if(e.getSource()==bt1)
{
if(ok1==1&&ok2==1&&ok3==1)
{
JOptionPane.showMessageDialog(this,"用户名:"+str1+'\n'+
"密码:"+str2+'\n'+"伊妹儿:"+str3+'\n'+"性别:"+str4+'\n'+
"爱好:"+str5+'\n'+"收入:"+Income.getSelectedItem());
}
t1.setText(null);t2.setText(null);t3.setText(null);
c1.setSelected(false);c2.setSelected(false);c3.setSelected(false);
Income.setSelectedIndex(0);
}
//如果重置按钮被按下
if(e.getSource()==bt2)
{
t1.setText(null);t2.setText(null);t3.setText(null);
c1.setSelected(false);c2.setSelected(false);c3.setSelected(false);
Income.setSelectedIndex(0);
}
}
//程序的入口
public static void main(String args[])
{
test p = new test("用户注册");
p.setSize(400,400);
p.setVisible(true);
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -