javaSE基础编程——GUI窗体

来源:互联网 发布:java调用mondodb的sum 编辑:程序博客网 时间:2024/06/07 05:13

设计一个简易的QQ登陆窗体,不需要实现登陆判断功能


//登陆

package com.cissst.software.loginview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.CharBuffer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * 登录窗体
 * @author Administrator
 * @date 2015-8-11
 */
public class QQLoginFrame extends JFrame {

 //用户名
 private JLabel lbl_user;
 private JTextField txt_user;
 //密码
 private JLabel lbl_pwd;
 private JPasswordField txt_pwd;
 //登录按钮
 private JButton btnLogin;
 //退出按钮
 private JButton btnCancel;
 
 public QQLoginFrame(){
  this.setTitle("QQ窗口");
  this.setBounds(400, 400, 300, 400);
  //取消布局管理器,自定义组件的大小和位置
  this.setLayout(null);
  
  lbl_user = new JLabel("用户名:");
  lbl_user.setLocation(10, 10);
  lbl_user.setSize(50, 30);
  this.add(lbl_user);
  
  txt_user = new JTextField();
  txt_user.setLocation(70, 10);
  txt_user.setSize(200, 30);
  this.add(txt_user);
  
  lbl_pwd = new JLabel("密码:");
  lbl_pwd.setLocation(10, 60);
  lbl_pwd.setSize(50, 30);
  this.add(lbl_pwd);
  
  txt_pwd = new JPasswordField();
  txt_pwd.setLocation(70, 60);
  txt_pwd.setSize(200, 30);
  this.add(txt_pwd);
  
  btnLogin = new JButton("登录");
        btnLogin.setSize(100,30);
        btnLogin.setLocation(90, 120);
       
       
        //登录按钮注册监听器
        btnLogin.addActionListener(new ActionListener(){

   @Override
   public void actionPerformed(ActionEvent e) {
    validateUser(e);    
   }
         
        });
        this.add(btnLogin);
  
  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  this.setVisible(true);
  
 }
 
 /**
  * 验证用户的合法性
  * @param e
  */
 public void validateUser(ActionEvent e){
  //1.获取用户名
  String username =txt_user.getText();
  JOptionPane.showMessageDialog(this, username+"登陆成功");
  //2.读取文件,验证用户的合法性(同学们完成)
//  String myname1 =  txt_user.getText();
  char[] password = txt_pwd.getPassword();
  //将图形界面的内容写入文件中
  try {
   FileWriter writer = new FileWriter("test1.txt");
   writer.write(username);
   writer.write(password);
   writer.flush();//清空缓存
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
//  FileReader reader = null;
//  try {
//   reader = new FileReader("test1.txt");
//  } catch (FileNotFoundException e1) {
//   // TODO Auto-generated catch block
//   e1.printStackTrace();
//  }
//  try {
//   reader.read();
//   reader.read(password);
//  } catch (IOException e1) {
//   // TODO Auto-generated catch block
//   e1.printStackTrace();
//  }
  
  
  //3.合法用户,跳转到注册窗体,当前窗体不显示
  //创建注册窗体对象
  QQRegisterFrame qq = new QQRegisterFrame(username);
  //qq.setUname(username);
  //qq.setTitle(username);
  qq.setVisible(true);
  this.setVisible(false);
 }
 
 public static void main(String[] args) {
  new QQLoginFrame();
 }
}



//注册

package com.cissst.software.loginview;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
 * 登录窗体
 * @author Administrator
 * @date 2015-8-11
 */
public class QQRegisterFrame extends JFrame{

 //昵称
 private JLabel lbl_user;
 private JTextField txt_user;
 //密码
 private JLabel lbl_pwd;
 private JPasswordField txt_pwd;
 //确认密码
 private JLabel lbl_pwd1;
 private JPasswordField txt_pwd1;
 //性别
 private JLabel lbl_sex;
 private ButtonGroup btnGroup;
 private JRadioButton rdoMale;
 private JRadioButton rdoFemale;
 //爱好
 private JLabel lbl_hobby;
 private JCheckBox chcStudy;
 private JCheckBox chcReStudy;
 private JCheckBox chcRRStudy;
 //个性签名
 private JLabel lbl_sign;
 private JTextArea txt_sign;
 //登录按钮
 private JButton btnLogin;
 //退出按钮
 private JButton btnCancel;
 private JScrollPane scrollpane;
 
