Ich habe versucht, einen ActionListener zu einem JFormattedTextField hinzuzufügen. Es ist nicht berufen, den Hörer, während Text ändern oder Fokusänderung:Wie setze ich einen ActionListener auf ein JFormattedTextField?
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ActionListenerExample extends JPanel {
public static void main(String[] args) {
JFrame f = new JFrame("Action Listener Example : ");
JPanel anyBICPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1));
anyBICPanel.setName("anyBICPanel");
JLabel anyBICLbl = new JLabel("Any BIC");
anyBICLbl.setName("anyBICLbl");
anyBICPanel.add(anyBICLbl);
JFormattedTextField anyBICTxt = new JFormattedTextField();
anyBICTxt.setName("anyBICTxt");
anyBICTxt.setColumns(30);
anyBICPanel.add(anyBICTxt);
anyBICTxt.addActionListener(anyBICActionListener);
JTextField tf2 = new JTextField(5);
anyBICPanel.add(tf2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(anyBICPanel);
f.pack();
f.setVisible(true);
}
public static ActionListener anyBICActionListener = new ActionListener() {
public void actionPerformed(ActionEvent evnt) {
String text = ((JFormattedTextField) evnt.getSource()).getText().toString();
JOptionPane.showMessageDialog(null, "Text : " + text);
}
};
}
Was ist der richtige Weg, um eine Action auf einen JFormattedTextField zu setzen?
Derselbe Listener kann zu mehr als einer Benutzerschnittstelle hinzugefügt werden! – AVA
@AVA ja es kann hinzugefügt werden. vorausgesetzt, Sie müssen die Quelle des Ereignisses überprüfen. – Beniton