java实现简单登录界面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现简单登录界面相关的知识,希望对你有一定的参考价值。

这是我的代码 在监听器那里有错。那位高手帮忙修改一下嘛!界面要求:有个用户 密码输入 还有确定 重置按钮(重置按钮代码还没写)。谢谢咯。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Login
public static void main(String []args)
LoginFrame frame=new LoginFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();



class LoginFrame extends JFrame
public LoginFrame()
setTitle("Login");
setSize(WIDTH,HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);

private static final int WIDTH=300;
private static final int HEIGHT=150;


class ButtonPanel extends JPanel
//create consturctor
public ButtonPanel()
setLayout(new GridLayout(3,2));
JLabel label1=new JLabel("请输入用户名:");
JTextField userName=new JTextField(10);
JLabel label2=new JLabel("请输入密码:");
JPasswordField myPassword = new JPasswordField();
JButton button1=new JButton("确认");
JButton button2=new JButton("取消");
add(label1);
add(userName);
add(label2);
add(myPassword);

//注册事件监听器
button1.addActionListener(new LoginAction());
add(button1);
button2.addActionListener(new LoginAction());
add(button2);


private class LoginAction extends JFrame implements ActionListener
public void actionPerformed(ActionEvent event)
JFrame jframe=new JFrame();
String a=userName.getText();
char[] b=myPassword.getPassword();
String getb=new String(b);
if(a==CHECKUSERNAME&&getb==CHECKPASSWORD)
JOptionPane.showMessageDialog(jframe,"登录成功");

else
JOptionPane.showMessageDialog(jframe,"登录失败");




public static final String CHECKUSERNAME = "a";
public static final String CHECKPASSWORD = "a";
private JPasswordField myPassword;
private JTextField userName;

参考技术A 自己写的比较规范的代码,都有注释:

import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框
import java.awt.Font;//字体
import java.awt.Color;//颜色
import javax.swing.JPasswordField;//密码框
import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import javax.swing.JOptionPane;//消息窗口

public class UserLogIn extends JFrame
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;

public UserLogIn()
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();


public void userInit()
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
this.setSize(300,200);//设置框架大小为长300,宽200
this.setResizable(false);//设置框架不可以改变大小
this.setTitle("用户登录");//设置框架标题
this.pnluser.setLayout(null);//设置面板布局管理
this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
this.lbluserLogIn.setText("用户登录");//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口

public void actionPerformed(ActionEvent e)
btnsub_ActionEvent(e);


);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口

public void actionPerformed(ActionEvent e)
btnreset_ActionEvent(e);


);
this.pnluser.add(lbluserLogIn);//加载标签到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//加载面板到框架
this.setVisible(true);//设置框架可显


public void btnsub_ActionEvent(ActionEvent e)
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals(""))
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
else if (pwd.equals(""))
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
else if(true)
this.dispose();
else
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
return;



public void btnreset_ActionEvent(ActionEvent e)
txtName.setText("");
pwdPwd.setText("");


public static void main(String[] args)
new UserLogIn();

本回答被提问者采纳
参考技术B import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import java.awt.Button;
import java.awt.Panel;
import java.lang.String;
import javax.swing.JOptionPane;
public class QQ extends Frame implements ActionListener
Label Lname ,Lpassword; //定义用户名和密码的标签
TextField Tname; //定义用户名的文本框
JPasswordField Tpassword; //定义密码的文本框。注:使用JPasswordField的好处就是输入的密码是以****显示给用户的
Panel panel1=new Panel(); //定义面板
Button Load,login; //定义登陆和注册的按钮
boolean b=false;
File file1; //定义记录用户注册的用户名和密码的文件夹
Writer writer;
String []Usersname=new String[1024]; //定义记录用户名的数组
String []Userspassword=new String[1024];//定义记录密码的数组
public QQ()
Lname=new Label ("用户名");Lname.setBackground(Color.red);//设置标签的背景颜色
Lpassword=new Label ("密码");Lpassword.setBackground(Color.red);//设置标签的背景颜色
Tname=new TextField (10);
Tpassword=new JPasswordField (10);
Load=new Button("登陆");Load.setBackground(Color.lightGray);
login=new Button("注册");login.setBackground(Color.lightGray);
add(panel1,BorderLayout.CENTER);
setLayout(new CardLayout(3,2));
panel1.add(Lname);
panel1.add(Tname);
panel1.add(Lpassword);
panel1.add(Tpassword);
panel1.add(Load);Load.addActionListener(this);
panel1.add(login);login.addActionListener(this);


