这是在Java中将FocusListener添加到JTextFields的正确方法吗?

我在Java应用程序中有数百个JTextFields ,我想在所有这些上添加FocusListener设置文本的水平对齐并在每个文本字段上添加FocusListener 。 所以,我做了这个方法,它的工作很棒。 但我只是想知道这种方法是否正确或有什么不对,或者我反对某种OOP规则?

这是代码

 public void CreateFocusListenerForFields(JTextField txt) { txt.setHorizontalAlignment(JTextField.RIGHT); txt.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { } @Override public void focusLost(FocusEvent e) { if(!NumberUtils.isNumber(txt.getText())) { txt.setBackground(new Color(254,157,157)); txt.requestFocus(); } else { txt.setBackground(Color.white); } } }); } 

并在我的文本字段上应用此方法

 CreateFocusListenerForFields(MyTextField); 

现在,当我运行代码时,它很有效,只是想知道这是否正确,如果没有那么当你必须在数百个字段上设置对齐和focuslistener时,另一种方法是什么? 谢谢你的好意见。

同样,我的偏见是使用InputVerifier而不是FocusListener,如果仅因为InputVerifier是更高级别的构造,并且在适用的情况下使用它们代替较低级别(更接近金属)构造似乎总是更安全。

两者的一个例子:

 import java.awt.Color; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.*; import javax.swing.text.JTextComponent; public class TestFieldVerification { public static final Color ERROR_COLOR = new Color(254,157,157); private static final int COLS = 8; private JPanel mainPanel = new JPanel(); private JTextField verifiedField = new JTextField(COLS); private JTextField focusCheckedField = new JTextField(COLS); private Color defaultBackground = null; public TestFieldVerification() { verifiedField.setInputVerifier(new MyVerfier(this)); focusCheckedField.addFocusListener(new MyFocusCheck(this)); mainPanel.add(new JLabel("With InputVerfier:")); mainPanel.add(verifiedField); mainPanel.add(new JLabel("With FocusListener:")); mainPanel.add(focusCheckedField); } public boolean verifyText(String text) { try { Integer.parseInt(text); return true; } catch (NumberFormatException nfe) { return false; } } public void setFieldBackground(JComponent component, boolean verified) { if (defaultBackground == null) { defaultBackground = component.getBackground(); } Color bg = verified ? defaultBackground : ERROR_COLOR; component.setBackground(bg); } public JComponent getMainPanel() { return mainPanel; } private static void createAndShowGui() { JFrame frame = new JFrame("MyVerifier"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TestFieldVerification().getMainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class MyVerfier extends InputVerifier { private TestFieldVerification gui; public MyVerfier(TestFieldVerification gui) { this.gui = gui; } @Override public boolean shouldYieldFocus(JComponent input) { gui.setFieldBackground(input, super.shouldYieldFocus(input)); return super.shouldYieldFocus(input); } @Override public boolean verify(JComponent input) { String text = ((JTextComponent) input).getText(); return gui.verifyText(text); } } class MyFocusCheck extends FocusAdapter { private TestFieldVerification gui; public MyFocusCheck(TestFieldVerification gui) { this.gui = gui; } @Override public void focusLost(FocusEvent e) { JTextComponent textComp = (JTextComponent) e.getSource(); String text = textComp.getText(); boolean verified = gui.verifyText(text); gui.setFieldBackground(textComp, verified); if (!verified) { textComp.requestFocusInWindow(); } } } 

我可能会创建一个工厂类,然后我用它来创建我的所有文本字段 – 我认为这更像是“正确的操作OOP”,例如:

 public class MyTextFieldFactory { private static final FocusListener defaultFocusListener = new FocusListener() { @Override public void focusGained(FocusEvent e) { } @Override public void focusLost(FocusEvent e) { // Your code ... } }; public static JTextField createDefaultTextField() { JTextField textField = new JTextField(); textField.setHorizontalAlignment(JTextField.RIGHT); textField.addFocusListener(defaultFocusListener); return textField; } } 

然后创建我的所有文本字段:

 JTextField myNewTextField = MyTextFieldFactory.createDefaultTextField(); 

这样做的另一个好处是,您只需为每个文本字段创建一个焦点侦听器实例,而不是一个新实例。