利用绝对布局制作登陆页面(改进)

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
//加载包swing
@SuppressWarnings("serial")
public class ZC extends JFrame{
public ZC()
{
String[] a={"a","s","d","f","g","h","j","k","l","q","w","e","r","t","y","u","i","o","p","z","x","c","v","b","n","m","0","1","2","3","4","5","6","7","8","9"};
setTitle("欢迎登陆");//设置窗体标题
setLayout(null);//窗体取消布局管理器设置
setBounds(0,0,250,350);//定位窗体的位置与大小
@SuppressWarnings("unused")
Container c=getContentPane();//创建容器对象
Random s1=new Random();
Random s2=new Random();
Random s3=new Random();
Random s4=new Random();
int i,j,k,l;
i=s1.nextInt(35);
j=s2.nextInt(35);
k=s3.nextInt(35);
l=s4.nextInt(35);
JLabel yh=new JLabel("用户名");//创建一个用户名标签
JLabel mm=new JLabel("密码");//创建一个密码标签
JLabel yz=new JLabel("验证码");//创建一个验证码标签
JTextField yh1=new JTextField(null);//添加一个文本框
JPasswordField mm1=new JPasswordField(null);//添加一个密码框
JTextField yz1=new JTextField(null);//添加一个文本框
JLabel yz2=new JLabel(a[i]+a[j]+a[k]+a[l]);
JButton b1=new JButton("登陆");
JButton b2=new JButton("取消");
yh.setBounds(10,30,80,30);//用户名位置和大小
mm.setBounds(10,75,80,30);//密码标签位置和大小
yz.setBounds(10,120,80,30);//验证码标签位置和大小
yh1.setBounds(60,30,100,30);//用户名框位置和大小
mm1.setBounds(60,75,100,30);//密码框位置和大小
yz1.setBounds(60,120,100,30);//验证码框位置和大小
yz2.setBounds(70,170,80,30);
b1.setBounds(20,220,80,30);
b2.setBounds(120,220,80,30);
c.add(yh);//讲标签和框添加到容器
c.add(mm);
c.add(yz);
c.add(yh1);
c.add(mm1);
c.add(yz1);

c.add(b2);
c.add(yz2);


c.add(b1);
class YZ implements ActionListener
{
public void actionPerformed(ActionEvent c)
{
String s1=yh1.getText().toString();
String s2=mm1.getText().toString();
String s3=yz1.getText().toString();
String s4=String.valueOf(a[i]+a[j]+a[k]+a[l]);
if(s1.equals(""))//未输入用户名
JOptionPane.showMessageDialog(null,"请输入用户名");
//@SuppressWarnings("deprecation")

else if(s2.equals(""))//未输入密码
JOptionPane.showMessageDialog(null,"请输入密码");
else if(s3.equals(""))//未输入验证码
JOptionPane.showMessageDialog(null,"请输入验证码");
else if(s3.equals(s4))
JOptionPane.showMessageDialog(null,"登陆成功");
else//验证码错误
JOptionPane.showMessageDialog(null,"验证码错误");
}
}
class GB implements ActionListener
{
public void actionPerformed(ActionEvent c)
{
System.exit(0);
}
}
b1.addActionListener(new YZ());
b2.addActionListener(new GB());
setVisible(true);//使窗体可见
}
public static void main( String args[] )
{
new ZC();
}

转载于:https://www.cnblogs.com/love-xi/p/4857807.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值