窗口登录的java代码 登陆窗口代码

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

方法一:

成都网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序设计、集团成都企业网站定制等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:木制凉亭等众多领域,积累了大量丰富的经验,同时也获得了客户的一致赞誉!

采用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

Java制作一个用户登录的窗口

Java用户登陆这块,主要还是类:

1,边界布局:BorderLayout。他主要分为五个布局,是JFrame(顶层容器),JDialog(创建对话框窗口的类)的默认布局方式。其最多容量为5个组件,超出5个得用其他的。设置方式为:BorderLayout.NORTH;BorderLayout.SOUTH;BorderLayout.CENTER;Borderlayout.CENTER;BorderLayout.LEFT;BorderLayout.RIGHT。

2,流式布局:FlowLayout。布局方式为从左到右,从上到下。是JPanel(轻量级容器)的默认面板布局。

3,网格布局:GridLayout。布局方式为行和列组成的网络。布局方法:setLayout(new

GridLayout(3,2,3,3));其中强两位数字表示三行两列,后两位表示行与行的间距为3,列与列的间距为3.

接着,就接触到JPanel面板。JPanel是非顶层容器,所以,一个界面只能由一个JFrame,但是可以有多个JPanel组件。其默认布局方式为流式布局。在JPanel这块,学到了用户登录界面的设计。从而接触到另外三个组件:文本框组件:JTextField;密码框组件:JPasswordField;标签组件:JLabel;复选框组件:JCheckBox;单选框组件:JRadioButton;按钮组件JButton。

登陆界面的java代码怎么写?

import java.awt.*; \x0d\x0aimport javax.swing.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0aclass LoginFrm extends JFrame implements ActionListener \x0d\x0a{ \x0d\x0aJLabel lbl1=new JLabel("用户名"); \x0d\x0aJLabel lbl2=new JLabel("密码"); \x0d\x0aJTextField txt=new JTextField(15); \x0d\x0aJPasswordField pf=new JPasswordField(); \x0d\x0aJButton btn1=new JButton("确定"); \x0d\x0aJButton btn2=new JButton("取消"); \x0d\x0a\x0d\x0apublic LoginFrm() \x0d\x0a{ \x0d\x0athis.setTitle("登陆"); \x0d\x0aJPanel jp=(JPanel)this.getContentPane(); \x0d\x0ajp.setLayout(new GridLayout(3,2,10,10)); \x0d\x0ajp.add(lbl1);jp.add(txt); \x0d\x0ajp.add(lbl2);jp.add(pf); \x0d\x0ajp.add(btn1);jp.add(btn2); \x0d\x0abtn1.addActionListener(this); \x0d\x0abtn2.addActionListener(this); \x0d\x0a} \x0d\x0a\x0d\x0apublic void actionPerformed(ActionEvent ae) \x0d\x0a{ \x0d\x0aif(ae.getSource()==btn1) \x0d\x0a{ \x0d\x0atry \x0d\x0a{ \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); \x0d\x0aConnection con=DriverManager.getConnection("jdbc:odbc:MyDB","",""); \x0d\x0aStatement cmd=con.createStatement(); \x0d\x0aResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'"); \x0d\x0aif(rs.next()) \x0d\x0a{ \x0d\x0aJOptionPane.showMessageDialog(null,"登陆成功!"); \x0d\x0a} \x0d\x0aelse \x0d\x0aJOptionPane.showMessageDialog(null,"用户名或密码错误!"); \x0d\x0a} catch(Exception ex){} \x0d\x0a\x0d\x0aif(ae.getSource()==btn2) \x0d\x0a{ \x0d\x0atxt.setText(""); \x0d\x0apf.setText(""); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0apublic static void main(String arg[]) \x0d\x0a{ \x0d\x0aJFrame.setDefaultLookAndFeelDecorated(true); \x0d\x0aLoginFrm frm=new LoginFrm(); \x0d\x0afrm.setSize(400,200); \x0d\x0afrm.setVisible(true); \x0d\x0a} \x0d\x0a}


名称栏目:窗口登录的java代码 登陆窗口代码
文章来源:http://azwzsj.com/article/ddejjpo.html