1. 程式人生 > >(筆記)圖形使用者介面:awt元件及swing元件 簡單例項

(筆記)圖形使用者介面:awt元件及swing元件 簡單例項

程式碼執行類:

package ch13.day0731.zy.zy01;
public class Start {
	public static void main(String[] args) {
		LoginFrm vLoginFrm=new LoginFrm();
		vLoginFrm.setVisible(true);	
	}
}

登入介面:

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class LoginFrm extends JFrame {
	//構造方法
	public LoginFrm() {
		this.initUI();
		this.binEvent();
	}
	//****宣告*****//
	//容器物件
	private JPanel container;
	//標籤
	private JLabel lblLoginName;
	private JLabel lblLoginPwd;
	//按鈕
	private JButton btnLogin;
	//文字
	private JTextField txtLoginName;
//	private JTextField txtLoginPwd;
	//文字加密顯示
	private JPasswordField txtLoginPass;
	
	//*******登入介面********//
	private void initUI() {
		//窗體寬高
		this.setSize(400,300);
		//設定標題
		this.setTitle("系統登入");
		//不允許最大化
		this.setResizable(false);
		
		//例項化容器物件
		container=new JPanel();
		//容器物件佈局設定為null,代表絕對佈局(根據座標)
		container.setLayout(null);
		//將容器物件新增到當前窗體
		this.add(container);
		
		//*********使用者名稱元件************//
		//初例化元件
		lblLoginName = new JLabel();
		//設定元件文字
		lblLoginName.setText("使用者名稱:");
		//設定元件的絕對位置:x/y/width/height
		lblLoginName.setBounds(50,50,80,50);
		//將上述元件放置到container容器物件裡
		container.add(lblLoginName);
		
		//初例化元件
		txtLoginName=new JTextField();
		//設定元件文字
		//txtLoginName.setText("admin");
		//設定元件的絕對位置:x/y/width/height
		txtLoginName.setBounds(130,60,180,30);
		//將上述元件放置到container容器物件裡
		container.add(txtLoginName);
		
		//*********登入按鈕************//
		//初例化元件
		btnLogin=new JButton();
		//設定元件文字
		btnLogin.setText("登入");
		//設定元件的絕對位置:x/y/width/height
		btnLogin.setBounds(130,150,80,30);
		//將上述元件放置到container容器物件裡
		container.add(btnLogin);
		
		//***********密碼元件**********//
		//初例化元件
		lblLoginPwd = new JLabel();
		//設定元件文字
		lblLoginPwd.setText("密碼:");
		//設定元件的絕對位置:x/y/width/height
		lblLoginPwd.setBounds(50,80,80,50);
		//將上述元件放置到container容器物件裡
		container.add(lblLoginPwd);
		
		//初例化元件
		txtLoginPass=new JPasswordField();
		//設定元件文字
		//txtLoginPass.setText("123");
		//設定元件的絕對位置:x/y/width/height
		txtLoginPass.setBounds(130,100,180,30);
		//設定加密符號
		txtLoginPass.setEchoChar('喵');
		//將上述元件放置到container容器物件裡
		container.add(txtLoginPass);
	}
	//*******事件繫結********//
	private void binEvent() {
		//點選右上角的關閉按鈕時關閉當前窗體
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//動作監聽
		btnLogin.addActionListener(e->{
			btnLogin_click(e);
		});
		
	}
	
	//*******系統主選單介面********//
	private void customload() {
		
	}
	
	//*******事件********//
	ManagerDao managerDao=new ManagerDao();
	private void btnLogin_click(ActionEvent e) {
		String vLoginName=txtLoginName.getText();
		String vLoginPass=txtLoginPass.getText();
		
		this.setTitle(txtLoginName.getText());
		System.out.println("登入...");
		
		//ManagerDao.java
		boolean vIsOk=managerDao.login(vLoginName, vLoginPass);
		
		if(vIsOk) {
			System.out.println("登入成功!");
			MainFrm vMainFrm=new MainFrm();
			this.setVisible(false);
			vMainFrm.setVisible(true);
		}else {
			System.out.println("登入失敗!");
		}	
	}	
}

主介面(二級頁面):

package ch13.day0731.zy.zy01;

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class MainFrm extends JFrame {
	//構造方法
	public MainFrm() {
		this.initUI();
		this.binEvent();
	}
	//****宣告*****//
	//容器物件
	private JPanel container;
	//標籤
	private JLabel lblLoginName;
	private JLabel lblLoginPwd;
	//按鈕
	private JButton btnLogin;
	//文字
	private JTextField txtLoginName;
//	private JTextField txtLoginPwd;
	//文字加密顯示
	private JPasswordField txtLoginPass;
	
	//*******登入介面********//
	private void initUI() {
		//窗體寬高
		this.setSize(800,600);
		//設定標題
		this.setTitle("學生資訊管理系統");
		//不允許最大化
		this.setResizable(false);
		
		//例項化容器物件
		container=new JPanel();
		//容器物件佈局設定為null,代表絕對佈局(根據座標)
		container.setLayout(null);
		//將容器物件新增到當前窗體
		this.add(container);
	
	}
	//*******事件繫結********//
	private void binEvent() {
		
		
	}
	
	//*******系統主選單介面********//
	private void customload() {
		
	}
}

使用者資訊儲存層:

package ch13.day0731.zy.zy01;
public class DataCenter {
	public static Manager[] manager=new Manager[100];
	/**
	 * 初始化物件的所有屬性
	 */
	static{
		
		for(int i=0;i<3;i++) {
			manager[i]=new Manager();
		}
		manager[0]=new Manager("admin","123");
		manager[1]=new Manager("user","123");
		manager[2]=new Manager("good","123");	
	}	
}

管理員類:

package ch13.day0731.zy.zy01;
public class Manager {
	private String userName;
	private String userPwd;
	
	//getter  setter
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	//構造方法
	public Manager() {}
	public Manager(String userName,String userPwd){
		this.setUserName(userName);
		this.setUserPwd(userPwd);
	}
}

管理員資訊操作層:

package ch13.day0731.zy.zy01;
public class ManagerDao {
	// 判斷賬密是否存在
	public boolean login(String pUserName, String pUserPwd) {
		boolean isRight = false;
		if(pUserName!=null&&pUserPwd!=null) {
			for (int i = 0; i < DataCenter.manager.length; i++) {
				if (DataCenter.manager[i] == null) {
					break;
				}
				isRight = pUserName.equals(DataCenter.manager[i].getUserName())
						&& pUserPwd.equals(DataCenter.manager[i].getUserPwd());
				if (isRight) {
					break;
				}
			}
		}		
		return isRight;
	}

	// 修改賬密
	public boolean change(String pUserName, String pNewPwd) {
		boolean isOk = false;
		
			for (int i = 0; i < DataCenter.manager.length; i++) {
				if (DataCenter.manager[i] == null) {
					break;
				}
				isOk = pUserName.equals(DataCenter.manager[i].getUserName());
				if (isOk) {
					DataCenter.manager[i].setUserPwd(pNewPwd);
					break;
				}
			}
		return isOk;
	}
}