我在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;
}
}
}
}
答案 0 :(得分:1)
试试这个
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;
}
}
}
}
注意:您的监听器可以多次使用。
答案 1 :(得分:0)
我认为您的代码不需要任何泛型; MyFocusListener
已经适用于所有JTextComponents
,而不仅仅适用于JTextFields
,并且它不适用于任何其他类,因为它使用JTextComponent
中声明的方法。您已经可以在其他应用程序中使用它。
如果您仍想让听众使用泛型(作业?),请使用:
class MyFocusListener<T extends JTextComponent> extends FocusAdapter {
public void focusGained(FocusEvent evt) {
final T c = (T)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 T c = (T) evt.getSource();
String s = c.getText();
if (evt.isTemporary()) {
return;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
c.requestFocus();
c.selectAll();
break;
}
}
}
}