【问题标题】:How to switch from 1 jtextfield to another in java [duplicate]如何在java中从1个jtextfield切换到另一个[重复]
【发布时间】:2022-11-05 00:54:22
【问题描述】:

我想切换 jtextfields,当我按下按钮时,例如

|领域| 字段 -> 右键

场地 |领域|

但我不知道怎么做。 我的代码:

public class InFile extends JFrame implements ActionListener{

public InFile() throws IOException{
  super();
  // I skipped setup etc.
  MyKeyAdapter m = new MyKeyAdapter();
  jTextField1.addKeyListener(m);
}

public class MyKeyAdapter extends KeyAdapter{    
  @Override
  public void keyPressed(KeyEvent e){} 
  @Override
  public void keyReleased(KeyEvent e){
      if (e.getSource() == jTextField1 && e.getKeyCode() == e.VK_DOWN) {
      //this is where i want to switch fields
      System.out.println("hi");
      } // end of if
  }
}

代码有效,如果我在编辑 TextField1 时按下它会打印 hi,所以我只想将我正在编辑的字段切换到 TextField2

【问题讨论】:

  • 不是关于上面的:它没有给出关于按键的直接答案,但它确实向您展示了如何将焦点从一个字段更改为另一个字段,您只需将这些片段与您的代码连接在一起。
  • Tab 键带您前进通过JTextFields。 Shift + Tab 将您带到上一个JTextFields

标签: java jtextfield


【解决方案1】:

我使用jTextField2.requestFocusInWindow(); 将焦点转移到 textfield2 上。

How to Set Focus on JTextField? 回答了我的问题

【讨论】:

  • requestFocus() 方法的 API 声明:developers are strongly encouraged to use requestFocusInWindow when possible
猜你喜欢
  • 2017-05-03
  • 2013-05-18
  • 2014-12-04
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多