使用AWT制作图形界面登陆、注册

   
AWT(Abstract Windows Toolkit)---抽象窗口工具包
§是由JAVA通过对操作系统的自身的界面组件实现完成界面渲染,也就是说,当我们利用 AWT 来构件图形用户界面的时候,我们实际上是在利用操作系统所提供的图形库。
§由于AWT 是依靠本地方法来实现其功能的,我们通常把AWT控件称为重量级控件。
§库中的组件种类比较少、速度比较快

示例图:

登陆页面开始:Login.java

public class Login extends Frame {
TextField textField1;
TextField textField2;
public Login(){
super("用户登录");
this.setLayout(null);
this.setSize(400, 300);
this.setLocation(200, 300);
this.setResizable(false);
Panel panel = new Panel();
panel.setBounds(40, 30, 320, 240);
panel.setLayout(null);

Label label1 = new Label("用户名:");
label1.setBounds(40, 80, 100, 20);
textField1 = new TextField();
textField1.setBounds(150, 80, 140, 20);

Label label2 = new Label("密    码:");
   label2.setBounds(40, 130, 100, 20);
   textField2 = new TextField();
textField2.setBounds(150, 130, 140, 20);

Button btnOk = new Button("OK");
Button btnCancel = new Button("Cancel");
Button btnRegister = new Button("Register");
btnOk.setBounds(30, 180, 60, 20);
btnCancel.setBounds(105, 180, 80, 20);
btnRegister.setBounds(200, 180, 80, 20);

//添加事件
BtnOk ok= new BtnOk();
btnOk.addActionListener(ok);
BtnCancel cancel = new BtnCancel();
btnCancel.addActionListener(cancel);
BtnRegister register = new BtnRegister();
btnRegister.addActionListener(register);

panel.add(label1);
panel.add(label2);
panel.add(textField1);
panel.add(textField2);
panel.add(btnOk);
panel.add(btnCancel);
panel.add(btnRegister);

this.add(panel);

}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Login().setVisible(true);
}
});
//new Login().setVisible(true);

}
//ok事件
class BtnOk implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
//从文件中读取账号密码
String username = "";
String userpwd = "";
String value = "";

File file = new File("E:\\project\\MyExclipse\\AWT01\\src\\along\\song\\login\\login.txt");
                       (建个login.text文本里面可以
                       admin
                       123456)

if(file.exists()){
try {
BufferedReader br = new BufferedReader(new FileReader(file));
value = br.readLine();
if(value != null){
username = value;
}
value = br.readLine();
if(value != null){
userpwd = value;
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
System.out.println("所读取的文件不存在");
}
System.out.println("name="+textField1.getText().trim()+",pwd="+textField2.getText().trim());
System.out.println("name1="+username+",pwd2="+userpwd);
if(textField1.getText().trim().equals(username) && textField2.getText().trim().equals(userpwd)){
JOptionPane.showMessageDialog(null, "登陆成功");
}else{
JOptionPane.showMessageDialog(null, "账号或密码错误,请重新输入","错误", JOptionPane.ERROR_MESSAGE);
}
}

}
//cancel事件
class BtnCancel implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
textField1.setText("");
textField2.setText("");

}

}
//register事件
class BtnRegister implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
dispose();
new Register().setVisible(true);

}

}


}

注册页面:Register.java

public class Register extends JFrame {
//创建面板对象
JPanel jpanel = new JPanel();

//创建控件对象
JLabel labZc = new JLabel("员工注册");
JLabel labName = new JLabel("姓名:");
JLabel labAge = new JLabel("年龄:");
JLabel labSex = new JLabel("性别:");
JLabel labEmail = new JLabel("邮箱:");
JLabel labHobby = new JLabel("爱好");
JButton jbOk = new JButton("确定");
JButton jbQx = new JButton("取消");

JTextField txtName = new JTextField();
JTextField txtEmail = new JTextField();
JComboBox jcbAge = new JComboBox();
JRadioButton jrbMan = new JRadioButton("男");
JRadioButton jrbWoman = new JRadioButton("女");
JCheckBox jcbPlay = new JCheckBox("玩");
JCheckBox jcbSing = new JCheckBox("唱");
JCheckBox jcbEat = new JCheckBox("吃");
ButtonGroup btnSex = new ButtonGroup();

public Register(){
            //设置框架是否可变
this.setResizable(false);
this.setTitle("用户注册");
this.setBounds(300, 300, 300, 400);
btnSex.add(jrbMan);
btnSex.add(jrbWoman);
jpanel.setLayout(null);

labZc.setBounds(100, 20, 100, 20);
jpanel.add(labZc);
labName.setBounds(50, 60, 60, 20);
jpanel.add(labName);
txtName.setBounds(120, 60, 100, 20);
jpanel.add(txtName);
labAge.setBounds(50, 100, 60, 20);
jpanel.add(labAge);
jcbAge.setBounds(120, 100, 100, 20);
for(int i=18;i<=60;i++){
jcbAge.addItem(i);
}
jpanel.add(jcbAge);
labSex.setBounds(50, 140, 60, 20);
jpanel.add(labSex);
jrbMan.setBounds(120, 140, 60, 20);
jpanel.add(jrbMan);
jrbWoman.setBounds(190, 140, 60, 20);
jpanel.add(jrbWoman);
labEmail.setBounds(50, 180, 60, 20);
jpanel.add(labEmail);
txtEmail.setBounds(120, 180, 100, 20);
jpanel.add(txtEmail);
labHobby.setBounds(50, 220, 60, 20);
jpanel.add(labHobby);
jcbPlay.setBounds(120, 220, 40, 20);
jpanel.add(jcbPlay);
jcbSing.setBounds(160, 220, 40, 20);
jpanel.add(jcbSing);
jcbEat.setBounds(200, 220, 40, 20);
jpanel.add(jcbEat);
jbOk.setBounds(60, 260, 80, 20);
jpanel.add(jbOk);
Button_Qd qd = new Button_Qd();
jbOk.addActionListener(qd);
jbQx.setBounds(160, 260, 80, 20);
jpanel.add(jbQx);
Button_Qx qx = new Button_Qx();
jbQx.addActionListener(qx);
this.add(jpanel);
}
class Button_Qd implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
String name = txtName.getText().trim();
String Email = txtEmail.getText().trim();
}

}
class Button_Qx implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txtName.setText("");
txtEmail.setText("");
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Register().setVisible(true);
}
});
}


}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶半欲缺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值