JAVA 图形界面编程:Swing 程序设计练习 管理系统登录窗体

//一个小练习

JAVA 图形界面编程:Swing 程序设计练习 管理系统登录窗体

 运行截图:

 

1. 程序有三个类组成,其中 LoginFrame 类用来定义登录窗体(使用绝对布局
或流式布局); MainFrame 类用来定义主窗体;测试类 Test ,创建 LoginFrame
体。
2. 当输入正确的用户名( admin )和密码( 123456 )并单击“登录”按钮时
打开主窗体,否则出现用户名或密码错误提示框。
代码:
package 管理系统登陆窗体;

import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;


class MainFrame extends JFrame {
	public MainFrame() {
	//	MainFrame() {	
	super(); // 继承父类的构造方法
	setTitle("主窗口");
	setBounds(200, 200, 400, 300); // 设置窗体的显示位置及大小
	getContentPane().setLayout(null); // 设置为不采用任何布局管理器
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	final JLabel label = new JLabel();
	label.setForeground(new Color(255, 0, 0));
	label.setFont(new Font("", Font.BOLD, 18));
	label.setText("欢迎进入主窗口");
	label.setBounds(100, 100, 180, 50); // 设置“企业人事管理系统”标签的
	//显示位置及大小
	getContentPane().add(label);
	} }


 class LoginFrame extends JFrame {
	private JTextField textField;
	private JPasswordField passwordField;
	private JLabel label, usernameLabel, passwordLabel;
	private JPanel jp;
	public LoginFrame() {
	//	LoginFrame() {	
	// 继承父类的构造方法
	super();
	// 设置窗体标题
	setTitle("登录窗口");
	// 设置窗体的显示位置及大小
	setBounds(100, 100, 300, 260);
	// 设置为不采用任何布局管理器
	setLayout(null);
	// 创建面板
	jp = new JPanel();
	// 设置面板位置及大小
	jp.setBounds(0, 0, 260, 240);
	// 窗体中添加面板
	jp.setLayout(null);
	Container contentPane = this.getContentPane();
	contentPane.add(jp);
	// 创建显示“企业人事管理系统”标签,并添加至面板
	label = new JLabel("企业人事管理系统");
	jp.add(label);
	// 设置“企业人事管理系统”标签的字体及颜色
	label.setForeground(new Color(255, 0, 0));
	label.setFont(new Font("", Font.BOLD, 18));
	// 显示位置及大小
	label.setBounds(50, 30, 170, 40);
	// 创建并向面板添加“用户名”标签
	usernameLabel = new JLabel("用户名:");
	jp.add(usernameLabel);
	// 设置“用户名”标签的显示位置及大小
	usernameLabel.setBounds(50, 80, 60, 15);
	// 创建并向面板添加“用户名”输入文本框
	textField = new JTextField();
	jp.add(textField);
	// 设置“用户名”文本框的显示位置及大小
	textField.setBounds(100, 80, 120, 20);
	// 创建“用户名”标签
	passwordLabel = new JLabel("密 码:");
	// 设置“密 码”标签的显示位置及大小
	passwordLabel.setBounds(50, 110, 60, 15);
	// 向面板添加“密 码”标签
	jp.add(passwordLabel);
	// 创建并向面板添加“密码”输入文本框
	passwordField = new JPasswordField();
	// 设置“密 码”密码框的显示位置及大小
	passwordField.setBounds(100, 110, 120, 20);
	jp.add(passwordField);
	// 创建“登录”按钮
	JButton landButton = new JButton("登录");
	// 设置“登录”按钮的显示位置及大小
	landButton.setBounds(60, 150, 70, 25);
	jp.add(landButton);
	// 添加“登录”按钮单击监听器,处理单击事件
	landButton.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent e) {
	
            if(textField.getText().trim().length()==0||new String(passwordField.getPassword()).trim().length()==0){
                JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
                return;
            }
            if(textField.getText().trim().equals("admin")&&new String(passwordField.getPassword()).trim().equals("123456"))
      
            // if(name.getText().trim().equals("mr")&&new String(password.getPassword()).trim().equals("mrsoft")){
            {
               // JOptionPane.showMessageDialog(null, "登录成功");
            	JOptionPane.showMessageDialog(null,"登录成功");
        
            	MainFrame m=new MainFrame();
            	m.setVisible(true);
        	
            }
            else{
                JOptionPane.showMessageDialog(null, "用户名或密码错误");
            }

		
		
		//JOptionPane.showMessageDialog(null, "用户名或密码错误");
	}
	});
	// 创建“退出”按钮
	JButton exitButton = new JButton("退出");
	// 设置“退出”按钮的显示位置及大小
	exitButton.setBounds(140, 150, 70, 25);
	jp.add(exitButton);
	// 添加“退出”按钮单击监听器,处理单击事件
	exitButton.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
	System.exit(0);
	}
	});
	} }





public class Jframe {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LoginFrame frame = new LoginFrame();
		// 设置窗体可见
		frame.setVisible(true);
		
	}

}

  • 11
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
以下是一个简单的系统管理员登录系统的图形界面Java代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class AdminLoginGUI extends JFrame implements ActionListener { private JLabel label_user, label_pwd; private JTextField field_user; private JPasswordField field_pwd; private JButton button_login, button_exit; public AdminLoginGUI() { // 设置窗口标题和大小 setTitle("系统管理员登录"); setSize(300, 200); setLocationRelativeTo(null); // 居中显示 setResizable(false); // 禁止调整窗口大小 // 创建标签和输入框 label_user = new JLabel("用户名:"); label_pwd = new JLabel("密 码:"); field_user = new JTextField(20); field_pwd = new JPasswordField(20); // 创建登录和退出按钮 button_login = new JButton("登录"); button_exit = new JButton("退出"); // 给按钮添加事件监听器 button_login.addActionListener(this); button_exit.addActionListener(this); // 创建面板 JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10)); panel.add(label_user); panel.add(field_user); panel.add(label_pwd); panel.add(field_pwd); panel.add(button_login); panel.add(button_exit); // 添加面板到窗口中 setContentPane(panel); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == button_login) { String username = field_user.getText(); String password = String.valueOf(field_pwd.getPassword()); if (username.equals("admin") && password.equals("123456")) { JOptionPane.showMessageDialog(this, "欢迎,系统管理员!"); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!", "登录失败", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == button_exit) { System.exit(0); } } public static void main(String[] args) { AdminLoginGUI gui = new AdminLoginGUI(); gui.setVisible(true); } } ``` 在上面的代码中,我们使用Java Swing创建了一个窗口,并在窗口中添加了用户名和密码的标签和输入框。然后,我们创建了登录和退出按钮,并给它们添加了事件监听器。在事件监听器中,我们通过getText()和getPassword()方法获取输入框中的用户名和密码,然后判断用户名和密码是否正确,并在登录成功或失败时弹出不同的提示框。最后,我们将面板添加到窗口中,并通过setVisible(true)方法显示窗口。运行该代码,就可以看到一个简单的系统管理员登录系统的图形界面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值