我想将焦点设置在作为对象消息传递给 JOptionPane 的特定 JTextField 上。这是我的代码(我希望将重点放在 txt2 上,但重点始终放在 txt1 上):
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
private JTextArea txt1 = new JTextArea();
private JTextArea txt2 = new JTextArea();
public TextArea()
{
setLayout(null);
setPreferredSize(new Dimension(200,100));
txt1.setBounds (20, 20, 220, 20);
txt2.setBounds (20, 45, 220, 20);
txt1.setText("Text Field #1");
txt2.setText("Text Field #2");
add(txt1);
add(txt2);
txt2.requestFocus();
}
private void display()
{
Object[] options = {this};
JOptionPane pane = new JOptionPane();
pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, txt2);
}
public static void main(String[] args)
{
new TextArea().display();
}
}