十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
下面是一个简单的登录示例:
成都创新互联公司是一家专业提供长春企业网站建设,专注与成都网站建设、成都做网站、H5响应式网站、小程序制作等业务。10年已为长春众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
代码复制展示:
public class ATMLogin {
public static void main(String[] args) {
// 设置用户名和密码
String username = "gqk";
String password = "520";
// 最多可以登录 3 次
for (int i = 0; i 3; i++) {
// 读取用户输入的用户名和密码
Scanner in = new Scanner(System.in);
System.out.print("请输入用户名:");
String inputUsername = in.nextLine();
System.out.print("请输入密码:");
String inputPassword = in.nextLine();
// 检查用户名和密码是否正确
if (inputUsername.equals(username) inputPassword.equals(password)) {
System.out.println("欢迎" + username + "登录!");
break; // 登录成功,退出循环
} else {
System.out.println("用户名或密码错误,请重新输入!");
}
}
// 如果 3 次登录都失败,则提示用户
System.out.println("卡片已经被锁,请联系客服人员!");
}
}
回答不易望请采纳
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.*;public class Login extends JFrame{
JLabel user,passwd;
JTextField userput;
JPasswordField passput;
JButton denglu,tuichu;
public Login(){
super("用户登录");
Container c=getContentPane();
c.setLayout(null);
Font f=new Font("宋体",Font.PLAIN,12);
user=new JLabel("账号");
passwd=new JLabel("密码");
userput=new JTextField();
passput=new JPasswordField();
denglu=new JButton("登录");
denglu.setFont(f);
denglu.addActionListener(new NewAction());
tuichu=new JButton("退出");
tuichu.setFont(f);
tuichu.addActionListener(new NewAction());
user.setBounds(50,50,60,20);
userput.setBounds(110,50,150,20);
passwd.setBounds(50,80,60,20);
passput.setBounds(110,80,150,20);
denglu.setBounds(50,160,60,30);
tuichu.setBounds(200,160,60,30);
c.add(user);
c.add(userput);
c.add(passwd);
c.add(passput);
c.add(denglu);
c.add(tuichu);
setSize(350, 300);
setVisible(true);
}
class NewAction implements ActionListener{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CDM";
String user="sa";
String passwd="394513265";
java.sql.Connection con;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con= DriverManager.getConnection(url,user,passwd);
}catch(Exception ep){
JOptionPane.showMessageDialog(null, "加载驱动失败!");
}
if(e.getSource()==denglu){
Find();
}
if(e.getSource()==tuichu){
dispose();
}
} public void Find(){
String lk="select * from login";
try{
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(lk);
while(rs.next()){
if(rs.getString(1).equals(userput.getText()) rs.getString(2).equals(passput.getText()))
new MainClient();
else
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
rs.close();
}catch(SQLException p){
JOptionPane.showMessageDialog(null, p.getMessage());
}
}
}
public static void main(String[] args) {
Login l=new Login();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
步骤就是建个工程 然后建个class
import java.awt.*; \x0d\x0aimport javax.swing.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0aclass LoginFrm extends JFrame implements ActionListener \x0d\x0a{ \x0d\x0aJLabel lbl1=new JLabel("用户名"); \x0d\x0aJLabel lbl2=new JLabel("密码"); \x0d\x0aJTextField txt=new JTextField(15); \x0d\x0aJPasswordField pf=new JPasswordField(); \x0d\x0aJButton btn1=new JButton("确定"); \x0d\x0aJButton btn2=new JButton("取消"); \x0d\x0a\x0d\x0apublic LoginFrm() \x0d\x0a{ \x0d\x0athis.setTitle("登陆"); \x0d\x0aJPanel jp=(JPanel)this.getContentPane(); \x0d\x0ajp.setLayout(new GridLayout(3,2,10,10)); \x0d\x0ajp.add(lbl1);jp.add(txt); \x0d\x0ajp.add(lbl2);jp.add(pf); \x0d\x0ajp.add(btn1);jp.add(btn2); \x0d\x0abtn1.addActionListener(this); \x0d\x0abtn2.addActionListener(this); \x0d\x0a} \x0d\x0a\x0d\x0apublic void actionPerformed(ActionEvent ae) \x0d\x0a{ \x0d\x0aif(ae.getSource()==btn1) \x0d\x0a{ \x0d\x0atry \x0d\x0a{ \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); \x0d\x0aConnection con=DriverManager.getConnection("jdbc:odbc:MyDB","",""); \x0d\x0aStatement cmd=con.createStatement(); \x0d\x0aResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'"); \x0d\x0aif(rs.next()) \x0d\x0a{ \x0d\x0aJOptionPane.showMessageDialog(null,"登陆成功!"); \x0d\x0a} \x0d\x0aelse \x0d\x0aJOptionPane.showMessageDialog(null,"用户名或密码错误!"); \x0d\x0a} catch(Exception ex){} \x0d\x0a\x0d\x0aif(ae.getSource()==btn2) \x0d\x0a{ \x0d\x0atxt.setText(""); \x0d\x0apf.setText(""); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0apublic static void main(String arg[]) \x0d\x0a{ \x0d\x0aJFrame.setDefaultLookAndFeelDecorated(true); \x0d\x0aLoginFrm frm=new LoginFrm(); \x0d\x0afrm.setSize(400,200); \x0d\x0afrm.setVisible(true); \x0d\x0a} \x0d\x0a}
你要先学会截图哦,你发的看不清楚,重新写了一个你参考参考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Day30A extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
private ButtonGroup btg;
private JRadioButton jr1,jr2;
Day30A(){
this.setTitle("注册账户");
this.setLayout(new GridLayout(7,1));
this.setSize(300,280);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
String str="卡片类型1,卡片类型2,卡片类型3,卡片类型4,卡片类型5";
jcb=new JComboBox(str.split(","));
labelId=new JLabel("账号: ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密码: ");
labelMoney=new JLabel("开户金额:");
labelSelect=new JLabel("存款类型:");
labelCar=new JLabel("卡片类型:");
addFun1();
addFun2();
}
private void addFun2() {
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
}
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
jp1.add(new JTextField(15));
jp2=new JPanel();
jp2.add(labelName);
jp2.add(new JTextField(15));
jp3=new JPanel();
jp3.add(labelPass);
jp3.add(new JTextField(15));
jp4=new JPanel();
jp4.add(labelMoney);
jp4.add(new JTextField(13));
jp5=new JPanel();
jp5.add(labelSelect);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
jr2=new JRadioButton("活期",true);
btg.add(jr1);
btg.add(jr2);
jp5.add(jr1);
jp5.add(jr2);
jp6=new JPanel();
jp6.add(labelCar);
jp6.add(jcb);
jp7=new JPanel();
jp7.add(new JButton("确定"));
jp7.add(new JButton("取消"));
}
public static void main(String[] args) {
new Day30A();
}
}