我已经用Java为JTextField Focus Listener编写了一个类,但我不知道如何将该类编写为通用类。一旦编写了通用类,所有JTextField都应使用该类,我该如何做到这一点,请告诉我编码的方式。如何为以下代码编写通用类:
package javaapplication7;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class testfocuss{
public static void main(String[] argv) throws Exception {
JTextField component = new JTextField(20);
JTextField component1 = new JTextField(20);
JTextField component2 = new JTextField(20);
JTextField component3 = new JTextField(20);
component.addFocusListener(new MyFocusListener());
component1.addFocusListener(new MyFocusListener());
component2.addFocusListener(new MyFocusListener());
component3.addFocusListener(new MyFocusListener());
JFrame f = new JFrame();
f.layout(new FlowLayout());
f.add(component1);
f.add(component);
f.add(component2);
f.add(component3);
f.pack();
f.setVisible(true);
}
}
class MyFocusListener extends FocusAdapter {
//boolean showingDialog = false;
public void focusGained(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
c.requestFocus();
c.selectAll();
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
c.setSelectionStart(i);
c.setSelectionEnd(i);
break;
}
}
}
public void focusLost(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
if (evt.isTemporary()) {
return;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
//System.out.println("must only contain digits");
c.requestFocus();
c.selectAll();
break;
}
}
}
}
最佳答案
试试这个
package javaapplication7;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class testfocuss {
public static void main(String[] argv) throws Exception {
JTextField component = new JTextField(20);
JTextField component1 = new JTextField(20);
JTextField component2 = new JTextField(20);
JTextField component3 = new JTextField(20);
MyFocusListener l = new MyFocusListener();
component.addFocusListener(l);
component1.addFocusListener(l);
component2.addFocusListener(l);
component3.addFocusListener(l);
JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.add(component1);
f.add(component);
f.add(component2);
f.add(component3);
f.pack();
f.setVisible(true);
}
}
class MyFocusListener<C extends JTextComponent> extends FocusAdapter {
// boolean showingDialog = false;
public void focusGained(FocusEvent evt) {
final C c = (C) evt.getSource();
String s = c.getText();
c.requestFocus();
c.selectAll();
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
c.setSelectionStart(i);
c.setSelectionEnd(i);
break;
}
}
}
public void focusLost(FocusEvent evt) {
final C c = (C) evt.getSource();
String s = c.getText();
if (evt.isTemporary()) {
return;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
// System.out.println("must only contain digits");
c.requestFocus();
c.selectAll();
break;
}
}
}
}
注意:您的听众可以使用多次。