En maade at goere det paa:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class StubbornGUI extends JFrame {
private JTextField tf;
public StubbornGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
tf = new JTextField();
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(StubbornGUI.this, "tf=" + tf.getText());
}
});
tf.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
tf.requestFocus();
}
});
getContentPane().add(tf, BorderLayout.CENTER);
getContentPane().add(new JTextField(), BorderLayout.NORTH);
getContentPane().add(new JTextField(), BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
StubbornGUI f = new StubbornGUI();
f.setVisible(true);
f.tf.requestFocus();
}
});
}
}