public void actionPerformed(ActionEvent e)
// 用户登录事件处理
if (e.getSource() == Load)
String name1 = Tname.getText();
String password1 = Tpassword.getText();
boolean flag = false;
int index = 0;
for (int i = 0; i < Usersname.length; i++)
if (name1.equals(Usersname[i]))
flag = true;
index = i;
break;


if (flag && password1.equals(Userspassword[index]))
//处理用户登录成功后的反馈信息
JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "对不起您的用户名或密码错误!", "错误",JOptionPane.ERROR_MESSAGE);



// 用户注册模块的事件处理
if (e.getSource() == login)
String NCmp = Tname.getText();
String PCmp = Tpassword.getText();

boolean flag = false;
for (int i = 0; i < Usersname.length; i++)
if (NCmp.equals(Usersname[i]))
flag = true;
break;


if (flag)
JOptionPane.showMessageDialog(null, "对不起您的用户名已经注册!", "错误", JOptionPane.ERROR_MESSAGE);
else
int index = 0;
JOptionPane.showMessageDialog(null, "注册成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
for (int i = 0; i < Usersname.length; i++)
if (Usersname[i] == null)
Usersname[i] = NCmp;
index = i;
break;


Userspassword[index] = PCmp;

try
file1=new File("Student.dat");
FileWriter fw=new FileWriter(file1,true);
fw.write("用户名"+"\t\t"+"密码"+"\n");
fw.write(NCmp+"\t\t"+PCmp+"\n");
fw.close();
catch (IOException e1)
e1.printStackTrace();




public static void main(String[] args)
// TODO Auto-generated method stub
Frame Fstudent=new QQ();
Fstudent.setSize(200,200);
Fstudent.setTitle("QQ仿真系统");

// 关闭窗口
Fstudent.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);
Fstudent.setVisible(true);



参考技术C 你的代码我不想看了- -!
给你我写的参考参考,我是不知道你干嘛要在监听器里写JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame

private static final String USER = "Freedom";
private static final String PASSWORD = "Destiny";

private JTextField user;
private JPasswordField password;

public LoginFrame()
user = new JTextField(15);
password = new JPasswordField(15);
password.setEchoChar('*');

JPanel p1 = new JPanel();
p1.add(new JLabel("User:"));
p1.add(user);
p1.add(new JLabel("PW:"));
p1.add(password);

add(p1,BorderLayout.CENTER);

JPanel p2 = new JPanel();
JButton button = new JButton("OK");
button.addActionListener(new ActionListener()//登录监听器

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
if(user.getText().equals(LoginFrame.USER) &&
password.getText().equals(LoginFrame.PASSWORD))
JOptionPane.showMessageDialog(LoginFrame.this, "Login Success!");
else
JOptionPane.showMessageDialog(LoginFrame.this, "Login Fail!");


);
p2.add(button);

button = new JButton("Exit");
button.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e)
System.exit(0);

);
p2.add(button);

add(p2,BorderLayout.SOUTH);

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

public static void main(String[] args)
new LoginFrame();


