大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
mport java.awt.HeadlessException;
网站设计、网站建设的开发,更需要了解用户,从用户角度来建设网站,获得较好的用户体验。创新互联公司多年互联网经验,见的多,沟通容易、能帮助客户提出的运营建议。作为成都一家网络公司,打造的就是网站建设产品直销的概念。选择创新互联公司,不只是建站,我们把建站作为产品,不断的更新、完善,让每位来访用户感受到浩方产品的价值服务。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用户名:");
JLabel lbl2 = new JLabel("密 码:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登录");
JPanel pnl = new JPanel();
private int error = 0;
public MainFrame(String title) throws HeadlessException {
super(title);
init();
}
private void init() {
this.setResizable(false);
pwd.setEchoChar('*');
pnl.add(lbl1);
pnl.add(txt);
pnl.add(lbl2);
pnl.add(pwd);
pnl.add(btn);
this.getContentPane().add(pnl);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equals(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error 3){
JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"对不起,您不是合法用户");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("测试");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
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);
}
}
这是我自己做的一个管理系统的登录界面,代码虽然有点繁琐,不过简单易懂,你根据自己的需要进行修改吧。
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*
* @author mzhe
*
*/
class Loginfrm extends JDialog implements ActionListener
{
JLabel l_name,l_pass,l_error;
JTextField t_name;
JButton b_enter,b_cancle,b_clear;
JPanel pan1,pan2,pan3,pan4;
JPasswordField t_pass;
String sname,spass;
int sunit;
NetConn sql;
Statement sqll;
ResultSet rs;
Librarybox lbox;
int until=0;
Loginfrm(JFrame f,String s)
{
//界面布局
super(f,s);
l_name=new JLabel("名字:");
l_pass=new JLabel("密码:");
l_error=new JLabel("请输入用户名和密码登录");
t_name=new JTextField("",10);
t_pass=new JPasswordField("",10);
t_pass.setEchoChar('*');
b_enter=new JButton("确定");
b_enter.addActionListener(this);
b_cancle=new JButton("取消");
b_cancle.addActionListener(this);
b_clear=new JButton("清除");
b_clear.addActionListener(this);
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
pan4=new JPanel();
pan1.add(l_name);pan1.add(t_name);
pan2.add(l_pass);pan2.add(t_pass);
pan3.add(l_error);
pan4.add(b_enter);pan4.add(b_cancle);pan4.add(b_clear);
setLayout(new GridLayout(4,1));
add(pan1);add(pan2);add(pan3);add(pan4);
//建立数据库连接
sql=new NetConn();
//设置窗口大小
setSize(300,300);
setVisible(false);
//得到屏幕信息以便使框架居中显示
Dimension screeSize = Toolkit.getDefaultToolkit().getScreenSize();
int screeWidth=screeSize.width;
int screeHeight=screeSize.height;
//得到框架的大小信息
Dimension frameSize=this.getSize();
int x=(screeWidth-frameSize.width)/2;
int y=(screeHeight-frameSize.height)/2;
this.setLocation(x, y);
}
public void actionPerformed(ActionEvent e)
{
//单击确定按钮的事件处理程序
if(e.getSource()==b_enter)
{
//如果连续登录次数小于4
if(until=4)
{
//如果用户名或者密码为空,将显示提示信息
if(t_name.getText().equals("")||t_pass.getText().equals(""))
{
l_error.setText("用户名和密码不能为空");
}
else
{
try
{
sqll=sql.connect();
//根据用户名查询
rs=sqll.executeQuery("SELECT * FROM users where username="+"'"+t_name.getText()+"'");
//遍历查询得到的结果集
while(rs.next())
{
sname=rs.getString(2);
spass=rs.getString(3);
//得到登录用户的级别
sunit=Integer.parseInt(rs.getString(4));
//如果密码正确
if(t_pass.getText().equals(spass))
{
//判断用户的级别,根据不同的级别,显示不同的菜单
switch(sunit)
{
case 1:
{
l_error.setText("登录成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(true);
lbox.bookse.setEnabled(true);
lbox.bookth.setEnabled(true);
lbox.bookfo.setEnabled(true);
lbox.mi_system_manger.setEnabled(true);
lbox.mi_system_login.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
case 2:
{
l_error.setText("登录成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(false);
lbox.bookse.setEnabled(false);
lbox.bookth.setEnabled(true);
lbox.bookfo.setEnabled(false);
lbox.mi_system_manger.setEnabled(false);
lbox.mi_system_login.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
case 3:
{
l_error.setText("登录成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(true);
lbox.bookse.setEnabled(false);
lbox.bookth.setEnabled(false);
lbox.bookfo.setEnabled(true);
lbox.mi_system_manger.setEnabled(false);
lbox.mi_system_login.setEnabled(false);
lbox.fi_msglabel_user.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
}
}
else
{
l_error.setText("用户名或密码错误!");
until++;
}
}
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
}
//超出登录次数
else
{
l_error.setText("你已经超出登录次数");
t_name.setEnabled(false);
t_pass.setEnabled(false);
b_enter.setEnabled(false);
b_clear.setEnabled(false);
}
}
//单击清除按钮的事件处理程序
else if(e.getSource()==b_clear)
{
t_name.setText("");
t_pass.setText("");
l_error.setText("请输入用户名和密码登录");
}
//单击取消按钮的事件处理程序
else if(e.getSource()==b_cancle)
{
dispose();
}
}
}
如果你比较认可的话,请采纳吧,不给分也没关系。只希望对你有用。
你这个不用数据库真的是有点难搞
我写了个用集合存储的,你看看,能否帮上你
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;
}
}
//User 用户的基本信息,也是USERINFO表中的3个列
package
登陆判断;
public class User {
private String name;
private String loginname;
private String
loginpsw;
public String getName() {
return name;
}
public
void setName(String name) {
this.name = name;
}
public String
getLoginname() {
return loginname;
}
public void
setLoginname(String loginname) {
this.loginname =
loginname;
}
public String getLoginpsw() {
return
loginpsw;
}
public void setLoginpsw(String loginpsw)
{
this.loginpsw = loginpsw;
}
}
//用于注册和登陆的小小代码块
package
登陆判断;
import java.util.Scanner;
public class In {
public static User getUser(){
User u = new User();
Scanner sc =
new
Scanner(System.in);
System.out.println("请输入登陆名");
u.setLoginname(sc.nextLine());
System.out.println("请输入密码");
u.setLoginpsw(sc.nextLine());
return
u;
}
public static User registerUser(){
User u = new
User();
Scanner sc = new
Scanner(System.in);
System.out.println("请注册用户名名");
u.setName(sc.nextLine());
System.out.println("请注册登陆名");
u.setLoginname(sc.nextLine());
System.out.println("请注册密码");
u.setLoginpsw(sc.nextLine());
return
u;
}
}
//登陆时的检查,判断登陆名和密码是否正确,正确则返回 用户名
package 登陆判断;
import java.sql.Connection;
import java.sql.DriverManager;
import
java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
public class Check {
public static void checkUser(User u)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
String
url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user =
"scott";
String password = "tiger";
Connection conn =
null;
ResultSet rs = null;
PreparedStatement pst =
null;
String sql = "Select name from USERINFO where loginname = ? and
loginpsw = ? ";
try {
conn = DriverManager.getConnection(url, user,
password);
pst = conn.prepareStatement(sql);
pst.setString(1,
u.getLoginname());
pst.setString(2,u.getLoginpsw());
rs =
pst.executeQuery();
while(rs.next()){
u.setName(rs.getString(1));
}
}
catch (SQLException e) {
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
conn.close();
} catch
(SQLException e)
{
e.printStackTrace();
}
}
if(u.getName() == null ||
u.getName().isEmpty()){
System.out.println("登陆失败");
}else{
System.out.println("欢迎"+u.getName()+"登陆");
}
}
}
//注册界面,要判断 登陆名是否有重复,有重复则注册失败。有点小疑问,已用注释标出来
package 登陆判断;
import java.sql.Connection;
import java.sql.DriverManager;
import
java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
public class Register {
public static void registUser(User u){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
String
url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user =
"scott";
String password = "tiger";
Connection conn =
null;
String sql = "Insert Into USERINFO
values(?,?,?)";
PreparedStatement pst =
null;
//判断登陆名是否已经存在
Statement s = null;
ResultSet rs =
null;
String sql_ck = "Select LOGINNAME FROM USERINFO";
try
{
conn = DriverManager.getConnection(url, user, password);
pst =
conn.prepareStatement(sql);
pst.setString(1,
u.getName());
pst.setString(2, u.getLoginname());
pst.setString(3,
u.getLoginpsw());
//判断登陆名是否已经存在
s =
conn.createStatement();
rs =
s.executeQuery(sql_ck);
while(rs.next()){//为什么用 rs.getString(2)会提示
无效的索引呢
if( rs.getString("LOGINNAME").equals(u.getLoginname())
){
System.out.println("登陆名已经存在,注册失败");
break;
}else{
pst.executeUpdate();
System.out.println("注册成功");
break;
}
}
}
catch (SQLException e) {
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
s.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
//main界面//
//数据库中表的结构式这样
//name(用户名,用于显示),
//loginname(登录名,即登陆时输入的ID)
//loginpsw(登陆时输入的密码)
package
登陆判断;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int chos = 0;
Scanner sc = new
Scanner(System.in);
System.out.println("\t\t登陆请按1\t\t注册请按2");
chos =
sc.nextInt();
switch(chos){
case 1:
Check.checkUser(In.getUser());break;
case 2:
Register.registUser(In.registerUser());break;
default:
System.out.println("请输入正确的选择");break;
}
}
}
假设登陆Oracle的数据库,连接方式
String driver = "oracle.jdbc.OracleDriver";
Connection cn = null;
Class.forName(driver);
String url = "jdbc:oracle:thin:@localhost:1521:ORACLE";
String user = "system";
String pwd = "密码";
cn = DriverManager.getConnection(url,user,pwd);
Statement st = cn.createStatement();
String sql = "Insert into loginfo (id,username,user,type) values( "id="+你的id+"username="+user+",password="+pwd+",type="+你的type);
st.executeUpdate(sql);
st.closed;
cn.closed;