我希望将焦点设置在一个特定的JTextField上,该JTextField作为Object Message传递给JOptionPane。这是我的代码(我希望焦点在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();
}
}