永发信息网

使用swing或awt控件实现用户登录功能,输入正确的用户名和密码后有正确的提示信息

答案:2  悬赏:0  手机版
解决时间 2021-03-20 06:50
  • 提问者网友:書生途
  • 2021-03-19 17:25
使用swing或awt控件实现用户登录功能,输入正确的用户名和密码后有正确的提示信息
最佳答案
  • 五星知识达人网友:夜余生
  • 2021-03-19 17:31
//这个绝对可以

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {

public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}

class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用户名:");
JLabel pwdLabel=new JLabel("密码:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("确定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陆");
setBounds(500, 200, 280, 220);
setResizable(false);
setVisible(true);
setLayout(null);
nameLabel.setBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
name.setBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用户名:"+name.getText());
System.out.println("密码:"+name.getText());
if("wyjwsj".equals(name.getText().trim())&&"12345".equals(password.getText().trim())){
this.dispose();
new MainFrm("用户界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用户名或密码错误,用户名为:wyjwsj,密码为:12345");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}

class MainFrm extends JFrame{
private JLabel info;

public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}参考资料:还有其他问题的话,给我发百度消息
全部回答
  • 1楼网友:雾月
  • 2021-03-19 19:06
package Login;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.awt.Color;

public class Login extends JFrame {
public Login() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
getContentPane().setBackground(Color.green);
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel1.setText("用户名:");
jLabel1.setBounds(new Rectangle(21, 24, 80, 35));
jButton1.setBounds(new Rectangle(59, 164, 88, 42));
jButton1.setText("OK");
jButton1.addActionListener(new Login_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(201, 164, 88, 42));
jButton2.setText("Cancel");
jButton2.addActionListener(new Login_jButton2_actionAdapter(this));
this.getContentPane().add(jLabel1);
Password.setBounds(new Rectangle(109, 76, 202, 36));
Name.setBounds(new Rectangle(109, 26, 203, 34));
this.getContentPane().add(jLabel2);
this.getContentPane().add(Password);
this.getContentPane().add(Name);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel2.setText("密 码:");
jLabel2.setBounds(new Rectangle(21, 81, 80, 35));
}

public static void main(String[] args) {
Login frame = new Login();
frame.setBounds(500, 500, 350, 280);
frame.setTitle("用户登录");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}

JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField Name = new JTextField();
JPasswordField Password = new JPasswordField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}

public void jButton1_actionPerformed(ActionEvent e) {
String tname = Name.getText().trim();
if (tname.equals("")) {
JOptionPane.showMessageDialog(this, "请输入用户名");
Name.requestFocus();
return;
}
String tpassword = Password.getText().trim();
if (tpassword.equals("")) {
JOptionPane.showMessageDialog(this, "请输入密码");
Password.requestFocus();
return;
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Students";
String sql = "select * from register where Name='" + tname + "'";
//加载注册驱动
try {
Class.forName(driver);
//获得连接
conn = DriverManager.getConnection(url, "sa", "root");
//创建一个Statement对象
st = conn.createStatement();
//确认信息
rs = st.executeQuery(sql);
if (rs.next()) {
String Name2 = rs.getString(1).trim();
String Password2 = rs.getString(2).trim();
if (tname.equals(Name2) && tpassword.equals(Password2)) {
JOptionPane.showMessageDialog(this, "登陆成功!");
} else {
if (tname.equals(Name2)) {
if (!tpassword.equals(Password2)) {
JOptionPane.showMessageDialog(this, "密码错误!");
Password.requestFocus();
}
} else {
JOptionPane.showMessageDialog(this, "用户名错误!");
Name.requestFocus();
}
}
} else {
JOptionPane.showMessageDialog(this, "没有该用户!");
Name.requestFocus();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
//关闭
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException ex2) {
ex2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex3) {
ex3.printStackTrace();
}
}
}

}
}

class Login_jButton1_actionAdapter implements ActionListener {
private Login adaptee;
Login_jButton1_actionAdapter(Login adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class Login_jButton2_actionAdapter implements ActionListener {
private Login adaptee;
Login_jButton2_actionAdapter(Login adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