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

要求:(当用户名为“admin”,密码为“123”时显示正确,其他则显示用户不存在)我是一名初学者,编了很久还是没有编出来,求高手们帮帮忙,小弟不胜感激!... 要求:(当用户名为“admin”,密码为“123”时显示正确,其他则显示用户不存在)
我是一名初学者,编了很久还是没有编出来,求高手们帮帮忙,小弟不胜感激!
展开
 我来答
孩子似的男人
2012-08-09
知道答主
回答量:25
采纳率:0%
帮助的人:9.6万
展开全部
方法一:
采用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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友3c24f79
2010-06-21 · TA获得超过138个赞
知道小有建树答主
回答量:136
采纳率:0%
帮助的人:141万
展开全部
可以直接拿去运行
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();
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ania6
推荐于2016-06-07 · TA获得超过180个赞
知道答主
回答量:82
采纳率:0%
帮助的人:108万
展开全部
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);
}
}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式