import java.awt.Color; import java.awt.Rectangle; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; /** * @Description test * @ClassName LoginUI * @Date 2016年12月28日 上午10:42:11 * @Author yanghao Copyright (c) All Rights Reserved, 2016. */ public class LoginUI extends JFrame { private static final long serialVersionUID = -3214668522103694354L; // HexTextField login_text_name = new HexTextField(""); HexPasswordField login_text_pwd = new HexPasswordField(""); // final int w = 360; final int h = 140; public LoginUI() { setSize(w, h); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.buildPane(); setVisible(true); this.requestFocus(); } private void buildPane() { JPanel pa = new JPanel(); pa.setLayout(null); add(pa); // JLabel lb_0 = new JLabel("用 户:"); lb_0.setBounds(12, 15, 50, 20); pa.add(lb_0); login_text_name.setBounds(60, 15, 100, 20); login_text_name.setPlaceholder("请输入登录名");//当输入框没有内容时,占位显示信息 pa.add(login_text_name); JLabel lb_1 = new JLabel("密 码:"); lb_1.setBounds(180, 15, 50, 20); pa.add(lb_1); login_text_pwd.setBounds(220, 15, 100, 20); login_text_pwd.setPlaceholder("请输入登录密码");//当输入框没有内容时,占位显示信息 pa.add(login_text_pwd); // JButton login_bt_sub = new JButton("登录");// login_bt_sub.setBounds((w - 120) / 2, 70, 120, 20); pa.add(login_bt_sub); // HexKeyCall adap = new HexKeyCall() { public void callBack(KeyEvent e) { if (e.getExtendedKeyCode() == 10) { login(); } } }; login_text_name.keyPressedCall(adap); login_text_pwd.keyPressedCall(adap); } protected void login() { String name = login_text_name.getText(); String pwd = String.valueOf(login_text_pwd.getPassword()); System.out.println("input:" + name + "/" + pwd); } public static void main(String[] args) { new LoginUI(); } } /** * @Description 输入框 * @ClassName HexTextField * @Date 2016年12月28日 上午10:41:45 * @Author yanghao Copyright (c) All Rights Reserved, 2016. */ class HexTextField extends JTextField { final int fontSize = 12; public HexTextField() { super(); setFont(new java.awt.Font("宋体", 0, fontSize)); } public HexTextField(String text) { super(text); setFont(new java.awt.Font("宋体", 0, fontSize)); this.setForeground(Color.black); } /* 默认显示值 */ /** * @Description 设置空值时的默认显示 * @param @param text 参数 * @return void 返回类型 * @throws */ public void setPlaceholder(String text) { addFocusListener(focuAdp); Rectangle bs = this.getBounds(); mask.setText(text); mask.setForeground(Color.lightGray); mask.setBounds(2, 0, bs.width - 2, bs.height - 2); mask.setFont(new java.awt.Font("宋体", 0, 12)); this.add(mask); } FocusListener focuAdp = new FocusListener() { public void focusGained(FocusEvent arg0) { mask.setVisible(false); } public void focusLost(FocusEvent arg0) { String text = getText().trim(); if (text.length() == 0) { mask.setVisible(true); } } }; public void setText(String text) { super.setText(text); if (mask != null) { mask.setVisible(false); } } JLabel mask = new JLabel(); /* 默认显示值 */ /* 键盘监听 */ private HexKeyCall call; KeyAdapter adap = new KeyAdapter() { public void keyPressed(KeyEvent e) { call.callBack(e); } }; public void keyPressedCall(HexKeyCall call) { this.call = call; addKeyListener(adap); } /* 键盘监听 */ } /** * @Description 密码 * @ClassName HexPasswordField * @Date 2016年12月28日 上午10:41:19 * @Author yanghao Copyright (c) All Rights Reserved, 2016. */ class HexPasswordField extends JPasswordField { final int fontSize = 12; public HexPasswordField() { super(); setFont(new java.awt.Font("宋体", 0, fontSize)); } public HexPasswordField(String text) { super(text); setFont(new java.awt.Font("宋体", 0, fontSize)); } /* 默认显示值 */ /** * @Description 设置空值时的默认显示 * @param @param text 参数 * @return void 返回类型 * @throws */ public void setPlaceholder(String text) { addFocusListener(focuAdp); Rectangle bs = this.getBounds(); mask.setText(text); mask.setForeground(Color.lightGray); mask.setBounds(2, 0, bs.width - 2, bs.height - 2); mask.setFont(new java.awt.Font("宋体", 0, 12)); this.add(mask); } FocusListener focuAdp = new FocusListener() { public void focusGained(FocusEvent arg0) { mask.setVisible(false); } public void focusLost(FocusEvent arg0) { String text = String.valueOf(getPassword()).trim(); if (text.length() == 0) { mask.setVisible(true); } } }; JLabel mask = new JLabel(); /* 默认显示值 */ /* 键盘监听 */ public void keyPressedCall(HexKeyCall call) { this.call = call; addKeyListener(adap); } private HexKeyCall call; KeyAdapter adap = new KeyAdapter() { public void keyPressed(KeyEvent e) { call.callBack(e); } }; /* 键盘监听 */ } /** * @Description 回调 * @ClassName HexKeyCall * @Date 2016年12月28日 上午10:42:50 * @Author yanghao Copyright (c) All Rights Reserved, 2016. */ interface HexKeyCall { void callBack(KeyEvent e); }