自己寫的比較規(guī)范的代碼,都有注釋:
創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)開(kāi)發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開(kāi)發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標(biāo)簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監(jiān)聽(tīng)
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);//設(shè)置關(guān)閉框架的同時(shí)結(jié)束程序
this.setSize(300,200);//設(shè)置框架大小為長(zhǎng)300,寬200
this.setResizable(false);//設(shè)置框架不可以改變大小
this.setTitle("用戶登錄");//設(shè)置框架標(biāo)題
this.pnluser.setLayout(null);//設(shè)置面板布局管理
this.pnluser.setBackground(Color.cyan);//設(shè)置面板背景顏色
this.lbluserLogIn.setText("用戶登錄");//設(shè)置標(biāo)簽標(biāo)題
this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設(shè)置標(biāo)簽字體
this.lbluserLogIn.setForeground(Color.RED);//設(shè)置標(biāo)簽字體顏色
this.lbluserName.setText("用戶名:");
this.lbluserPWD.setText("密 碼:");
this.btnSub.setText("登錄");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//設(shè)置標(biāo)簽x坐標(biāo)120,y坐標(biāo)15,長(zhǎng)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()//匿名類實(shí)現(xiàn)ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名類實(shí)現(xiàn)ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//加載標(biāo)簽到面板
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);//設(shè)置框架可顯
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"賬號(hào)不能為空","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密碼不能為空","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"賬號(hào)或密碼錯(cuò)誤","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new UserLogIn();
}
}
你要先學(xué)會(huì)截圖哦,你發(fā)的看不清楚,重新寫了一個(gè)你參考參考!
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("注冊(cè)賬戶");
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("賬號(hào): ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密碼: ");
labelMoney=new JLabel("開(kāi)戶金額:");
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();
}
}
效果圖
代碼
!DOCTYPE?html
html
head
meta?charset="UTF-8"
title先鋒圖書館管理系統(tǒng)-登錄/title
style
*{
margin:?0;
padding:?0;
list-style:?none;
}
#top{
width:?1000px;
height:?95px;
margin:?0?auto;
margin-top:?25px;
}
#top_top{
width:?1000px;
height:?65px;
background:?deepskyblue;
}
#top_top_left{
width:?300px;
height:?65px;
float:?left;
}
#top_top_leftlabel{
width:?200px;
height:?65px;
color:?white;
float:?right;
}
#top_top_left#a2{
padding-left:?10px;
padding-top:?20px;
font-size:?16px;
}
#top_bottom{
width:?1000px;
height:?30px;
}
#top_bottom_left{
width:?340px;
height:?30px;
line-height:?30px;
font-size:?12px;
background:?skyblue;
color:?white;
text-indent:?2em;
float:?left;
}
#top_bottom_right{
width:?660px;
height:?30px;
line-height:?30px;
font-size:?12px;
color:?blueviolet;
text-align:?center;
float:?right;
background:?lightskyblue;
}
#content{
width:?1000px;
height:?600px;
margin:?0?auto;
background:#587FBA;
}
#content#text{
width:?1000px;
height:?50px;
line-height:?50px;
padding-top:?100px;
font-size:?36px;
font-family:"楷體";
font-weight:?bold;
text-align:?center;
}
#content#login{
width:?480px;
height:?210px;
margin-top:?20px;
margin-left:?260px;
background:?#85A0CB;
}
#content#loginimg{
float:?left;
}
#content#login#select{
width:?305px;
height:?210px;
float:?right;
}
#content#login#selectdiv{
width:?230;
height:?30px;
margin-left:?30px;
}
#content#login#select#d1{
margin-top:30px;
margin-bottom:?3px;
}
#content#login#selectp{
font-size:?14px;
margin-left:?95px;
}
#bottom{
width:?1000px;
height:?35px;
line-height:?35px;
margin:?0?auto;
background:?deepskyblue;
text-align:?center;
color:?white;
}
/style
/head
body
div?id="top"
div?id="top_top"
div?id="top_top_left"
img?src="img/test/a13.png"?width="78px"?height="65px"label?id="a2"先鋒圖書館系統(tǒng)管理平臺(tái)/label
/div
/div
div?id="top_bottom"
div?id="top_bottom_left"當(dāng)前位置?:?首頁(yè)??系統(tǒng)管理??登錄/div
div?id="top_bottom_right"當(dāng)前時(shí)間?:?label?id="lable"/label/div
/div
/div
div?id="content"
div?id="text"歡迎登錄先鋒圖書館管理系統(tǒng)/div
div?id="login"
img?src="img/test/a14.png"??width="175px"?height="210px"/
form?id="select"
div?id="d1"用戶名:?nbsp;nbsp;input?type="text"?//div
div密????nbsp;?碼:?nbsp;nbsp;input?type="password"?//div
p
input?type="radio"?name="user"?value="read"/讀者nbsp;nbsp;nbsp;nbsp;
input?type="radio"?name="user"?value="admin"/管理員
/pbr/
p
input?type="button"?value="確定"?style="width:?50px;"?onclick="put()"/nbsp;nbsp;nbsp;nbsp;
input?type="reset"?value="重置"?style="width:?50px;"/
/p
/form
/div
/div
div?id="bottom"欣欣科技有限公司版權(quán)所有/div
/body
script?type="text/javascript"?src="JQuery/jquery.js"/script
script?type="text/javascript"?src="js/GetCurrentTime.js"/script
script
//驗(yàn)證用戶名和密碼
function?put(){
var?d?=?$("#selectdivinput");//獲取用戶名和密碼
var?name?=?d[0].value;
var?pass?=?d[1].value;
var?user?=?null;
var?r?=?document.getElementsByName("user");//獲取用戶類型
for(i=0;ir.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name?+?","?+pass?+?","?+user);//輸出測(cè)試
if(user==null){
window.alert("請(qǐng)選擇用戶類型!");
}else?if(user=="admin"??name!="admin"){
window.alter("用戶名錯(cuò)誤!");
}else?if(user=="admin"??name=="admin"??pass!="123456"){
window.alert("密碼錯(cuò)誤!");
}else?if(name=="admin"??pass=="123456"??user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁(yè)面中打開(kāi)新鏈接
}else{
window.alert("用戶名錯(cuò)誤");
}
}
/script
/html
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,?"錯(cuò)誤",?"提示",?JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一個(gè)微信公眾號(hào),經(jīng)常會(huì)分享一些Java技術(shù)相關(guān)的干貨,還有一些學(xué)習(xí)資源。
如果你喜歡我的分享,可以用微信搜索“Java團(tuán)長(zhǎng)”或者“javatuanzhang”關(guān)注。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用戶名");
JLabel lbl2=new JLabel("密碼");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("確定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陸");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陸成功!");
}
else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯(cuò)誤!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
程序如下:
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,"密碼輸入錯(cuò)誤,請(qǐng)?jiān)僭囈淮?);
error++;
}
else{
JOptionPane.showMessageDialog(null,"對(duì)不起,您不是合法用戶");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("測(cè)試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}
編程的注意事項(xiàng):
1、Java是一門面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
2、 Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际?、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
3、2006年11月13日,Java技術(shù)的發(fā)明者Sun公司宣布,將Java技術(shù)作為免費(fèi)軟件對(duì)外發(fā)布。Sun公司正式發(fā)布的有關(guān)Java平臺(tái)標(biāo)準(zhǔn)版的第一批源代碼,以及Java迷你版的可執(zhí)行源代碼。從2007年3月起,全世界所有的開(kāi)發(fā)人員均可對(duì)Java源代碼進(jìn)行修改。