调用requestFocus()方法,也就是edit.requestFocus()

解决方案 »

  1.   

    好像不行哦!li_haizhou(阿土) :您试过吗?就是我那个源程序,用jdk
      

  2.   

    必须在画面构筑好之后设置焦点才有效,用下面的试试import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ToolBar extends JFrame {
    JTextArea edit = new JTextArea(8, 40); public ToolBar() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon image1 = new ImageIcon("button1.gif");
    JButton button1 = new JButton(image1);
    ImageIcon image2 = new ImageIcon("button2.gif");
    JButton button2 = new JButton(image2);
    ImageIcon image3 = new ImageIcon("button3.gif");
    JButton button3 = new JButton(image3);
    JToolBar bar = new JToolBar();
    bar.add(button1);
    bar.add(button2);
    bar.add(button3);

    edit.setLineWrap(true);
    //edit.setCursor(null);
    JScrollPane scroll = new JScrollPane(edit);
    JPanel pane = new JPanel();
    setBounds(250,250,0,0);
    BorderLayout bord = new BorderLayout();
    pane.setLayout(bord);
    pane.add("North", bar);
    pane.add("Center", scroll);

    setContentPane(pane);
    }//Constructor

    public static void main(String[] args) {
    ToolBar frame = new ToolBar();
    frame.pack();
    frame.setVisible(true);
                edit.requestFocus();
    }
    }
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ToolBar extends JFrame {
    JTextArea edit = new JTextArea(8, 40); public ToolBar() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon image1 = new ImageIcon("button1.gif");
    JButton button1 = new JButton(image1);
    ImageIcon image2 = new ImageIcon("button2.gif");
    JButton button2 = new JButton(image2);
    ImageIcon image3 = new ImageIcon("button3.gif");
    JButton button3 = new JButton(image3);
    JToolBar bar = new JToolBar();
    bar.add(button1);
    bar.add(button2);
    bar.add(button3);

    edit.setLineWrap(true);
    //edit.setCursor(null);
    JScrollPane scroll = new JScrollPane(edit);
    JPanel pane = new JPanel();
    setBounds(250,250,0,0);
    BorderLayout bord = new BorderLayout();
    pane.setLayout(bord);
    pane.add("North", bar);
    pane.add("Center", scroll);

    setContentPane(pane);
    }//Constructor

    public static void main(String[] args) {
    ToolBar frame = new ToolBar();
    frame.pack();
    frame.setVisible(true);
                frame.edit.requestFocus();
    }
    }
    注意在main中要通过frame去访问类的edit部件:)
      

  4.   

    不行啊,各位大哥,编译都过不了ToolBar.java:41: cannot resolve symbol
    symbol  : variable edit
    location: class ToolBar
                    edit.requestFocus();
                    ^
    1 error
      

  5.   

    frame.edit.requestFocus();这样也不行
      

  6.   

    看我该好了
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ToolBar extends JFrame {

    public ToolBar() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon image1 = new ImageIcon("button1.gif");
    JButton button1 = new JButton(image1);
    ImageIcon image2 = new ImageIcon("button2.gif");
    JButton button2 = new JButton(image2);
    ImageIcon image3 = new ImageIcon("button3.gif");
    JButton button3 = new JButton(image3);
    JToolBar bar = new JToolBar();
    bar.add(button1);
    bar.add(button2);
    bar.add(button3);
    final JTextArea edit = new JTextArea(8, 40);
    edit.setLineWrap(true);
    //edit.setCursor(null);
    JScrollPane scroll = new JScrollPane(edit);
    JPanel pane = new JPanel();
    setBounds(250,250,0,0);
    BorderLayout bord = new BorderLayout();
    pane.setLayout(bord);
    pane.add("North", bar);
    pane.add("Center", scroll);
    setContentPane(pane);
    this.addWindowListener(new WindowAdapter(){
    public void windowOpened(WindowEvent e){
    edit.requestFocus();
    }
    });


    }//Constructor

    public static void main(String[] args) {
    ToolBar frame = new ToolBar();
    frame.pack();
    frame.setVisible(true);
    }
    }