swing 登陆界面,JTextField 圆角,JPasswordField 默认提示

JTextField 圆角:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;

import javax.swing.JTextField;

public class MyRoundJtextField extends JTextField{
	
	private Shape shape;
    public MyRoundJtextField() {
//        super(size);
        setOpaque(false); // As suggested by @AVD in comment.
    }
    
    @Override
    public void setBounds(int x, int y, int width, int height) {
    	// TODO Auto-generated method stub
    	super.setBounds(x, y, width, height);
    }
    
    protected void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRoundRect(0, 0, getWidth()-2, getHeight()-2, 12, 12);
         super.paintComponent(g);
    }
    protected void paintBorder(Graphics g) {
    	Graphics2D g2d = (Graphics2D) g;
    	//圆角抗锯齿
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
//		g2d.setColor(new Color(221,221,221));
        
		g2d.drawRoundRect(0, 0, getWidth()-2, getHeight()-2, 12, 12);
    }
//    public boolean contains(int x, int y) {
//         if (shape == null || !shape.getBounds().equals(getBounds())) {
//             shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 10, 10);
//         }
//         return shape.contains(x, y);
//    }

}

JPasswordField 圆角:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;

import javax.swing.JPasswordField;

public class MyRoundJpassTextfield extends JPasswordField{
	
	private Shape shape;
    public MyRoundJpassTextfield() {
//        super(size);
        setOpaque(false); // As suggested by @AVD in comment.
    }
    protected void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRoundRect(0, 0, getWidth()-2, getHeight()-2, 12, 12);
         super.paintComponent(g);
    }
    protected void paintBorder(Graphics g) {
    	Graphics2D g2d = (Graphics2D) g;
    	//圆角抗锯齿
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
//		g2d.setColor(new Color(221,221,221));
        
		g2d.drawRoundRect(0, 0, getWidth()-2, getHeight()-2, 12, 12);
    }
//    public boolean contains(int x, int y) {
//         if (shape == null || !shape.getBounds().equals(getBounds())) {
//             shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 10, 10);
//         }
//         return shape.contains(x, y);
//    }

}

输入框默认提示:

import java.awt.Color;
import java.awt.Image;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;


public class JTextFieldHintListener implements FocusListener {

	private String hintText;

	private JTextField textField;
	

	public JTextFieldHintListener(JTextField jTextField,String hintText) {

		this.textField = jTextField;

		this.hintText = hintText;
		
		jTextField.setText(hintText);  //默认直接显示

		jTextField.setForeground(Color.GRAY);

	}

 

	@Override
	public void focusGained(FocusEvent e) {

		//获取焦点时,清空提示内容
		String temp = textField.getText();

		if(temp.equals(hintText)) {

			textField.setText("");

			textField.setForeground(Color.BLACK);
			
			if(textField instanceof JPasswordField){
				((JPasswordField) textField).setEchoChar('*');
			}
		}
	}

 

	@Override
	public void focusLost(FocusEvent e) {	
		//失去焦点时,没有输入内容,显示提示内容
		String temp = textField.getText();
		if(StringUtils.isBlank(temp)) {
			if(textField instanceof JPasswordField){
				((JPasswordField) textField).setEchoChar((char)0);
			}
			
			textField.setForeground(Color.GRAY);
				
			textField.setText(hintText);
			
		}
	}

}

测试类:

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Test_JTextField extends JFrame{
	
	private JTextField user_jtextField;
	private JPasswordField pass_jpassField;
	
	public void initUI(){
		this.setTitle("JTextField demo");
		this.setLayout(null);
		this.setSize(560, 480);
		
		user_jtextField = new MyRoundJtextField();
		user_jtextField.setBounds(200, 160, 180, 56);
		user_jtextField.addFocusListener(new JTextFieldHintListener(user_jtextField, "用户名"));
		this.add(user_jtextField);
		
		pass_jpassField = new MyRoundJpassTextfield();
		pass_jpassField.setBounds(200, 240, 180, 56);
		pass_jpassField.addFocusListener(new JTextFieldHintListener(pass_jpassField, "密码"));
		pass_jpassField.setEchoChar((char)0);
		this.add(pass_jpassField);
		
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		Test_JTextField textFrame = new Test_JTextField();
		textFrame.initUI();
	}
	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值