java窗口建立

package meirizongjie;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import meirizongjie.Connect;
public class Base extends JFrame{

public Base(){
super("Load");
setLayout(null);
setSize(400,400);

//设置标签
JLabel username = new JLabel();
username.setText("用户名:"); //设置标签的名字
username.setBounds(100,150,60,30); //设置标签的在窗体中的坐标位置和大小
JLabel password = new JLabel();
password.setText("密码:");
password.setBounds(100,200,60,30);

//设置文本框
JTextField user_name = new JTextField(20);
user_name.setBounds(170,150,100,20);
JPasswordField pass_word = new JPasswordField(20);
pass_word.setBounds(170,200,100,20);

//设置按钮
JButton btnLoad = new JButton("登录");
btnLoad.setBounds(100,250,60,30);

JButton btnReset = new JButton("注册");
btnReset.setBounds(170,250,60,30);



//设置按钮监听事件
ActionListener loadAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name = user_name.getText();
String pass = pass_word.getText();
if("admin".equals(name)&&"123456".equals(pass)){
JOptionPane.showMessageDialog(null, "管理员登录成功","成功",JOptionPane.PLAIN_MESSAGE);

}else{
JOptionPane.showMessageDialog(null, "登录失败!请重试","失败",JOptionPane.ERROR_MESSAGE);
}
}
};

ActionListener resetAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
user_name.setText("");
pass_word.setText("");
}

};

add(username);
add(password);
add(user_name);
add(pass_word);
add(btnLoad);
add(btnReset);
btnLoad.addActionListener(loadAction);
btnReset.addActionListener(resetAction);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub

new Base();
}

}

 

posted @ 2023-02-21 20:50  AsrielDream  阅读(46)  评论(0)    收藏  举报