import java.awt.*;
創(chuàng)新互聯(lián)是一家成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè),提供網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需求定制開(kāi)發(fā),網(wǎng)站開(kāi)發(fā)公司,于2013年成立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開(kāi)發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營(yíng)并提出專業(yè)建議和思路。
import java.awt.event.*;
import javax.swing.*;
public class Test_Login extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntLogin;
private JButton bntCannel;
private JPasswordField pwd;
private JTextField username;
private JLabel jLabel2;
private JLabel jLabel1;
public static void main(String[] args) {
Test_Login inst = new Test_Login();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public Test_Login() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("用戶名");
jLabel1.setBounds(45, 30, 75, 25);
}
{
jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText("密碼");
jLabel2.setBounds(45, 75, 55, 15);
}
{
username = new JTextField();
jPanel1.add(username);
username.setBounds(100, 30, 140, 25);
}
{
pwd = new JPasswordField();
jPanel1.add(pwd);
pwd.setBounds(100, 70, 140, 25);
}
{
bntLogin = new JButton();
jPanel1.add(bntLogin);
bntLogin.setText("登陸");
bntLogin.setBounds(40, 120, 60, 30);
bntLogin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (username.getText().equals("lisong")
pwd.getText().equals("lisong")) {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄成功");
} else {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄失敗");
}
}
});
bntCannel = new JButton();
jPanel1.add(bntCannel);
bntCannel.setText("取消");
bntCannel.setBounds(180, 120, 60, 30);
bntCannel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
}
pack();
setSize(300, 215);
} catch (Exception e) {
e.printStackTrace();
}
}
}
containsValue就檢查value里有沒(méi)有這個(gè)值用它來(lái)判斷賬號(hào)密碼是否匹配當(dāng)然會(huì)錯(cuò)誤
比如 key = 1 value = 123;
key = 2 value = 456;
如果用 賬號(hào)是2 密碼是 123來(lái)登錄
按照你第一個(gè)判斷 key里包含了2 是真,value里包含了123也是真,所以整個(gè)if的結(jié)果是真會(huì)提示成功,然而其實(shí)帳號(hào)密碼并不對(duì)
,后面的沒(méi)看,然后就是你的那個(gè)flag完全可以用boolean類型 為什么要用String呢,建議樓主別浮躁好好學(xué)習(xí)一下java的基礎(chǔ)慢慢來(lái)
你要先學(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();
}
}
給你詳細(xì)講一下。
比如系統(tǒng)有個(gè)登陸頁(yè)面(login.jsp):
name:_________
password:____________
(登陸按鈕)
你按下登陸按鈕,就根據(jù)name和password去數(shù)據(jù)庫(kù)里面查,如果判斷有此用戶并且密碼正確,就設(shè)置一個(gè)session的鍵對(duì)應(yīng)的值,鍵名字自己取,統(tǒng)一即可,比如"userInfo",代碼就是servlet的doPost里面
HttpSession session = request.getSession();
Hashtable userInfo = new Hashtable();
userInfo.setAttribute("userName", request.getParameter("userName");
userInfo.setAttribute("passWords", Util.toSecret( request.getParameter("passWords)); //密碼最好加密
session.setAttribute("userInfo", userInfo);
session是在一定時(shí)期(超時(shí)時(shí)間內(nèi))一直存在的,這段時(shí)間內(nèi)你可以隨時(shí)判斷用戶是否合法,否則就退回登陸頁(yè)面。
在任何除了登陸頁(yè)面以外的頁(yè)面訪問(wèn),只需判斷有沒(méi)有這個(gè)鍵值,沒(méi)就到登陸頁(yè)面,否則進(jìn)正常頁(yè)面。(最好寫在servlet中,讓servlet當(dāng)頁(yè)面控制器)代碼如下:
if ( session.getAttribute("userInfo")==null ) {
response.sendRedirect(request.getServletContext.getPath() + "/login.jsp");
} else {
request.getRequestDispatcher("/正常頁(yè)面.jsp").forward(request,response);
}
if(request.getParameter("userclass").equals("用戶"))
...
else if(request.getParameter("userclass").equals("管理員")
...
else
...
原理: 1.隨機(jī)生成4個(gè)數(shù)字 用到了Random類 2.對(duì)這4個(gè)數(shù)字設(shè)置字體格式 用 setFont方法 3.改變字體顏色用setColor 然后隨機(jī)生成顏色 代碼如下 package s1; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a