JAVA J2SE SWING AWT 登录界面 输入用户名和密码 登录器

 

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

/**
 * @author greatwqs
 * @date 2009年10月19日
 */
public class loginFrame extends JFrame {
 public loginFrame() {
  super("管理系统登陆");
  this.setBounds(280, 280, 300, 200);
  setLayout(new BorderLayout());
  // 设置登陆界面的宽度和高度

  JPanel p1 = new JPanel();
  p1.setLayout(new GridLayout(2, 2));
  p1.add(userL);
  p1.add(user);
  p1.add(passwordL);
  p1.add(password);
  p1.setSize(150, 100);

  JPanel p2 = new JPanel();
  p2.add(ok);
  p2.add(cancel);

  add("North", p1);
  add("Center", p2);
  add("South", cue);

  // 到此为止,登陆的界面基本结束!下面的是为里面的按钮设置具体的事件监听器

  cancel.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });// 设置登陆时的取消按钮,按这个按钮就退出所有的程序

  ok.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if ((user.getText()).equals("team")
      && String.valueOf((password.getPassword())).equals(
        "team")) {
     result = true;
     // hidFun();
     JOptionPane.showMessageDialog(null, "你好:你登陆成功,进入系统!");

    } else {
     // cue.setText("你输入的密码错误!");
     JOptionPane.showMessageDialog(null, "你好:你输入的密码错误,请重新输入!");

    }
   }
  }

  );// 设置ok键的事件监听器,并验证用户名和密码,密码输入错误时提示"您输入的密码错误!"

 }

 public boolean isLogin() {
  return result;
 }

 /**
  * 设置本界面隐藏..
  *
  */
 private void hidFun() {
  this.setVisible(false);
 }

 private JLabel userL = new JLabel("用户名");

 private JLabel passwordL = new JLabel("密码");

 private JLabel cue = new JLabel();

 private JTextField user = new JTextField("team");

 private JPasswordField password = new JPasswordField();

 private JButton ok = new JButton("确定");

 private JButton cancel = new JButton("退出");

 private boolean result = false;

 /**
  * 主函数.....
  *
  * @param wqs
  */
 public static void main(String[] wqs) {
  loginFrame oop = new loginFrame();
  oop.setVisible(true);
  oop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

 

 

 

 

 

 

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NULLLayoutDemo extends JFrame implements ActionListener {
 private JLabel lbUser = new JLabel();
 private JLabel info = new JLabel();
 private JLabel lbPassword = new JLabel();
 private JButton btnLog = new JButton();/* 该行有错误什么问题呀 */
 private Container container = getContentPane();
 private JTextField jtextfield_1 = new JTextField(10);
 private JPasswordField jtextfield_2 = new JPasswordField(10);

 public NULLLayoutDemo() {
  super("登陆器");
  this.setSize(350, 200);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 public static void main(String[] args) {
  NULLLayoutDemo frame = new NULLLayoutDemo();
  frame.setLayout();
  frame.setVisible(true);
 }

 public void setLayout() {
  container.setLayout(null);
  lbUser.setText("用户名");
  container.add(lbUser);

  container.add(jtextfield_1); // 增加输入用户名的文本框
  jtextfield_1.setBounds(80, 26, 120, 26);// 设置用户名的文本框位置

  lbUser.setBounds(16, 26, 42, 16);
  lbPassword.setText("密码");

  container.add(lbPassword);
  lbPassword.setBounds(16, 56, 40, 16);
  btnLog.setText("登陆");

  jtextfield_2.setEchoChar('$');
  container.add(jtextfield_2);// 设置密码的文本框位置
  jtextfield_2.setBounds(80, 56, 120, 26);// 增加输入密码的文本框

  container.add(btnLog);
  btnLog.addActionListener(this);
  btnLog.setBounds(125, 101, 73, 25);

  info.setBounds(0, 130, 300, 35); // 设置信息的标签
  info.setText("用户名是:name 密码是:password");
  container.add(info);
  /*
   * public void ceshi(){ if() }
   */
 }

 public void actionPerformed(ActionEvent e) {

  String name = jtextfield_1.getText(); // 取得用户名文本框的信息
  String password = jtextfield_2.getText();// 取得密码文本框的信息
  // 判断是否符合条件
  if ("name".equals(name) && "password".equals(password)) {
   JOptionPane.showMessageDialog(this, "恭喜你!输入正确", "通过",
     JOptionPane.INFORMATION_MESSAGE, null);
  } else {
   JOptionPane.showMessageDialog(this, "你的输入错误", "错误",
     JOptionPane.INFORMATION_MESSAGE, null);

  }

 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值