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实现简单登录界面
自己写的比较规范的代码,都有注释:
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();
}
}
㈢ 鐢╦ava鍐欎竴涓锏婚檰鐣岄溃浠g爜銆
姒傝堪
鍏蜂綋妗嗘灦浣跨敤jframe锛屾枃链妗嗙粍浠讹细JTextField锛涘瘑镰佹嗙粍浠讹细JPasswordField锛涙爣绛剧粍浠讹细JLabel锛涘嶉夋嗙粍浠讹细JCheckBox锛涘崟阃夋嗙粍浠讹细JRadioButton锛涙寜阍缁勪欢JButton銆
锏诲綍鐣岄溃锛
Swing 鏄涓涓涓筼ava璁捐$殑GUI宸ュ叿鍖呫
Swing鏄疛AVA锘虹绫荤殑涓閮ㄥ垎銆
Swing鍖呮嫭浜嗗浘褰㈢敤鎴风晫闱锛圙UI锛夊櫒浠跺傦细鏂囨湰妗嗭纴鎸夐挳锛屽垎闅旂獥镙煎拰琛ㄣ
Swing鎻愪緵璁稿氭瘆AWT镟村ソ镄勫睆骞曟樉绀哄厓绱犮傚畠浠鐢ㄧ函Java鍐欐垚锛屾墍浠ュ悓Java链韬涓镙峰彲浠ヨ法骞冲彴杩愯岋纴杩欎竴镣逛笉镀廇WT銆傚畠浠鏄疛FC镄勪竴閮ㄥ垎銆傚畠浠鏀鎸佸彲镟存崲镄勯溃𨱒垮拰涓婚桡纸钖勭嶆搷浣灭郴缁熼粯璁ょ殑鐗规湁涓婚桡级锛岀劧钥屼笉鏄鐪熺殑浣跨敤铡熺敓骞冲彴鎻愪緵镄勮惧囷纴钥屾槸浠呬粎鍦ㄨ〃闱涓婃ā浠垮畠浠銆傝繖镒忓懗镌浣犲彲浠ュ湪浠绘剰骞冲彴涓娄娇鐢↗AVA鏀鎸佺殑浠绘剰闱㈡澘銆傝交閲忕骇缁勪欢镄勭己镣瑰垯鏄镓ц岄熷害杈冩参锛屼紭镣瑰氨鏄鍙浠ュ湪镓链夊钩鍙颁笂閲囩敤缁熶竴镄勮屼负銆
姒傚康瑙f瀽锛
JFrame_ java镄凣UI绋嫔簭镄勫熀链镐濊矾鏄浠JFrame涓哄熀纭锛屽畠鏄灞忓箷涓妛indow镄勫硅薄锛岃兘澶熸渶澶у寲銆佹渶灏忓寲銆佸叧闂銆
JPanel_ Java锲惧舰鐢ㄦ埛鐣岄溃(GUI)宸ュ叿鍖却wing涓镄勯溃𨱒垮瑰櫒绫伙纴鍖呭惈鍦╦avax.swing 鍖呬腑锛屽彲浠ヨ繘琛屽祵濂楋纴锷熻兘鏄瀵圭獥浣扑腑鍏锋湁鐩稿悓阃昏緫锷熻兘镄勭粍浠惰繘琛岀粍钖堬纴鏄涓绉嶈交閲忕骇瀹瑰櫒锛屽彲浠ュ姞鍏ュ埌JFrame绐椾綋涓銆伞
JLabel_ JLabel 瀵硅薄鍙浠ユ樉绀烘枃链銆佸浘镀忔垨钖屾椂鏄剧ず浜岃呫傚彲浠ラ氲繃璁剧疆鍨傜洿鍜屾按骞冲归绨鏂瑰纺锛屾寚瀹氭爣绛炬樉绀哄尯涓镙囩惧唴瀹瑰湪浣曞勫归绨銆傞粯璁ゆ儏鍐典笅锛屾爣绛惧湪鍏舵樉绀哄尯鍐呭瀭鐩村眳涓瀵归绨銆傞粯璁ゆ儏鍐典笅锛屽彧鏄剧ず鏂囨湰镄勬爣绛炬槸寮濮嬭竟瀵归绨锛涜屽彧鏄剧ず锲惧儚镄勬爣绛惧垯姘村钩灞呬腑瀵归绨銆
JTextField_涓涓杞婚噺绾х粍浠讹纴瀹冨厑璁哥紪杈戝崟琛屾枃链銆
JPasswordField_ 鍏佽告垜浠杈揿叆浜嗕竴琛屽瓧镀忚緭鍏ユ嗭纴浣嗛殣钘忔槦鍙(*) 鎴栫偣鍒涘缓瀵嗙爜(瀵嗙爜)
JButton_ JButton 绫荤殑瀹炰緥銆傜敤浜庡垱寤烘寜阍绫讳技瀹炰緥涓镄 "Login"銆