第十二周课程总结

一、用户登录界面

实验源码

package test1;

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.JPasswordField;
import javax.swing.JTextField;

public class 图像界面 {
public static void main(String[] args) {
	JFrame f=new JFrame("用户登录系统");
	f.setLayout(null);
	JLabel info=new JLabel();
	JButton submit=new JButton("登录");
	JButton reset=new JButton("重置");
	JLabel namelab=new JLabel("用户名");
	JLabel passlab=new JLabel("密码");
	JTextField nametext=new JTextField();
	JPasswordField passtext=new JPasswordField();
	
	submit.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getSource()==submit) {
				String n=nametext.getText();
				String p=new String(passtext.getPassword());
				if(n.equals("liulei")&&p.equals("123456")) {
					info.setText("登录成功");
				}
				else if(n.equals("")) {
					info.setText("请输入用户名");
				}
				else if(p.equals("")) {
					info.setText("请输入密码");
				}
				else
					info.setText("用户名或密码错误");
			}
		}
	});
	reset.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getSource()==reset) {
				nametext.setText("");
				passtext.setText("");
				info.setText("");
			}
		}
	});
	
	namelab.setBounds(20,20, 40, 40);
	passlab.setBounds(20, 80, 40, 40);
	nametext.setBounds(80, 20, 240, 40);
	passtext.setBounds(80, 80, 240, 40);
	submit.setBounds(120, 140, 60, 40);
	reset.setBounds(220, 140,60, 40);
	info.setBounds(10, 200, 100, 40);
	
	f.add(namelab);
	f.add(passlab);
	f.add(nametext);	
	f.add(passtext);
	f.add(submit);
	f.add(reset);
	f.add(info);
	f.setSize(400,300);
	f.setLocation(450,234);
	f.setVisible(true); 
}
}

实验结果



二、文本组件JTextComponent

JTextComponent类的常用方法

单行文本输入组件JTextField

JTextField类的常用方法

JTextField类可以使用JTextComponent类的所有方法

密文输入组件JPasswordField

JPasswordField类的常用方法

三、事件处理

1.事件和监听器

事件就是表示一个对象发生状态的变化,监听器对事件的变化进行相应的处理。

事件的处理流程

2.窗体事件

WindowListener是专门处理窗体事件的监听接口,如窗口打开、关闭等。

WindowLisener接口的方法

3.动作事件及监听处理

ActionListener接口处理按钮的动作事件

ActionListener接口方法

posted @ 2019-11-15 15:23  L磊  阅读(215)  评论(0编辑  收藏  举报