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();
}
}