获得焦点应该是在show出来之后,
public class MyFrame extends JFrame
{
jTextArea;
public void setMyFocus()
{
jTextArea.requestFocus();
}
public static void main(...)
{
MyFrame f = new ...;
f.show();//f.setVisible(true);
f.setMyFocus();
}
}
public class MyFrame extends JFrame
{
jTextArea;
public void setMyFocus()
{
jTextArea.requestFocus();
}
public static void main(...)
{
MyFrame f = new ...;
f.show();//f.setVisible(true);
f.setMyFocus();
}
}
package developproject;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class testFrame extends JFrame{
private JTextArea textArea;
private JTextField textField;
private JButton button; public testFrame() {
JPanel panel = new JPanel();
textArea = new JTextArea(8,40);
textField = new JTextField(6);
button = new JButton("Click");
JScrollPane scorllPane = new JScrollPane(textArea);
panel.add(scorllPane);
this.getContentPane().add(textField,"North");
this.getContentPane().add(panel,"Center");
this.getContentPane().add(button,"South");
this.setSize(500,300);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void init() {
textArea.requestFocus();
} public static void main(String[] args) {
testFrame f = new testFrame();
f.show();
f.init();
}
}
1。先show 后focus
2.把想要加焦点的第一个部署在容器中,然后默认焦点就是这个其实这些问题仔细看api都能解决