DOC

02-AWT

By Laurie Hughes,2014-10-08 22:31
15 views 0
02-AWT

    目录

    1知识点 ............................................................................................................................................ 1 1.1上次课程的主要知识点 ........................................................................................................... 1 1.2 本次预计讲解的知识点 .......................................................................................................... 2 2具体内容 ........................................................................................................................................ 2 2.1 AWT .......................................................................................................................................... 2

     2.2 示例......................................................................................................................................... 3

     3 总结 ............................................................................................................................................. 12

    4 作业 ............................................................................................................................................. 12

    课程名称,02-AWT 1知识点

    1.1上次课程的主要知识点

    1

    2

    1.2 本次预计讲解的知识点

     1AWT

     2

2具体内容

    2.1 AWT

     |- 容器

     |- Frame 顶层容器

     |- Dialog 顶层容器

     |- Panel 非顶层容器

     |- 组件

     |- Label 标签 ?用来显示文本或图片,

     |- Button 按钮

     |- TextField 文本框

     …….

     |- 布局管理器

     |- 绝对位置布局

     |- 网格布局 GridLayout

     |- 边框布局 BorderLayout

     |- 事件

2.2 示例

     完成一个登录界面 , 连接mysql数据库!实现登录验证。

    FrmLogin

    package com.whos.javase.course_02.view;

import java.awt.Button;

    import java.awt.Frame;

    import java.awt.Label;

    import java.awt.Panel;

    import java.awt.TextField;

    import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;

    import javax.swing.JOptionPane;

import com.whos.javase.course_02.dao.UserInfoDao;

    import com.whos.javase.course_02.entity.UserInfo;

    public class FrmLogin extends Frame{

     //将窗体上的组件作为窗体的属性

     private Panel panel ;

     private Label lblUsername ;

     private Label lblPassword;

     private TextField txtUsername;

     private TextField txtPassword;

     private Button btnLogin;

     private Button btnReg;

     //初始化窗体

     public FrmLogin(){

     //设置窗体位置及大小

     //super.setBounds(200, 100, 300, 200);

     //设置窗体大小

     super.setSize(300, 200);

     panel = new Panel();

     //设置布局方式

     panel.setLayout(null);//绝对位置布局

     super.add(panel) ;

     lblUsername = new Label();

     lblUsername.setBounds(50, 20, 50, 20);

     lblUsername.setText("用户名:");

     panel.add(lblUsername);

     txtUsername = new TextField();

     txtUsername.setBounds(100, 20, 100, 20);

     panel.add(txtUsername);

     lblPassword = new Label();

     lblPassword.setBounds(50, 50, 50, 20);

     lblPassword.setText("密码:");

     panel.add(lblPassword);

     txtPassword = new TextField();

     txtPassword.setBounds(100, 50, 100, 20);

     panel.add(txtPassword);

     btnLogin = new Button();

     btnLogin.setBounds(120, 80, 30, 20);

     btnLogin.setLabel("登录");

     panel.add(btnLogin);

     btnReg = new Button();

     btnReg.setBounds(160, 80, 30, 20);

     btnReg.setLabel("注册");

     panel.add(btnReg);

     //添加登录的点击事件

     btnLogin.addActionListener(new ActionListener(){

     public void actionPerformed(ActionEvent e) {

     //点击按钮时!会调用此方法

     btnLoginClick(e);

     }

     });

     //添加注册点击事件

     btnReg.addActionListener(new ActionListener() {

     public void actionPerformed(ActionEvent e) {

     // TODO Auto-generated method stub

     btnRegClick(e);

     }

     });

     //窗体关闭事件

     this.addWindowListener(new WindowAdapter() {

     public void windowClosing(WindowEvent e) {

     // TODO Auto-generated method stub

     System.exit(0);

     }

     });

     }

     //登录

     private void btnLoginClick(ActionEvent e){

     //得到输入的用户名和密码

     String username = txtUsername.getText();

     String password = txtPassword.getText();

     UserInfoDao userDao = new UserInfoDao();

     UserInfo user = userDao.queryUser(username);

     if (user==null){

     JOptionPane.showMessageDialog(this, "用户名不存在?");

     }else{

     if (user.getPassword().equals(password)){

     JOptionPane.showMessageDialog(this, "登录成功?");

     }else{

     JOptionPane.showMessageDialog(this, "密码错误?");

     }

     }

     }

     //注册

     private void btnRegClick(ActionEvent e){

     //启动注册的窗体

     FrmReg frmReg = new FrmReg();

     frmReg.setLocationRelativeTo(null);

     frmReg.setVisible(true);

     }

    }

UserInfo

    package com.whos.javase.course_02.entity;

//实体类

    public class UserInfo {

     private int userId;

     private String username;

     private String password;

     public int getUserId() {

     return userId;

     }

     public void setUserId(int userId) {

     this.userId = userId;

     }

     public String getUsername() {

     return username;

     }

     public void setUsername(String username) {

     this.username = username;

     }

     public String getPassword() {

     return password;

     }

     public void setPassword(String password) {

     this.password = password;

     }

}

UserInfoDao

public class UserInfoDao {

     //根据用户名查询用户信息

     public UserInfo queryUser(String username){

Report this document

For any questions or suggestions please email
cust-service@docsford.com