 public QQRegisterFrame(String uname){
//  this.setTitle("QQ窗口");
  this.setTitle(uname + "登录");
  this.setBounds(400, 400, 300, 400);
  //取消布局管理器,自定义组件的大小和位置
  this.setLayout(null);
  //用户名
  lbl_user = new JLabel("昵称:");
  lbl_user.setLocation(10, 10);
  lbl_user.setSize(50, 30);
  this.add(lbl_user);
  
  txt_user = new JTextField();
  txt_user.setLocation(70, 10);
  txt_user.setSize(200, 30);
  this.add(txt_user);
  
  //密码
  lbl_pwd = new JLabel("密码:");
  lbl_pwd.setLocation(10,50);
  lbl_pwd.setSize(50, 30);
  this.add(lbl_pwd);
  
  txt_pwd = new JPasswordField();
  txt_pwd.setLocation(70, 50);
  txt_pwd.setSize(200, 30);
  this.add(txt_pwd);
  
  //确认密码
  lbl_pwd = new JLabel("确认密码:");
  lbl_pwd.setLocation(10,80);
  lbl_pwd.setSize(90,60);
  this.add(lbl_pwd);
    
  txt_pwd = new JPasswordField();
  txt_pwd.setLocation(70, 90);
  txt_pwd.setSize(200, 30);
  this.add(txt_pwd);
  
  //性别
  lbl_sex = new JLabel("性别:");
  lbl_sex.setLocation(10,140);
  lbl_sex.setSize(50, 30);
  this.add(lbl_sex);
  
  //创建单选按钮组件男
  btnGroup = new ButtonGroup();
  rdoMale = new JRadioButton("男");
  rdoMale.setLocation(60,125);
  rdoMale.setSize(80, 60);
  //默认该单选按钮组件选中状态
  rdoMale.setSelected(true);
  btnGroup.add(rdoMale);
  this.add(rdoMale);
  //创建单选按钮组件女
  rdoFemale = new JRadioButton("女");
  rdoFemale.setLocation(150,125);
  rdoFemale.setSize(80, 60);
  //默认该单选按钮组件选中状态
  rdoFemale.setSelected(true);
  btnGroup.add(rdoFemale);
  this.add(rdoFemale);
  
  //爱好
  lbl_hobby = new JLabel("爱好:");
  lbl_hobby.setLocation(10,185);
  lbl_hobby.setSize(50, 30);
  this.add(lbl_hobby);
  //第一个复选框
  chcStudy = new JCheckBox("学习");
  chcStudy.setLocation(60, 190);
  chcStudy.setSize(60, 20);
  this.add(chcStudy);
  //第二个复选框
  chcReStudy = new JCheckBox("再学");
  chcReStudy.setLocation(120, 190);
  chcReStudy.setSize(60, 20);
  this.add(chcReStudy);
  //第三个复选框
  chcRRStudy = new JCheckBox("又学");
  chcRRStudy.setLocation(180, 190);
  chcRRStudy.setSize(60, 20);
  this.add(chcRRStudy);
  
  //个性签名
  lbl_sign = new JLabel("个性签名:");
  lbl_sign.setLocation(10, 210);
  lbl_sign.setSize(90, 60);
  this.add(lbl_sign);
  
  txt_sign=new JTextArea(6,10);
  txt_sign.setLineWrap(true);
  txt_sign.setBounds(70, 210, 200, 100);
  //给文本域添加滚动面板
  scrollpane=new JScrollPane(txt_sign);
  scrollpane.setBounds(70,210,200,100);
  this.add(scrollpane);

  //读取按钮
  btnLogin = new JButton("读出注册信息");
  btnLogin.setLocation(70, 470);
  btnLogin.setSize(70, 25);
  this.add(btnLogin);
  
  //退出按钮
  btnCancel = new JButton("退出");
  btnCancel.setLocation(170, 470);
  btnCancel.setSize(70, 25);
  this.add(btnCancel);
  
  //给按钮添加事件
  //1.匿名函数
  btnLogin.addActionListener(new ActionListener(){
  @Override
  public void actionPerformed(ActionEvent e){
   String gender="";
    if(rdoMale.isSelected()){
     gender ="男";
     }else{
       gender="女";
      }
      String hobby ="";
      if(chcStudy.isSelected()){
       hobby ="学习";
      }
      if (chcReStudy.isSelected()){
       hobby = hobby +" " + "再学";
      }
      if (chcRRStudy.isSelected()){
       hobby = hobby +" " + "又学";
      }
      
      //同学们完成:注册窗体读取注册然后将用户注册的内容写入文件.
       
       String myname =  txt_user.getText();
       String mysign =  txt_sign.getText();
       char[] password = txt_pwd.getPassword();
       
       System.out.println("昵称:" + myname);
       System.out.println("性别:" + gender);
       System.out.println("密码:" + password);
       System.out.println("兴趣爱好:" + hobby);
       System.out.println("个性签名:" + mysign);
       
       //将图形界面的内容写入文件中
       try {
        FileWriter writer = new FileWriter("test.txt");
        writer.write(myname);
        writer.write(gender);
        writer.write(password);
        writer.write(hobby);
        writer.write(mysign);
        writer.flush();//清空缓存
       } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }
       
       FileReader reader1 = null;
       try {
        reader1 = new FileReader("test.txt");
       } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }
       try {
        reader1.read();
        reader1.read(password);
       } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }
       
     }
    });
    this.add(btnLogin);
    
    
  //关闭窗体程序结束
  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  this.setVisible(true);
  
 }
 
 /*public static void main(String[] args) throws Exception {
  new QQRegisterFrame();
 }*/
}


0 0
原创粉丝点击