loginframe.java

来自「里面所含源码是本人平时做程序的一些实例」· Java 代码 · 共 98 行

JAVA
98
字号
package loginInterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginFrame extends JFrame implements ActionListener {
	JPanel contentPane;
	JLabel nameLabel=new JLabel();
	JLabel passwordLabel=new JLabel();
	JLabel userCategoryLabel=new JLabel();
	JTextField nameTextField=new JTextField();
	JPasswordField passwordField=new JPasswordField();
	JButton loginBtn=new JButton();
	JButton exitBtn=new JButton();
	JComboBox stateComboBox=new JComboBox();
	Font font=new Font("Dialog",0,15);
	
	public LoginFrame(){
		try{jbInit();}
		catch(Exception e){e.printStackTrace();}
	}
	private void jbInit() throws Exception{
		//取得窗口面板
		contentPane=(JPanel)this.getContentPane();
		//定义窗口面板的布局
		contentPane.setLayout(null);
		//定义窗口的大小和标题
		this.setSize(new Dimension(400,330));
		this.setTitle("登录模块");
		//定义标签的标题、字符大小和位置
		nameLabel.setText("用户名:");
		nameLabel.setFont(font);
		nameLabel.setBounds(new Rectangle(65,57,81,16));
		passwordLabel.setText("密码:");
		passwordLabel.setFont(font);
		passwordLabel.setBounds(new Rectangle(65,112,79,16));
		userCategoryLabel.setText("用户类型:");
		userCategoryLabel.setFont(font);
		userCategoryLabel.setBounds(new Rectangle(65,156,79,16));
		//定义编辑框的位置
		nameTextField.setBounds(new Rectangle(194,67,118,22));
		passwordField.setBounds(new Rectangle(194,112,118,22));
		//定义按钮的标题,动作字符串,字符大小,位置和加入动作接收器
		loginBtn.setText("登录");
		loginBtn.setActionCommand("login");
		loginBtn.setFont(font);
		loginBtn.setBounds(new Rectangle(65,224,109,25));
		loginBtn.addActionListener(this);
		exitBtn.setText("退出");
		exitBtn.setActionCommand("exit");
		exitBtn.setFont(font);
		exitBtn.setBounds(new Rectangle(203,224,109,25));
		exitBtn.addActionListener(this);
		//定义下拉列表框的内容,第1个显示项和位置
		stateComboBox.addItem("管理员");
		stateComboBox.addItem("普通用户");
		stateComboBox.setSelectedIndex(0);
		stateComboBox.setBounds(new Rectangle(194,158,118,22));
		//为面板加入各个控件
		contentPane.add(nameLabel,null);
		contentPane.add(nameTextField,null);
		contentPane.add(passwordLabel,null);
		contentPane.add(passwordField,null);
		contentPane.add(userCategoryLabel,null);
		contentPane.add(stateComboBox,null);
		contentPane.add(loginBtn,null);
		contentPane.add(exitBtn,null);
	}
		protected void processWindowEvent(WindowEvent e){
			if(e.getID()==WindowEvent.WINDOW_CLOSING){
				System.exit(0);
			}
		}
		//按钮单击事件处理代码
		public void actionPerformed(ActionEvent e){
			//取得按钮的动作字符串
			String actionCommand=e.getActionCommand().trim();
			//取得用户名
			String name=nameTextField.getText();
			//取得密码
			String password=new String(passwordField.getPassword());
			//取得用户类型
			int state=stateComboBox.getSelectedIndex();
			//创建返回的字符串
			String message="";
			if(actionCommand.equals("login")){
				Login login=new Login(name,password,state);
				message=login.checkLogin();
			//显示返回的信息
			JOptionPane.showMessageDialog(null, message);
			
			}
			if(actionCommand.equals("exit")){
				//清空内存
				System.exit(0);
			}
		}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?