重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联公司专注于慈溪企业网站建设,响应式网站建设,商城网站定制开发。慈溪网站建设公司,为慈溪等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
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);// 设置颜色
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()返回一个组件数组。
//[0]表示选取该组件数组的第一个组件
//setBackground(Color.RED) 表示把该组件变成红色的背景颜色
jtf.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {// 失去焦点的时候
// 如果内容为空,设置文本
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();
}
}
代码上已经回答了你的2个问题
passwordField.addKeyListener(new KeyListener() {//创建一个监听器
@Override
public void keyTyped(KeyEvent e) {//按下的监听器
String passString =new String(passwordField.getPassword());//取回密码框的内容
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);// 密码框专用,没有明文回显
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