程序如下:
我們注重客戶提出的每個要求,我們充分考慮每一個細節(jié),我們積極的做好網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)服務,我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)建站贏得了業(yè)內(nèi)的良好聲譽,這一切,也不斷的激勵著我們更好的服務客戶。 主要業(yè)務:網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計,成都小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。
mport java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密 ? ? 碼:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登錄");
JPanel pnl = new JPanel();
private int error = 0;
public MainFrame(String title) throws HeadlessException {
super(title);
init();
}
private void init() {
this.setResizable(false);
pwd.setEchoChar('*');
pnl.add(lbl1);
pnl.add(txt);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equal花憨羔窖薏忌割媳公顱s(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error 3){
JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("測試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}
編程的注意事項:
1、Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復雜的編程。
2、 Java具有簡單性、面向?qū)ο蟆⒎植际?、健壯性、安全性、平臺獨立與可移植性、多線程、動態(tài)性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等。
3、2006年11月13日,Java技術(shù)的發(fā)明者Sun公司宣布,將Java技術(shù)作為免費軟件對外發(fā)布。Sun公司正式發(fā)布的有關(guān)Java平臺標準版的第一批源代碼,以及Java迷你版的可執(zhí)行源代碼。從2007年3月起,全世界所有的開發(fā)人員均可對Java源代碼進行修改。
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//定義該類繼承自JFrame,實現(xiàn)ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
//創(chuàng)建JPanel對象
private JPanel jp=new JPanel();
//創(chuàng)建3個標并加入數(shù)組
JLabel name = new JLabel("請輸入用戶名");
JLabel password = new JLabel("請輸入密碼");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
//創(chuàng)建登陸和重置按扭并加入數(shù)組
JButton login = new JButton("登陸");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
//創(chuàng)建文本框以及密碼框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
//設(shè)置布局管理器為空布局,這里自己擺放按鈕、標簽和文本框
jp.setLayout(null);
for(int i=0;i2;i++)
{
//設(shè)置標簽和按扭的位置與大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
//添加標簽和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//為2個按鈕注冊動作事件監(jiān)聽器
jb[i].addActionListener(this);
}
//設(shè)置文本框的位置和大小,注意滿足美觀并足夠用戶名的長度
jName.setBounds(130,15,100,20);
//添加文本框到JPanel容器中
jp.add(jName);
//為文本框注冊動作事件監(jiān)聽器
jName.addActionListener(this);
//設(shè)置密碼框的位置和大小,注意滿足美觀和足夠密碼的長度
jPassword.setBounds(130,60,100,20);
//添加密碼框到JPanel容器中
jp.add(jPassword);
//設(shè)置密碼框中的回顯字符,這里設(shè)置美元符號
jPassword.setEchoChar('$');
//為密碼框注冊動作事件監(jiān)聽器
jPassword.addActionListener(this);
//設(shè)置用于顯示登陸狀態(tài)的標簽大小位置,并將其添加進JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
//添加JPanel容器到窗體中
this.add(jp);
//設(shè)置窗體的標題、位置、大小、可見性及關(guān)閉動作
this.setTitle("登陸窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//實現(xiàn)動作監(jiān)聽器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
//如果事件源為文本框
if(e.getSource()==jName)
{
//切換輸入焦點到密碼框
jPassword.requestFocus();
}
//如果事件源為重置按扭
else if(e.getSource()==jb[1])
{
//清空姓名文本框、密碼框和show標簽中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
//讓輸入焦點回到文本框
jName.requestFocus();
}
//如果事件源為登陸按鈕,則判斷登錄名和密碼是否正確
else
{
//判斷用戶名和密碼是否匹配
if(jName.getText().equals("lixiangguo")
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
jl[2].setText("登陸成功,歡迎您的到來!");
}
else
{
jl[2].setText("對不起,您的用戶名或密碼錯誤!");
}
}
}
public static void main(String[] args)
{
//創(chuàng)建LoginTest窗體對象
new LoginTest();
}
}
這個簡單點的
import?java.awt.Dimension;
import?java.awt.Toolkit;
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?Test26?{
public?static?void?main(String[]?args)?{
final?String?userName?=?"abc";
final?String?passwrod?=?"123";
JFrame?jFrame?=?new?JFrame("登陸界面");
Dimension?dimension?=?Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth()?-?200)?/?2,?((int)dimension.getHeight()?-?300)?/?2,?200,?150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel?label1?=?new?JLabel("姓名");
label1.setBounds(10,?10,?100,?30);
jFrame.add(label1);
JLabel?label2?=?new?JLabel("密碼");
label2.setBounds(10,?40,?100,?30);
jFrame.add(label2);
final?JTextField?text1?=?new?JTextField();
text1.setBounds(50,?15,?130,?20);
jFrame.add(text1);
final?JPasswordField?text2?=?new?JPasswordField();
text2.setBounds(50,?45,?130,?20);
jFrame.add(text2);
JButton?button?=?new?JButton("Login");
button.setBounds(10,?75,?170,?40);
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if(userName.equals(text1.getText())??passwrod.equals(text2.getText()))?{
JOptionPane.showMessageDialog(null,?"登陸成功誤",?"提示",?JOptionPane.INFORMATION_MESSAGE);
}?else?{
JOptionPane.showMessageDialog(null,?"錯誤",?"提示",?JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一個微信公眾號,經(jīng)常會分享一些Java技術(shù)相關(guān)的干貨,還有一些學習資源。
如果你喜歡我的分享,可以用微信搜索“Java團長”或者“javatuanzhang”關(guān)注。
用java做好的登陸界面,當?shù)顷懗晒筇D(zhuǎn)到下個頁面的代碼如下:
如果登陸驗證是在jsp中,那么跳轉(zhuǎn)可以寫成
1.response.sendRedirct("跳轉(zhuǎn)到頁面");
2.jsp:forward page="跳轉(zhuǎn)頁面"/
3.response.setHeader("Location","");
如果是登陸驗證是在servlet中,那么中轉(zhuǎn)可以寫成
1.response.sendRedirect("/a.jsp");
2.RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
也可以使用js代碼實現(xiàn):
script
function validate(){
window.location.href="/index.jsp";
}
/script
你要先學會截圖哦,你發(fā)的看不清楚,重新寫了一個你參考參考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Day30A extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
private ButtonGroup btg;
private JRadioButton jr1,jr2;
Day30A(){
this.setTitle("注冊賬戶");
this.setLayout(new GridLayout(7,1));
this.setSize(300,280);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
String str="卡片類型1,卡片類型2,卡片類型3,卡片類型4,卡片類型5";
jcb=new JComboBox(str.split(","));
labelId=new JLabel("賬號: ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密碼: ");
labelMoney=new JLabel("開戶金額:");
labelSelect=new JLabel("存款類型:");
labelCar=new JLabel("卡片類型:");
addFun1();
addFun2();
}
private void addFun2() {
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
}
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
jp1.add(new JTextField(15));
jp2=new JPanel();
jp2.add(labelName);
jp2.add(new JTextField(15));
jp3=new JPanel();
jp3.add(labelPass);
jp3.add(new JTextField(15));
jp4=new JPanel();
jp4.add(labelMoney);
jp4.add(new JTextField(13));
jp5=new JPanel();
jp5.add(labelSelect);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
jr2=new JRadioButton("活期",true);
btg.add(jr1);
btg.add(jr2);
jp5.add(jr1);
jp5.add(jr2);
jp6=new JPanel();
jp6.add(labelCar);
jp6.add(jcb);
jp7=new JPanel();
jp7.add(new JButton("確定"));
jp7.add(new JButton("取消"));
}
public static void main(String[] args) {
new Day30A();
}
}
概述
具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:
代碼實例
import javax.swing.*;
import java.awt.*; ? //導入必要的包
public class denglu extends JFrame{
JTextField jTextField ;//定義文本框組件
JPasswordField jPasswordField;//定義密碼框組件
JLabel jLabel1,jLabel2;
JPanel jp1,jp2,jp3;
JButton jb1,jb2; //創(chuàng)建按鈕
public denglu(){
jTextField = new JTextField(12);
jPasswordField = new JPasswordField(13);
jLabel1 = new JLabel("用戶名");
jLabel2 = new JLabel("密碼");
jb1 = new JButton("確認");
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//設(shè)置布局
this.setLayout(new GridLayout(3,1));
jp1.add(jLabel1);
jp1.add(jTextField);//第一塊面板添加用戶名和文本框
jp2.add(jLabel2);
jp2.add(jPasswordField);//第二塊面板添加密碼和密碼輸入框
jp3.add(jb1);
jp3.add(jb2); //第三塊面板添加確認和取消
// ? ? ? ?jp3.setLayout(new FlowLayout()); ?//因為JPanel默認布局方式為FlowLayout,所以可以注銷這段代碼.
this.add(jp1);
this.add(jp2);
this.add(jp3); ?//將三塊面板添加到登陸框上面
//設(shè)置顯示
this.setSize(300, 200);
//this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("登陸");
}
public static void main(String[] args){
new denglu();
}
}
拓展內(nèi)容
java swing包
Swing 是一個為Java設(shè)計的GUI工具包。
Swing是JAVA基礎(chǔ)類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平臺運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統(tǒng)默認的特有主題),然而不是真的使用原生平臺提供的設(shè)備,而是僅僅在表面上模仿它們。這意味著你可以在任意平臺上使用JAVA支持的任意面板。輕量級組件的缺點則是執(zhí)行速度較慢,優(yōu)點就是可以在所有平臺上采用統(tǒng)一的行為。
概念解析:
JFrame?– java的GUI程序的基本思路是以JFrame為基礎(chǔ),它是屏幕上window的對象,能夠最大化、最小化、關(guān)閉。
JPanel?– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。
JLabel?– JLabel 對象可以顯示文本、圖像或同時顯示二者。可以通過設(shè)置垂直和水平對齊方式,指定標簽顯示區(qū)中標簽內(nèi)容在何處對齊。默認情況下,標簽在其顯示區(qū)內(nèi)垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。
JTextField?–一個輕量級組件,它允許編輯單行文本。
JPasswordField?– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創(chuàng)建密碼(密碼)
JButton?– JButton 類的實例。用于創(chuàng)建按鈕類似實例中的 "Login"。
網(wǎng)頁名稱:java登錄界面代碼,java登錄界面代碼加圖片
文章路徑:http://jinyejixie.com/article40/dsedgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站導航、搜索引擎優(yōu)化、外貿(mào)建站、做網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)