java_登录

在这里插入图片描述

package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
public class app extends JFrame{
	public static void main(String[] args){
		app mainWindow=new app();
		mainWindow.run();
	}

	public void run(){
		//创建Java_Label+图标
		ImageIcon icon_logo = new ImageIcon("./img/logo.png");//图  在test  目录下
		JLabel label1 = new JLabel(icon_logo);//logo图放到标签上
		label1.setBounds(150, 30, 200, 50);//设置界限:位置(150,30)和标签大小(100,50)
 		
		JLabel LBl_Username = new JLabel("用  户:");//创建用户名 java_Label
		LBl_Username.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 14));//设置Java_Label字体和对齐方式和大小
		LBl_Username.setLocation(141, 130);//设置Java_Label的位置和大小的两种方式
		LBl_Username.setSize(60, 25);
		//创建密码标签界限
		JLabel LBl_Password = new JLabel("密  码:");
		LBl_Password.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 14));
		LBl_Password.setBounds(141, 180, 60, 25);
		
		//声明+创建输入框位置
		JPasswordField passwordField = new JPasswordField();//创建Java_PasswordField,Java_TextField
		passwordField.setBounds(210, 180, 100, 25);//设置位置和宽度、高度
		JTextField textField_user = new JTextField();//创建Java_PasswordField,Java_TextField
		textField_user.setBounds(210, 140, 100, 25);//设置位置和宽度、高度
		textField_user.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));//设置输入的字体
		
		//声明+创建按钮java_Button登录和取消
		JButton Login_Btn = new JButton("登录");
		JButton Cancel_Btn = new JButton("取消");
		//设置java_Button位置和大小
		Login_Btn.setBounds(140, 220, 60, 25);
		Cancel_Btn.setBounds(250, 220, 60, 25);
		
		
		//声明布局管理器
		Container container = this.getContentPane();
		
		//组件加入布局管理器
		container.add(label1);
		container.add(LBl_Username);
		container.add(LBl_Password);
		container.add(passwordField);
		container.add(textField_user);
		container.add(Login_Btn);
		container.add(Cancel_Btn);
	
		//按钮附加功能:监听事件
		
		//用户输入框:最大输入8位
		textField_user.addKeyListener(new KeyAdapter() {
 
			@Override
			public void keyTyped(KeyEvent e_key) {
				if(e_key.getKeyChar() == KeyEvent.VK_BACK_SPACE){
					e_key.isActionKey();
				}else if(textField_user.getText().length() >9){
					e_key.consume();//消费
				}
			}		
		});	
		
		//textField_user监听---点击Enter后执行---密码框获得焦点 
		textField_user.addActionListener(new ActionListener() {//
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//光标切换到passwordField框
				passwordField.requestFocus();				
			}
		});
		
		//passwordField监听---点击enter---登录按钮执行单击命令
		passwordField.addActionListener(new ActionListener() {
			
			@Override//重新动作时间
			public void actionPerformed(ActionEvent e) {
				//当鼠标光标在密码框时 点击登录
				Login_Btn.doClick();
			}
		});
		
		//当登录按钮被点击后 将进行登录操作
		Login_Btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				JFrame frame_main = new JFrame();//声明新窗口
			
				JLabel LBl_hello = new JLabel("Welcome  hello");//
				LBl_hello.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 20));//设置Java_Label字体和对齐方式和大小
				LBl_hello.setLocation(200, 200);//设置Java_Label的位置和大小的两种方式
				LBl_hello.setSize(100, 25);
//								
				Container container = frame_main.getContentPane();
				//组件加入布局管理器
				container.add(LBl_hello);
				
				 JPanel root = new JPanel();
			      root.add(new JButton("我是JButton"));
			      root.add(new JToggleButton("我是JToggleButton"));
			      root.add(new JLabel("我是JLabel"));
			      root.add(new JCheckBox("我是JCheckBox"));
			      root.add(new JRadioButton("我是JRadioButton"));
			      root.add(new JTextField("我是JTextField"));
			      root.add(new JPasswordField("我是JPasswordField"));
			      root.add(new JTextArea("我是JTextArea"));
			      add(root);
			      
			      setSize(400, 300);
			      setDefaultCloseOperation(EXIT_ON_CLOSE);
			      setVisible(true);					
				frame_main.setSize(800, 800);
				frame_main.setVisible(true);
				frame_main.setTitle("主页面");
				frame_main.setLocationRelativeTo(null);
				frame_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出关闭
			}
		});
		
		//设置窗体Logo
		Image image=Toolkit.getDefaultToolkit().createImage("./img/icon.png");
		this.setIconImage(image);
		//设置窗体的属性
		this.setLayout(null);//设置空布局管理器
		this.setVisible(true);//窗体可见
		this.setTitle("登录");//窗体标题
		this.setSize(500, 400);//窗体高、宽
		this.setLocationRelativeTo(null);//居中显示
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置Jframe窗体关闭---窗口程序结束
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值