是否可以檢查jtextfield是否已被選中/取消選定(即文本字段已被點擊且光標現在在該字段內)?檢查是否選擇了JTextfield
//編輯 得益於以下這裏的幫助是一個工作示例
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class test extends JFrame {
private static JPanel panel = new JPanel();
private static JTextField textField = new JTextField(20);
private static JTextField textField2 = new JTextField(20);
public test() {
panel.add(textField);
panel.add(textField2);
this.add(panel);
}
public static void main(String args[]) {
test frame = new test();
frame.setVisible(true);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
System.out.println("selected");
}
@Override
public void focusLost(FocusEvent e) {
System.out.println("de-selected");
}
});
}
}
請學習Java命名約定並嚴格遵守 – kleopatra