Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望覆盖getText(),以便它可以返回一个空值而不是一个空白值。我注意到这getText()是其中的一部分,JTextComponent但我一直无法成功覆盖它。获得预期结果的最佳方法是什么?
getText()
JTextComponent
您可以子类JTextField化并覆盖该getText方法,因此
JTextField
getText
public final class JCustomTextField extends JTextField{ . . . @Override public String getText(){ // do stuff } }
或者您可以内联覆盖,例如
JTextField textField = new JTextField(){ . . . @Override public String getText(){ // do stuff } };