Java登录界面的实现

1  用Java实现一个登录界面需要哪些API类?

   大家先看一下QQ的登录界面  


截图中,我们可以看到,上面的主要部分有:登录按钮,输入框(包括密码输入框和账号输入框)  用户头像  等等

所以,要实现一个简单的登录界面,我们要用到的API类有:   

JFrame:窗体容器组件类   MouseListener:鼠标事件接口,提供处理按下,释放,点击,进入和离开动作的接口

MouseEvent:捕获事件源对象的信息和操作      JLabel    JTextField:账号输入框     JPasswordField:密码输入框

讲一下事件监听机制:

包括事件源对象,监听方法以及事件接口   

事件源对象: 首先,所有容器组件和元素组件都可以成为事件源对象  简单来说,你的这个动作(比如说点击) 发生在哪一个组件上面,哪一个就是你的事件源对象  (点击的按钮就是一个事件源对象)   

事件监听方法:捕获事件源对象上动作的方法   

      addActionListener()  捕获类似鼠标的点击动作 或输入框上面的回车动作  然后将动作和信息交给addActionListener()事件       监听方法的ActionListener参数对象

      addMouseListener()  addMouseMotionListener  addKeyListener()  ......等等

事件接口(也叫事件处理类): 处理动作的具体操作 

    ActionListener() 动作事件接口,只有一个事件处理方法,事件处理方法中有一个参数ActionEvent,这个对象中存储事件源对象的信息和动作处理

    MoseListener() 鼠标事件接口,有五个事件处理方法,分别时按下(MousePressed),释放(MouseReleased),点击(MouseClicked),进入(MouseEntered)和离开(MouseExited)    这5个方法中都有一个参数(MouseEvent)

     MouseMotionListener()  鼠标移动事件接口,有2个事件处理方法 移动和拖动...

登录界面的主要代码:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Login frame=new Login();
		frame.show();
	}

	public void show() {
		JFrame frame=new JFrame();
		frame.setTitle("登陆界面");
		frame.setDefaultCloseOperation(3);      //发起close时默认执行操作(读者可自行百度)
		frame.setSize(380, 600);
		frame.setLocationRelativeTo(null);         //设置窗体显示在居中位置
		frame.setResizable(false);
		frame.setLayout(new FlowLayout());         //设置为流式布局
		
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(30);        //账号输入框 括号内是输入框长度
		
		JLabel L2=new JLabel("密码:");
		JPasswordField te2=new JPasswordField(30);//   PasswordField 的使用  密码输入框
		te2.setEchoChar('*');
		
		JLabel L=new JLabel();
		L.setIcon(new ImageIcon ("D:\\JAVA\\图片\\新建文件夹\\4.jpg"));    //将你的电脑里面的照片添加到L这个组件上来
		frame.add(L);                                  //将其他组件都添加到窗体上面来
		frame.add(L1);
		frame.add(te1);
		frame.add(L2);
		frame.add(te2);
		
		JButton B1=new JButton("登陆");
		ButtonListener li1=new ButtonListener(te1,te2);             
		B1.addActionListener(li1);                            //给登录按钮添加监听
		frame.add(B1);
		JButton B2=new JButton("取消");
		frame.add(B2);
		frame.setVisible(true);         //窗体设置为可见
	}
	//自己定义一个类来实现接口
		public  class ButtonListener implements java.awt.event.ActionListener{    //实现ActionListener 接口 implement
			public JTextField te1=new JTextField();               //传参
			public JPasswordField te2=new JPasswordField();
//			public Huaban hua=new Huaban();                       //一个画板对象
			public ButtonListener(JTextField te1,JPasswordField te2) {//重载    窗体上的账号框,密码框传到监听上来
				this.te1=te1;	
				this.te2=te2;
			}
			public void actionPerformed(ActionEvent e) {            //捕获点击动作
				String zhang=te1.getText();                     //getText   用于获取输入框内的东西
				String mi= String.valueOf(te2.getPassword());   //获得密码框内的东西(获取的不同方法读者可以自行百度)
				if((zhang.equals("123456")==true)&&(mi.equals("111"))) {    //设置账号密码匹配
//					hua.show1();	                  //此处可以调用一个画板对象中的函数 弹出一个界面
				}
				else System.out.println("密码错误");
			}
			
		}
}

以上就是界面实现的代码   

看一下我的丑界面  :


确实做得有点随便    不过实现了基本的功能  

猜你喜欢

转载自blog.csdn.net/hudaJY/article/details/80285093