返回本章節
返回作業目錄
需求說明:
使用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);
}
}