请教。。。。。

yadang 2002-02-01 11:27:23
在一个对话框有一个文本框,一个按钮。怎样在显示这个框时让输入焦点自动定在文本框,而不是按钮上面??
下面2位的方法不好用。调用textarea.requestFocus()后,当打开对话框时,输入焦点还是自动落在按钮上面。


回复贴子:
回复人: gdsean(摇滚java) (2002-1-31 17:59:56) 得5分
textarea.requestFocus();
回复人: skyyoung(路人甲) (2002-1-31 18:00:26) 得5分
transferFocus();

...全文
41 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Realize 2002-02-01
如果均是同一级别的组件,和先后顺序有关!(如果没有显式地指定ORDER!)
回复
skyyoung 2002-02-01
如果先落到按钮上,就调用transferFocus()回到文本框里。
回复
gdsean 2002-02-01
代码看看可以吗?
回复
gdsean 2002-02-01
这里:
void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
getContentPane().add(panel1);
jTextField1.requestFocus();
panel1.add(jTextField1, new XYConstraints(48, 38, 69, -1));
panel1.add(jTextField2, new XYConstraints(181, 38, 79, -1));
panel1.add(jButton1, new XYConstraints(108, 98, -1, -1));
}
回复
yadang 2002-02-01
请看下面的程序:
为什么点击按钮,输入焦点可以切换到别的控件。而当对话框显示时为什么不能自动切换?


package testjava;

import javax.swing.UIManager;
import java.awt.*;

public class App1 {
boolean packFrame = false;

/**Construct the application*/
public App1() {
Dialog1 frame = new Dialog1();

frame.show();
frame.jTextField1.transferFocus();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**Main method*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new App1();
}
}


package testjava;

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;

public class Dialog1 extends JDialog {
JPanel panel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();

public Dialog1(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}

public Dialog1() {
this(null, "", false);
}
void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setNextFocusableComponent(jTextField2);
getContentPane().add(panel1);
panel1.add(jTextField1, new XYConstraints(48, 38, 69, -1));
panel1.add(jTextField2, new XYConstraints(181, 38, 79, -1));
panel1.add(jButton1, new XYConstraints(108, 98, -1, -1));
}

void jButton1_actionPerformed(ActionEvent e) {
jTextField1.transferFocus();
}
}
回复
yadang 2002-02-01
在哪里可以指定一个控件的ORDER??怎么没找到?
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2002-02-01 11:27
社区公告
暂无公告