import?java.awt.*;
創(chuàng)新互聯(lián)公司專注于慈溪企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開發(fā)。慈溪網(wǎng)站建設(shè)公司,為慈溪等地區(qū)提供建站服務(wù)。全流程按需設(shè)計網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
import?java.awt.event.*;
import?javax.swing.*;
public?class?QQLogin?extends?JFrame?{
JTextField?jtf,jtf1;
public?QQLogin()?{
jtf?=?new?JTextField(12);
jtf.setText("請輸入");
jtf.setForeground(Color.GRAY);//?設(shè)置顏色
JPanel?jp?=?new?JPanel();
jp.add(jtf);
jtf1?=?new?JTextField(12);
JPanel?jp1?=?new?JPanel();
jp1.add(jtf1);
this.add(jp1,?BorderLayout.NORTH);
this.add(jp,BorderLayout.SOUTH);
this.setTitle("密碼輸入提示");
this.setBounds(300,?200,?220,?160);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//jp1.getComponents()[0].setBackground(Color.RED);
//jp1上可能添加了很多組件,于是可以使用getComponents()返回一個組件數(shù)組。
//[0]表示選取該組件數(shù)組的第一個組件
//setBackground(Color.RED)?表示把該組件變成紅色的背景顏色
jtf.addFocusListener(new?FocusListener()?{
@Override
public?void?focusLost(FocusEvent?e)?{//?失去焦點的時候
//?如果內(nèi)容為空,設(shè)置文本
if?(jtf.getText().trim().equals(""))?{
jtf.setText("請輸入");
}
}
@Override
public?void?focusGained(FocusEvent?e)?{//?得到焦點的時候
if?(jtf.getText().trim().equals("請輸入"))?{
jtf.setText("");//?讓文本為空白
}
}
});
}
public?static?void?main(String[]?args)?{
new?QQLogin();
}
}
代碼上已經(jīng)回答了你的2個問題
passwordField.addKeyListener(new KeyListener() {//創(chuàng)建一個監(jiān)聽器
@Override
public void keyTyped(KeyEvent e) {//按下的監(jiān)聽器
String passString =new String(passwordField.getPassword());//取回密碼框的內(nèi)容
if (passString.length()==5) {//判斷長度
e.consume();//輸入無效
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
完整代碼如下:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;public class FieldDemo extends JFrame implements ActionListener {
JTextField nameField;
JPasswordField passField; public static void main(String arg[]) {
new FieldDemo();
} public FieldDemo() {
int height;
int width;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLocation(250, 150);
Container pane = getContentPane();
pane.setLayout(null);
Insets insets = pane.getInsets(); JLabel nameLabel = new JLabel("User name");
Dimension labelDim = nameLabel.getPreferredSize();
int x1 = insets.left + 20;
int y1 = insets.top + 20;
nameLabel.setBounds(x1, y1, labelDim.width, labelDim.height);
nameLabel.setHorizontalTextPosition(JLabel.RIGHT);
pane.add(nameLabel); JLabel passLabel = new JLabel("Password");
int x2 = x1;
int y2 = y1 + labelDim.height + 20;
passLabel.setBounds(x2, y2, labelDim.width, labelDim.height);
passLabel.setHorizontalTextPosition(JLabel.RIGHT);
pane.add(passLabel); nameField = new JTextField(32);
x1 += labelDim.width;
Dimension textDim = nameField.getPreferredSize();
nameField.setBounds(x1, y1, textDim.width, textDim.height);
pane.add(nameField); passField = new JPasswordField(32);// 密碼框?qū)S茫瑳]有明文回顯
x2 += labelDim.width;
passField.setBounds(x2, y2, textDim.width, textDim.height);
pane.add(passField); width = x1 + textDim.width + 20 + insets.left; JButton button = new JButton("Login");
Dimension buttonDim = button.getPreferredSize();
int x3 = (width - buttonDim.width) / 2;
int y3 = y2 + labelDim.height + 20;
button.setBounds(x3, y3, buttonDim.width, buttonDim.height);
button.addActionListener(this);
pane.add(button); height = y3 + buttonDim.height + 40 + insets.bottom; setSize(width, height);
setVisible(true);
} public void actionPerformed(ActionEvent e) {
System.out.println(nameField.getText());
char password[] = passField.getPassword();
System.out.println(password);
System.exit(0);
}
}
JTextField jtf=new JTextField();
String str=jtf.getText().trim();//密碼字符串
JOptionPane.showConfirmDialo(this,jtf ,"輸入密碼",JOptionPane.OK_CANCEL_OPTION );
if(str.length!=6){
JOptionPane.showMessageDialog(this,"請輸入6位密碼");
}
//直接打的,可能有錯誤
!DOCTYPE?html
html
head
title登錄/title
/head
body
form?onsubmit="return?check()"
label用戶名/labelinput?id="username"?type="text"?name="username"
label密碼/labelinput?id="password"?type="password"?name="password"
input?type="submit"?value="登錄"
/form
script?type="text/javascript"
function?check(){
var?username?=?document.getElementById("username").value;
var?password?=?document.getElementById("password").value;
if(!username){
alert("用戶名不能為空!");
return?false;
}
if(!password){
alert("密碼不能為空!");
return?false;
}
return?true
}
/script
/body
/html