我计划做的是当我按下Enter
键时,应用程序将启动按钮btn_teach
,并切换到另一个具有textfield
焦点的选项卡。现在,当我实现它时,如果我手动按下按钮(而不是按下Enter
),它就可以完美地工作。当我实际按下Enter
时,选项卡会切换回来,但该选项卡上的文本字段没有聚焦。
tp
是JTabbedPane
. 我的计划是从标签索引 1 切换到标签索引 0 并设置txt_send
焦点
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
String say = txt_saypane.getText();
String ans = txt_anspane.getText();
//this.clear();
say = say.replace("\n","");
ans = ans.replace("\n","");
this.talk(this.botTeach(say,ans), false);
tp.setSelectedIndex(0);
}
public void stateChanged(ChangeEvent arg0)
{
// TODO Auto-generated method stub
int sel = tp.getSelectedIndex();
if(sel == 0)
txt_send.requestFocusInWindow();
if(sel == 1)
txt_saypane.requestFocusInWindow();
}
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
if(txt_saypane.isFocusOwner() || txt_anspane.isFocusOwner())
btn_teach.doClick();
}
}
KeyListener
在选项卡之间切换时更改焦点的正确方法是什么JTabbedPane
?