简体   繁体   English

定制的textfieldUI外观

[英]customized textfieldUI look&feel

Code below allows to change default TextFieldUI to allow you to have hints shown on the JTextField. 下面的代码允许更改默认的TextFieldUI,以使您可以在JTextField上显示提示。 The problem I'm having is, I'm using nimbus look and feel for my program and when I change this textfieldUI, everything works fine, as it should, but textfield look and feel changes to ugly looking one. 我遇到的问题是,我在程序中使用了nimbus外观,并且当我更改此textfieldUI时,一切工作正常,应该如此,但是textfield外观却变成了难看的外观。 I think, since I'm overriding defaults I should set look and feel in this code manually, but just don't know how to do it! 我认为,由于我要覆盖默认值,因此我应该在此代码中手动设置外观,但是只是不知道该怎么做!

import java.awt.Color;
java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;

public class HintTextFieldUI extends BasicTextFieldUI implements FocusListener {

private String hint;
private boolean hideOnFocus;
private Color color;

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
    repaint();
}

private void repaint() {
    if(getComponent() != null) {
        getComponent().repaint();           
    }
}

public boolean isHideOnFocus() {
    return hideOnFocus;
}

public void setHideOnFocus(boolean hideOnFocus) {
    this.hideOnFocus = hideOnFocus;
    repaint();
}

public String getHint() {
    return hint;
}

public void setHint(String hint) {
    this.hint = hint;
    repaint();
}
public HintTextFieldUI(String hint) {
    this(hint,false);
}

public HintTextFieldUI(String hint, boolean hideOnFocus) {
    this(hint,hideOnFocus, null);
}

public HintTextFieldUI(String hint, boolean hideOnFocus, Color color) {
    this.hint = hint;
    this.hideOnFocus = hideOnFocus;
    this.color = color;
}

@Override
protected void paintSafely(Graphics g) {
    super.paintSafely(g);
    JTextComponent comp = getComponent();
    comp.setu
    if(hint!=null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))){
        if(color != null) {
            g.setColor(color);
        } else {
            g.setColor(comp.getForeground().brighter().brighter().brighter());              
        }
        int padding = (comp.getHeight() - comp.getFont().getSize())/2;
        g.drawString(hint, 2, comp.getHeight()-padding-1);          
    }
}

@Override
protected void installListeners() {
    super.installListeners();
    getComponent().addFocusListener(this);
}
@Override
protected void uninstallListeners() {
    super.uninstallListeners();
    getComponent().removeFocusListener(this);
}

public void focusGained(FocusEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();
}

public void focusLost(FocusEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();  
}

} }

我不知道雨云的风格是什么,但也许文本提示会为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM