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窗体关闭---窗口程序结束
}
}
java_登录
最新推荐文章于 2024-07-04 22:55:50 发布