编写Java程序,使用Swing事件处理机制实现用户登录和英雄信息显示

返回本章节

返回作业目录


需求说明:

使用Swing事件处理机制实现用户登录和英雄信息显示

实现思路:

创建LoginView类,该类用于显示登录界面,为登录按钮添加ActionListener事件,监听鼠标的左键单击事件。该事件对应的处理方法中,接收用户输入的账号和密码,如果用户输入的账号和密码为空,使用JOptionPane对话框显示友好提示。

当用户输入的账号或密码不正确,系统使用JOptionPane对话框给出友好提示信息,如果用户输入的账号密码正确,调用创建HeroListView类的无参构造器,即可显示英雄信息列表,并将登录界面隐藏。

创建HeroListView类,该类用于显示英雄信息列表,将数据显示在JTable中。

实现代码:

LoginView类:

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

public  class LoginView extends JFrame {
	public  LoginView() {
		this.setTitle("用户登录");
		this.setBounds(0, 0, 500, 350);
		this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
		this.setResizable(false);//让窗口大小不可改变
		this.setLayout(null);
		
		JLabel userText = new JLabel("账号:");
		userText.setBounds(90, 70, 80, 25);
		JLabel pwdText = new JLabel("密码:");
		pwdText.setBounds(90, 140, 80, 25);
		JTextField userId = new JTextField();
		userId.setBounds(160, 70, 220, 30);
		JPasswordField userPsd = new JPasswordField();
		userPsd.setBounds(160, 140, 220, 30);
		JButton DL = new JButton("登录");
		DL.setBounds(160, 200, 80, 35);
		JButton ZC = new JButton("注册");
		ZC.setBounds(285, 200, 80, 35);
		
		this.add(userText);
		this.add(pwdText);
		this.add(userId);
		this.add(userPsd);
		this.add(DL);
		this.add(ZC);
		
		//为DL添加鼠标被单击事件
		DL.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String id = userId.getText();
				String pwd = userPsd.getText();
				if("".equals(id)||"".equals(pwd)) {
					JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
				}else if ("123".equals(id)&&"123".equals(pwd)) {
					JOptionPane.showMessageDialog(null, "登录成功!欢迎使用!");
					HeroListView hv = new HeroListView();
					hv.setVisible(true);
					setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "账户名或密码错误,请检查!");
				}
			}
		});
	}
}

HeroListView类:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

public class HeroListView extends JFrame {
	public HeroListView() {
		this.setTitle("英雄信息列表");
		this.setBounds(0, 0, 500, 350);
		this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
		this.setResizable(false);//让窗口大小不可改变
		this.setLayout(null);
		this.setVisible(true);
		BorderLayout bl=new BorderLayout();
		this.setLayout(bl);
		//定义表格的表头信息
		String[] heads={"姓名","年龄","电话"};
		//定义表格中显示的信息
		Object[][] data={
	        		{"丘处机","33","13870943994"},{"徐世绩","28","18062795496"},
			{"杜如晦","29","18600158798"},{"方孟傲","35","13836188388"},
			{"杜小月","19","15248779908"},{"余国荔","25","13238112922"},
			{"罗士信","29","15927271222"},{"林徽因","34","15108235082"},
			{"陈永华","39","13296693349"},{"萧漩语","34","15847006955"}};
		//创建JTable对象
		JTable jTable = new JTable(data,heads);
		int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		//创建滚动条面板对象,将JTable加入滚动条中,显示横向和垂直滚动条
		JScrollPane jsp=new JScrollPane(jTable,v,h);
		//将滚动面板添加至JFrame中
		this.add(jsp);
		this.setSize(450, 200);
		this.setVisible(true);
	}
}

Test类:

public class Test {
	public static void main(String[] args) {
		LoginView login = new LoginView();
		login.setVisible(true);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章