用JAVA语言编程实现一个用户登录窗口

2024-11-02 05:18:19
推荐回答(3个)
回答1:

方法一:
采用JOptionPane中的一个非常有用的静态方法 showOptionPane();
源码如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
public class Login1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
Object[] options = {"登录","取消"};
int i = JOptionPane.showOptionDialog(null,createLoginPanel(),"登录信息",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if(i==0) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") && !password.equals("")) {
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.show();
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
else System.exit(0);
}
static JPanel createLoginPanel() {
JPanel ret = new JPanel();

JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.RIGHT));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.RIGHT));
passwordPanel.add(passwordField);

Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
ret.add(box);

ret.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
return ret;
}
static JFrame mainFrame = null;
static JTextField usernameField = null;
static JPasswordField passwordField = null;
}
运行:
javac -deprecation Login1.java
java Login
(因为有一个过期的API,所以用了 -deprecation 命令)

方法二,使用了两个JFrame类共同实现,第一次显示第一个frame,当点了登录后且操作合法时,第一个窗口就被释放了 dispose();再显示第二个窗口:
源码如下:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Login2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
//////////////////////////////////////////////////////////////
loginWindow = new JFrame("登录信息");
loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginWindow.setBounds(350,350,250,200);
loginWindow.setResizable(false);
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.CENTER));
usernamePanel.add(usernameField);

JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.CENTER));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
JPanel infoPanel = new JPanel();
infoPanel.add(box);
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
JButton submitButton = new JButton("登录");
JButton cancelButton = new JButton("取消");
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();

if(!username.equals("") && !password.equals("")) {
loginWindow.dispose();
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton); buttonPanel.add(cancelButton);
loginWindow.getContentPane().add(infoPanel,BorderLayout.CENTER);
loginWindow.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.EAST);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.WEST);
loginWindow.setVisible(true);
/////////////////////////////////////////////////////////////////
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
}
static JFrame loginWindow,mainFrame;
static final JTextField usernameField = new JTextField(10);
static final JPasswordField passwordField = new JPasswordField(10);
}
运行:
javac -deprecation Login2.java
java Login2

回答2:

可以直接拿去运行
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.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MainUI {

private String username;
private String password;
private JFrame login_Frame;
private JLabel username_Label;
private JLabel password_Label;
private JTextField username_Field;
private JPasswordField password_Field;
private JButton reset_Button;
private JButton submit_Button;

private static MainUI clientUI = new MainUI();;

public static MainUI getUIClass() {
return clientUI;
}

/**
* 创建登陆窗口
*/
public void createLoginUI() {
login_Frame = new JFrame();
login_Frame.setTitle("chaochao简易聊天室登陆框");
login_Frame.setSize(210, 170);
username_Field = new JTextField(10);
password_Field = new JPasswordField(10);
username_Label = new JLabel("用户名");
password_Label = new JLabel("密 码");
reset_Button = new JButton("重置");
submit_Button = new JButton("登陆");
java.awt.FlowLayout fl = new java.awt.FlowLayout();
login_Frame.setLayout(fl);
login_Frame.add(username_Label);
login_Frame.add(username_Field);
login_Frame.add(password_Label);
login_Frame.add(password_Field);
login_Frame.add(submit_Button);
login_Frame.add(reset_Button);
login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗体居中
login_Frame.setLocationRelativeTo(null);
login_Frame.setVisible(true);

//重置按钮加事件监听器
reset_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username_Field.setText("");
password_Field.setText("");
}
});

//登录按钮加事件监听器
submit_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = username_Field.getText();
password = password_Field.getText();
//判断用户名密码是否正确
if (username.equals("admin") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "登陆成功!", "消息",
JOptionPane.INFORMATION_MESSAGE);
login_Frame.dispose();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
});

}

public static void main(String[] args) {
MainUI c = getUIClass();
c.createLoginUI();
}

}

回答3:

package denglujiemian;

import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

class denglujiemian extends JFrame implements ActionListener{

JFrame jf=new JFrame("QQ2010");
JPanel jp=new JPanel();
JLabel jl[]=new JLabel[9];
char c;
JButton jb1,jb2;
JCheckBox jc1,jc2;
JMenu ch=new JMenu();
JMenuItem jm=new JMenuItem("登录",new ImageIcon("image\\.jpg"));
JComboBox jcb=new JComboBox ();
JPasswordField jtf=new JPasswordField(12);
Color color=new Color(228,244,255);
DefaultCellEditor dce=new DefaultCellEditor(jcb);
denglujiemian(){
jf.setContentPane(new MyPanel());
jf.setBounds(400, 300, 340, 250);
jf.setLayout(null);
Image image = Toolkit.getDefaultToolkit().getImage("image\\qq.jpg");
jf.setIconImage(image);
Icon icon=new ImageIcon("image\\logo.jpg");
jl[1]=new JLabel("",icon,SwingConstants.TRAILING);
Icon icon1=new ImageIcon("image\\di.jpg");
jl[6]=new JLabel("",icon1,SwingConstants.TRAILING);
jl[2]=new JLabel("账号:");
jl[3]=new JLabel("密码:");
jl[4]=new JLabel("注册新账号");
jl[5]=new JLabel("找回密码");
jl[7]=new JLabel("状态:");
jc1=new JCheckBox("记住密码");
jc2=new JCheckBox("自动登录");
jb1=new JButton("设置");
jb2=new JButton("登录");
jb1.setBounds(10, 187, 70, 20);
jb2.setBounds(255, 187, 70, 20);
jl[1].setBounds(0, 0, 335, 64);
jl[2].setBounds(25, 80, 50,20);
jl[3].setBounds(25, 120, 50, 20);
jl[4].setBounds(253, 80, 70, 20);
jl[4].setForeground(Color.BLUE);
jl[5].setBounds(253, 120, 70, 20);
jl[5].setForeground(Color.blue);
jl[6].setBounds(0, 182, 340, 34);//底边图片
jl[7].setBounds(25, 150, 80, 20);
jc1.setBounds(100 ,150, 80, 20);
jc1.setBackground(color);
jc2.setBounds(180 ,150, 80, 20);
jc2.setBackground(color);
jcb.setBounds(60, 80, 190, 22);
ch.setBounds(60, 150, 30, 20);
jtf.setBounds(60, 120,190, 22);
jcb.setEditable(true);
ch.add(jm);
jf.add(jb1);
jf.add(jb2);
jf.add(jc1);
jf.add(jc2);
jf.add(jl[7]);
jf.add(jl[6]);
jf.add(jl[4]);
jf.add(jl[5]);
jf.add(jl[3]);
jf.add(jl[2]);
jf.add(jl[1]);
jf.add(jtf);
jf.add(jcb);
jf.add(ch);

jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}
public class denglu {
public static void main(String[] args) {
// TODO Auto-generated method stub

new denglujiemian();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("image\beijin.jpg");
g2.drawImage(img, 0, 0, this.getWidth(),this.getHeight(), this);
}
}

}