JFormattedTextField에 ActionListener를 추가하려고했습니다. 그것은 동안 청취자를 호출하지 않는 텍스트 변경, 초점 변경 :ActionListener를 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);
}
};
}
JFormattedTextField에는에 ActionListener를 설정하는 올바른 방법은 무엇입니까? 텍스트 변경이나 변화에 초점을하면서
동일한 수신기를 둘 이상의 사용자 인터페이스에 추가 할 수 있습니다! – AVA
@AVA 네, 추가 할 수 있습니다. 이벤트의 출처를 확인해야하는 경우에 한합니다. – Beniton