十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//这个是我写的,里面有连接数据库的部分。你可以拿去参考一下
创新互联专注于萝北网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供萝北营销型网站建设,萝北网站制作、萝北网页设计、萝北网站官网定制、微信平台小程序开发服务,打造萝北网络公司原创品牌,更为您提供萝北网站排名全网营销落地服务。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用户名:");
JLabel lbl2 = new JLabel("密码:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("确定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
this.setTitle("登陆");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 数据库名为Car_zl,密码为1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名为user,user_ID和User_ps是存放用户名和密码的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陆成功!");
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
什么都不说了 直接给你代码吧
package com.moliying.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登录");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");
public Login() {
frame.setSize(300, 200);
frame.setBounds(450, 300, 300, 200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
// 顶部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系统管理员登录"));
c.add(titlePanel, "North");
// 中部表单
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel a1 = new JLabel("用户名:");
a1.setBounds(50, 20, 50, 20);
JLabel a2 = new JLabel("密 码:");
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(110, 20, 120, 20);
password.setBounds(110, 60, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel, "Center");
// 底部按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel, "South");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(username.getText().toString());
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
}
public static void main(String[] args) {
// new Login();
String ss = "abbabbbaabbbccba";
System.out.println(ss.split("b").length);
}
}
String username = "",password = "",passwordagain = ""; // 定义用户名和密码
将该变量等于为全局变量 或局部变量即可
你这个不用数据库真的是有点难搞
我写了个用集合存储的,你看看,能否帮上你
java.util.ListString list = new ArrayListString();
list.add("qq=123");//存储的时候用(用户名=密码)的形式
list.add("ww=456");
String username = "qq";
String password = "123";
for (int i = 0; i list.size(); i++) {
String num = username +"="+password;
if(num.equals(list.get(i))){
System.out.println("登录成功");
break;
}
}