如何采用java设置一个登陆界面

2025-10-15 07:37:54

1、1.打开编译软件:

小编采用的是eclipse软件,也可以采用其他的软件。

前提,小伙们要有一定的java基础,能看懂相应的类的知识。

如何采用java设置一个登陆界面

2、2.建立一个工程:

在自己的编译环境下建立一个java工程,小编的工程名,包名和类名都一样是“textpass”.

如何采用java设置一个登陆界面

1、1.给类布局

要建立这个类,要有属性和方法,本实例用到了三个个方法(构造方法,事件监听方法和main方法)和相应的属性,建立的框架代码如下所示:

public class textpass extends JFrame implements ActionListener {

private JPanel jp=new JPanel();

private JLabel[] jlArray={new JLabel("用户名"),

new JLabel("密 码"),new JLabel("")};

private JButton[] jbArray={new JButton("登陆"),

new JButton("清空")};

private JTextField jtxtName =new JTextField();

private JPasswordField jtxtPassword= new JPasswordField();

public textpass(){

}

public void actionPerformed(ActionEvent e){

}

public static void main(String[] args) {

}

}

如何采用java设置一个登陆界面

2、2.编写构造方法:

关于建立这个登陆界面的布局,事件的注册等,具体代码如下:

public textpass(){

jp.setLayout(null);

for(int i=0;i<2;i++){

jlArray[i].setBounds(30, 20+i*50, 80, 26);

jbArray[i].setBounds(50+i*110, 130, 80,26);

jp.add(jlArray[i]);

jp.add(jbArray[i]);

jbArray[i].addActionListener(this);

}

jtxtName.setBounds(80,20,180,30);

jp.add(jtxtName);

jtxtName.addActionListener(this);

jtxtPassword.setBounds(80,70,180,30);

jp.add(jtxtPassword);

jtxtPassword.setEchoChar('*');

jtxtPassword.addActionListener(this);

jlArray[2].setBounds(10, 180, 300, 30);

jp.add(jlArray[2]);

this.add(jp);

this.setTitle("登陆");

this.setResizable(false);

this.setBounds(100, 100, 300, 250);

this.setVisible(true);

}

如何采用java设置一个登陆界面

3、3.编写事件方法:

用于监听键盘和鼠标事件,并处理,其代码如下:

public void actionPerformed(ActionEvent e){

if(e.getSource()==jtxtName){

jtxtPassword.requestFocus();

}else if(e.getSource()==jbArray[1]){

jlArray[2].setText("");

jtxtName.setText("");

jtxtPassword.setText("");

jtxtName.requestFocus();

}else{

if(jtxtName.getText().equals("小明")&&String.valueOf

(jtxtPassword.getPassword()).equals("123")){

jlArray[2].setText("登陆成功");

}else{

jlArray[2].setText("登陆错误");

}

}

}

如何采用java设置一个登陆界面

4、4.编写main方法

main方法时整个程序执行的初始点,代码如下:

public static void main(String[] args) {

new textpass();

}

如何采用java设置一个登陆界面

5、5.注意事项:

这样整个程序是写完了,但是有时候还是会报错,看看大家是不是忘了导入相应的类了,这里贴出所需要的类。

6、6.完整代码:

为了便于调试,下面小编给出该工程的完整代码:

package textpass;

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.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class textpass extends JFrame implements ActionListener {

private JPanel jp=new JPanel();

private JLabel[] jlArray={new JLabel("用户名"),

new JLabel("密 码"),new JLabel("")};

private JButton[] jbArray={new JButton("登陆"),

new JButton("清空")};

private JTextField jtxtName =new JTextField();

private JPasswordField jtxtPassword= new JPasswordField();

public textpass(){

jp.setLayout(null);

for(int i=0;i<2;i++){

jlArray[i].setBounds(30, 20+i*50, 80, 26);

jbArray[i].setBounds(50+i*110, 130, 80,26);

jp.add(jlArray[i]);

jp.add(jbArray[i]);

jbArray[i].addActionListener(this);

}

jtxtName.setBounds(80,20,180,30);

jp.add(jtxtName);

jtxtName.addActionListener(this);

jtxtPassword.setBounds(80,70,180,30);

jp.add(jtxtPassword);

jtxtPassword.setEchoChar('*');

jtxtPassword.addActionListener(this);

jlArray[2].setBounds(10, 180, 300, 30);

jp.add(jlArray[2]);

this.add(jp);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setTitle("登陆");

this.setResizable(false);

this.setBounds(100, 100, 300, 250);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==jtxtName){

jtxtPassword.requestFocus();

}else if(e.getSource()==jbArray[1]){

jlArray[2].setText("");

jtxtName.setText("");

jtxtPassword.setText("");

jtxtName.requestFocus();

}else{

if(jtxtName.getText().equals("小明")&&String.valueOf(jtxtPassword.getPassword()).equals("123")){

jlArray[2].setText("登陆成功");

}else{

jlArray[2].setText("登陆错误");

}

}

}

public static void main(String[] args) {

new textpass();

}

}

1、1.运行程序:

编译并运行会出现先下面的界面。

如何采用java设置一个登陆界面

2、2.操作演示

按照我们的登陆时的习惯填入相关的用户名和密码试试吧。

如何采用java设置一个登陆界面

3、3.总结:

大家在回味一下吧,应该很有用的,终于敲完了,纯手打,累死了,给点个赞呗。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