我正在追踪,http://www.java2s.com/Tutorial/Java/0240__Swing/RegexFormatterwithaJFormattedTextField.htm。在给定的示例中,如何在不遵循格式化程序的RegEx格式的情况下更改其输入文本上的JFormattedTextField的前景色/文本颜色?

最佳答案

当用户尝试使用JFormattedTextField更改焦点时,可以更改InputVerifier的前景色。从此完整的example开始,在shouldYieldFocus()的实现中调节颜色。

@Override
public boolean shouldYieldFocus(JComponent input) {
    if (verify(input)) {
        tf.setValue(date);
        tf.setForeground(Color.black);
        return true;
    } else {
        tf.setForeground(Color.red);
        return false;
    }
}


java - 如何在不遵循RegEx格式的情况下更改JFormattedTextField输入文本的前景色/文本颜色?-LMLPHP

要在键入时查看更改,请使用DocumentListener以类似的方式评估DateFormat实例。

关于java - 如何在不遵循RegEx格式的情况下更改JFormattedTextField输入文本的前景色/文本颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37671340/

10-14 15:28
查看更多