java swing中登录界面验证码的实现。

    public class ValidCode extends JComponent implements MouseListener   

    private String code;  

    private int width, height = 40;  

    private int codeLength = 4;  

    private Random random = new Random();  

    public ValidCode()   

    width = this.codeLength * 16 + (this.codeLength - 1) * 10;  

    setPreferredSize(new Dimension(width, height));  

    setSize(width, height);  

    this.addMouseListener(this);  

    setToolTipText("点击可以更换验证码");  

      

    public int getCodeLength()   

    return codeLength;  

      

    /* 

    设置验证码文字的长度 

    */  

    public void setCodeLength(int codeLength)   

    if(codeLength < 4)   

    this.codeLength = 4;  

    else   

    this.codeLength = codeLength;  

      

      

    public String getCode()   

    return code;  

      

    /* 

    产生随机的颜色 

    */  

    public Color getRandColor(int min, int max)   

    if (min > 255)  

    min = 255;  

    if (max > 255)  

    max = 255;  

    int red = random.nextInt(max - min) + min;  

    int green = random.nextInt(max - min) + min;  

    int blue = random.nextInt(max - min) + min;  

    return new Color(red, green, blue);  

      

    /* 

    设置验证码具体的字母是什么 

    */  

    protected String generateCode()   

    char[] codes = new char[this.codeLength];  

    for (int i = 0, len = codes.length; i < len; i++)   

    if (random.nextBoolean())   

    codes[i] = (char) (random.nextInt(26) + 65);  

    else   

    codes[i] = (char) (random.nextInt(26) + 97);  

      

      

    this.code = new String(codes);  

    return this.code;  

      

    @Override  

    protected void paintComponent(Graphics g)   

    super.paintComponent(g);  

    if(this.code == null || this.code.length() != this.codeLength)   

    this.code = generateCode();  

      

    width = this.codeLength * 16 + (this.codeLength - 1) * 10;  

    super.setSize(width, height);  

    super.setPreferredSize(new Dimension(width, height));  

    Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);  

    g.setFont(mFont);  

    //绘制出验证码的背景的矩形轮廓  

    Graphics2D g2d = (Graphics2D) g;  

    g2d.setColor(getRandColor(200, 250));  

    g2d.fillRect(0, 0, width, height);  

    g2d.setColor(getRandColor(180, 200));  

    g2d.drawRect(0, 0, width - 1, height - 1);  

    //绘制出验证码背景的线  

    int i = 0, len = 150;  

    for (; i < len; i++)   

    int x = random.nextInt(width - 1);  

    int y = random.nextInt(height - 1);  

    int x1 = random.nextInt(width - 10) + 10;  

    int y1 = random.nextInt(height - 4) + 4;  

    g2d.setColor(getRandColor(180, 200));  

    g2d.drawLine(x, y, x1, y1);  

      

    /*i = 0; len = 300; 

    for (; i < len; i++)  

    int x = random.nextInt(width); 

    int y = random.nextInt(height); 

    g2d.setColor(getRandColor(150, 180)); 

    g2d.drawRect(x, y, 0, 0); 

    */  

    //绘制出验证码的具体字母  

    i = 0; len = this.codeLength;  

    FontMetrics fm = g2d.getFontMetrics();  

    int base = (height - fm.getHeight())/2 + fm.getAscent();  

    for(;i<len;i++)   

    int b = random.nextBoolean() ? 1 : -1;  

    g2d.rotate(random.nextInt(10)*0.01*b);  

    g2d.setColor(getRandColor(20, 130));  

    g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);  

      

      

    //下一个验证码  

    public void nextCode()   

    generateCode();  

    repaint();  

      

    @Override  

    public void mouseClicked(MouseEvent e)   

    nextCode();  

      

    @Override  

    public void mousePressed(MouseEvent e)   

    // TODO Auto-generated method stub  

      

    @Override  

    public void mouseReleased(MouseEvent e)   

    // TODO Auto-generated method stub  

      

    @Override  

    public void mouseEntered(MouseEvent e)   

    // TODO Auto-generated method stub  

      

    @Override  

    public void mouseExited(MouseEvent e)   

    // TODO Auto-generated method stub  

      

      

参考技术A 上面的骗人,财富值扣了,结果东西还不存在~~~~
呜~

以上是关于java实现简单登录界面的主要内容,如果未能解决你的问题,请参考以下文章

JAVA语言实现简单登录界面

用java设计一个“掌上微博”的用户登录界面,要求界面友好,使用方便,如下图

java web实现简单的用户登录需要哪些技术

用Django设计一个简单的注册和登录界面

java swing中登录界面验证码的实现。

JSP内置对象的cookie和session实现简单登录界面