SWT中的TXT文本框怎么响应回车键事件啊

GOALSTAR 2006-04-13 11:35:56
请问我在界面上几个对象,文本框,下拉框,按钮,我想通过回车来从一个文本框跳到另外一个,好像Tab键一样的功能。

谢谢~~~~~~~~~~

...全文
1180 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zclgod 2006-05-11
  • 打赏
  • 举报
回复
学习
hsd2008 2006-04-28
  • 打赏
  • 举报
回复
SWT这样也行。按回车键后变为TAB
TraverseListener traverseListener = new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.keyCode == 13) {
e.detail = SWT.TRAVERSE_TAB_NEXT;
e.doit = true;
}
}
};

text1.addTraverseListener(traverseListener);
text2.addTraverseListener(traverseListener);
btb368 2006-04-27
  • 打赏
  • 举报
回复
你看看这个简单的程序:
/**
* Author:btb8668@hotmail.com
* Version:1.0
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* The class will be test the Enter key
*/
public class TestFrame2 implements ActionListener {

public static String str ;
private static JFrame jfr = new JFrame("Frame");

JButton jbt_ok = new JButton("OK");
JButton jbt_cal = new JButton("CANCLE");
JTextField jtf = new JTextField(20);


public static JTextArea jta = new JTextArea("",20,50);

public TestFrame2(){
//add jbt_ok to JFrame
jfr.setLayout(new FlowLayout());
jbt_ok.addActionListener(this);
jfr.add(jbt_ok);

jbt_cal.addActionListener(this);
jfr.add(jbt_cal);

jtf.addKeyListener(new koLis());
jfr.add(jtf);

jfr.setSize(300,200);
jfr.setVisible(true);


}
public static void main(String[] args){
new TestFrame2();
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="OK"){
jtf.setText("test");
}
if(e.getActionCommand()=="CANCLE"){
jtf.setText("");
}
}


/**
* The class implement a Listener for jtf
*/
class koLis extends KeyAdapter {

public void keyPressed(KeyEvent e){
if(e.getKeyCode()==10){
jtf.setText("Enter pressed");
}
}

}


}
ciahi 2006-04-27
  • 打赏
  • 举报
回复
TextListener好像也可以吧
windforce9811 2006-04-26
  • 打赏
  • 举报
回复
ActionListener+Robot就可以搞定
Rockey 2006-04-26
  • 打赏
  • 举报
回复
SWT里可以用AWT的事件监听器?
qufan 2006-04-24
  • 打赏
  • 举报
回复
ItemStateListener接口
此接口在那些想具有处理同意form中相关组件之间的内部状态变化能力的方法中实现,接口必须实现
void itemStateChanged(Item item) 方法,当指定Item发生如下几种变化的情况:
changes the set of selected values in a ChoiceGroup; //choiceGroup的值发生变化
adjusts the value of an interactive Gauge; //调节相互度量的值(相对的值???)
enters or modifies the value in a TextField; //输入或者更改了文本框中的值
enters a new date or time in a DateField; and //输入新的数据或者事件在数据域
回车算不算呢???
cswei1021 2006-04-22
  • 打赏
  • 举报
回复
就监听ActionListener, 文本中的回车事件+按钮单击,对。建议去把JFC的Tutorial中相关章节看一遍。
suncheng_hong 2006-04-21
  • 打赏
  • 举报
回复
ActionLinstener 可以监听文本的回车事件,和按钮单击事件
Rockey 2006-04-21
  • 打赏
  • 举报
回复
问一下java_augur兄:
combo.addListener (SWT.DefaultSelection, new Listener ()
这句加的监听器是SelectionListener吗?SelectionListener可以像AWT里的ActionListener一样监听Text的回车事件吗?
java_augur 2006-04-20
  • 打赏
  • 举报
回复
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

public class CheckCREvent {

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout (new RowLayout ());
Combo combo = new Combo (shell, SWT.NONE);
combo.setItems (new String [] {"A-1", "B-1", "C-1"});
Text text = new Text (shell, SWT.SINGLE | SWT.BORDER);
text.setText ("some text");
combo.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event e) {
System.out.println (e.widget + " - Default Selection");
}
});
text.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event e) {
System.out.println (e.widget + " - Default Selection");
}
});
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
ciahi 2006-04-20
  • 打赏
  • 举报
回复
也有一个方法:
public void action(Event e,Object o)
{
if(e.target ==? )
{
if(……)
{……}
}
}
比较好用,但是个比较老的方法
ai_Fei 2006-04-16
  • 打赏
  • 举报
回复
/**
*author ai_Fei
*/

JTextField.txt = new JTextField(10);

txt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ if(e.getSource() == tex)){ Write method of want you want in this area; } }});
ai_Fei 2006-04-16
  • 打赏
  • 举报
回复
/**
*
*/

JTextField.txt = new JTextField(10);

addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ if(e.getSource() == tex)){ Write method of want you want in this area; } }});
ai_Fei 2006-04-16
  • 打赏
  • 举报
回复
实话跟你说:

ActionLinstener 完全可以监听到了
Voxer 2006-04-16
  • 打赏
  • 举报
回复
swing下这样
passwordField.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
。。。。。

}
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}

}
});
Aogu_Xt 2006-04-14
  • 打赏
  • 举报
回复
应该用KeyListener可以实现吧。在文本框里加一个KeyListener~~判断是不是回车事件~~
个人想法。没有真正的试过~~
GOALSTAR 2006-04-14
  • 打赏
  • 举报
回复
具体一点比较好
jesse8013 2006-04-14
  • 打赏
  • 举报
回复
ketstroke

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