Java程序设计——编写一个登录页面

 需要编写两个类

LonginFrame、MainFrame

LonginFrame类

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame {
	JLabel lbluserLogIn;
	JLabel lbluserName;
	JLabel lbluserPWD;
	JTextField txtName;
	JPasswordField pwdPwd;
	JButton btnSub;
	JButton btnReset;

	public LoginFrame() {
		lbluserLogIn = new JLabel();
		lbluserName = new JLabel();
		lbluserPWD = new JLabel();
		txtName = new JTextField();
		pwdPwd = new JPasswordField();
		btnSub = new JButton();
		btnReset = new JButton();
		userInit();
	}

	void userInit() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(290, 220);
		setResizable(false);
		setLocationRelativeTo(null);
		setTitle("登录");
		setVisible(true);
		setLayout(null);
		getContentPane().setBackground(Color.lightGray);
		lbluserLogIn.setText("用户登录");
		lbluserLogIn.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 20));
		lbluserLogIn.setForeground(Color.RED);
		lbluserName.setText("用户名:");
		lbluserPWD.setText("密码:");
		btnSub.setText("登录");
		btnReset.setText("重置");
		lbluserLogIn.setBounds(100, 15, 160, 30);
		lbluserName.setBounds(50, 55, 60, 20);
		lbluserPWD.setBounds(50, 85, 60, 25);
		txtName.setBounds(110, 55, 120, 20);
		pwdPwd.setBounds(110, 85, 120, 20);
		btnSub.setBounds(85, 120, 60, 20);
		btnSub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnsub_ActionEvent(e);
			}
		});
		btnReset.setBounds(155, 120, 60, 20);
		btnReset.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				btnReset_ActionEvent(e);
			}
		});
		add(lbluserLogIn);
		add(lbluserName);
		add(lbluserPWD);
		add(txtName);
		add(pwdPwd);
		add(btnSub);
		add(btnReset);
		}

	void btnsub_ActionEvent(ActionEvent e) {
		String name = txtName.getText();
		String pwd = String.valueOf(pwdPwd.getPassword());
		if (name.equals("")) {
			JOptionPane.showConfirmDialog(null, "账号不能为空", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		} else if (pwd.equals("")) {
			JOptionPane.showConfirmDialog(null, "密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		} else if (name.equals("abc") && pwd.equals("123")) {
			this.dispose(); //释放当前窗体。关闭登录窗体
			new MainFrame();
		} else {
			JOptionPane.showConfirmDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
	}

	
	private void MainFrame()
	{
		JFrame myWindow=new JFrame("其它");
		JRadioButton rbMan=new JRadioButton("练习");
		JRadioButton rbWonman=new JRadioButton("其它",true);
		ButtonGroup bg=new ButtonGroup();
		bg.add(rbMan);
		bg.add(rbWonman);
		JCheckBox[] jcb= {new JCheckBox("帮助"),new JCheckBox("退出")};

		
	}

	void btnReset_ActionEvent(ActionEvent e) {
		txtName.setText("");
		pwdPwd.setText("");
	}
	
	public static void main(String[] args) {
		LoginFrame lf = new LoginFrame();
	}
}

MainFrame类

import javax.swing.*;
import java.awt.event.*;
public class MainFrame  extends JFrame implements ActionListener
{
	
	JMenuBar bar;
	JMenu exMenu,otherMenu;
	JMenuItem helpMenuItem,exitMenuItem;
	
	MainFrame(){
		exMenu = new JMenu("练习");
		helpMenuItem = new JMenuItem("帮助");
		exitMenuItem =new JMenuItem("退出");
		otherMenu = new JMenu("其它");
		otherMenu.add(helpMenuItem);
		otherMenu.add(exitMenuItem);
		
		bar =new JMenuBar();
		bar.add(exMenu);
		bar.add(otherMenu);
		setJMenuBar(bar);
		
		helpMenuItem.addActionListener(this);
		exitMenuItem.addActionListener(this);
		
		setTitle("主题窗口");
		setBounds(100,50,420,380);
		validate();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}


	@Override
	public void actionPerformed(ActionEvent e)
	{
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
			if(e.getSource()==helpMenuItem)
					JOptionPane.showConfirmDialog(null, "你好,请查看网络资源!","帮助",JOptionPane.CLOSED_OPTION);
			else if(e.getSource()==exitMenuItem)
					System.exit(0);
	}

}

运行LonginFrame类

 在这里设置账号、密码

 

 之后在进入新界面,这个界面还需要进行设置才可以使用

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张謹礧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值