登录界面设计源码

package pc1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class UserLoad {
 ImageIcon icon;

 JFrame f1 = new JFrame("jack");

 JPanel contentPanel = new JPanel();

 GridLayout show;

 JLabel l2 = new JLabel("用户名:");

 JLabel l3 = new JLabel("密     码:");

 JLabel l4 = new JLabel("                                   "
   + "All rights reserved by jack");

 JLabel l5 = new JLabel("用户名或密码错误!");

 JLabel l6 = new JLabel("");

 // Label l4 = new Label("验证码:");

 JTextField t1 = new JTextField(15);

 JPasswordField t2 = new JPasswordField(15);

 // JTextField t3 = new JTextField();

 JButton b1 = new JButton("登录");

 JButton b2 = new JButton("重置");

 public UserLoad() {
  f1.setBounds(350, 250, 360, 250);
  f1.add(contentPanel);

  show = new GridLayout(6, 1, 0, 0);
  contentPanel.setLayout(show);
  contentPanel.setVisible(true);

  // 用户登录
  JPanel p1 = new JPanel() {
   public void paintComponent(Graphics g) {
    icon = new ImageIcon("D://wwjjdk//Load//pc1//宝马.jpg");
    Dimension dim = getSize();
    g.drawImage(icon.getImage(), 0, 0, 360, 60, null);
   }
  };

  p1.setLayout(new GridLayout(1, 1));
  contentPanel.add(p1);
  // 用户名
  JPanel p2 = new JPanel();
  p2.setLayout(new FlowLayout());
  p2.add(l2);
  p2.add(t1);
  contentPanel.add(p2);
  // 密码
  JPanel p3 = new JPanel();
  p3.setLayout(new FlowLayout());
  p3.add(l3);
  p3.add(t2);
  contentPanel.add(p3);
  /*
   * 验证码 Panel p4 = new Panel(); p4.setLayout(new GridLayout(1, 2, 10,
   * 3)); p4.add(l4); p4.add(t3); contentPanel.add(p4);
   */
  JPanel p4 = new JPanel();
  p4.setLayout(new GridLayout(1, 1));
  p4.add(l6);
  contentPanel.add(p4);
  // 登录与重置
  JPanel p5 = new JPanel();
  p5.setLayout(new FlowLayout());
  p5.add(b1);
  p5.add(b2);
  contentPanel.add(p5);
  // 版权声明
  JPanel p6 = new JPanel();
  p6.setBackground(Color.LIGHT_GRAY);
  p6.setLayout(new GridLayout(1, 1));
  l4.setForeground(Color.blue);
  p6.add(l4);
  contentPanel.add(p6);
  f1.setResizable(false);
  f1.setVisible(true);
  
  b1.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {

     HomePage m=new HomePage();
     f1.setVisible(false);
     m.jf.setVisible(true);
    }
   }

  });
  b1.addActionListener(new ActionListener() {// 对“登陆”进行监听
     public void actionPerformed(ActionEvent e) {
      if (e.getSource() == b1) {
       l6
         .setText("                                          "
           + "用户名或密码错误!");
      }
      if (util(t1.getText(), new String(t2.getPassword()))) {
       HomePage mf=new HomePage();
       f1.setVisible(false);
      }
     }
    });

  
  b2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b2) {
     t1.setText("");
     t2.setText("");
     l6.setText("");

    }
   }
  });

 }

 public static boolean util(String name, String password) {
  boolean f = false;
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader("D:/wwjjdk/Login/Login.txt"));
   String s = null;
   while ((s = br.readLine()) != null) {
    String str[] = s.split(" ");
    if (name.equals(str[0]) && password.equals(str[1])) {
     f = true;
     break;
    }

   }
   br.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return f;
 }

 public static void main(String[] args) {
  UserLoad ul = new UserLoad();

 }

}
 

你可能感兴趣的:(职场,休闲,登录界面)