我想将在程序一运行时,就把焦点设置在一个JTextArea内,该用什么函数? 调用requestFocus()方法,也就是edit.requestFocus() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像不行哦!li_haizhou(阿土) :您试过吗?就是我那个源程序,用jdk 必须在画面构筑好之后设置焦点才有效,用下面的试试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(); }} 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部件:) 不行啊,各位大哥,编译都过不了ToolBar.java:41: cannot resolve symbolsymbol : variable editlocation: class ToolBar edit.requestFocus(); ^1 error frame.edit.requestFocus();这样也不行 看我该好了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); }} 并发的问题 tomcat 连接池 无法关闭 java仿windows图片查看器制作问题..... 一个程序的问题 Statement执行SQL语句时遇到的日期格式难题 JDBC-ODBC桥的连接,报的错误是什么意思啊? 多维数组定义问题!求人帮忙了! 关于FileOutputStream类的问题? 哈哈,5个角角了,散、我散、我散散散...... 请问一个入门级别的问题,大家帮忙看看: 大家快快来救救...我..啊. 高分求助! 编写ftp客户端,如何控制time out时间
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();
}
}
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部件:)
symbol : variable edit
location: class ToolBar
edit.requestFocus();
^
1 error
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);
}
}