将所有输入捕获到 JTextField 中
您好,我在我的 swing 表单中使用了 4 个 JTextField。我需要做的是通过按 Enter 键和 Tab 键将焦点从一个 JTextField 移动到另一个 JTextField。我成功完成了它,但问题是 JTextField 只允许数字值,如果我在 J Textfield 中给出除数值以外的任何值,JTextField 被锁定,并且 JTextFields 之间不会发生焦点转移。我使用的代码。
package focus;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class Main implements KeyListener{
JTextField txt1 = new JTextField(20);
JTextField txt2 = new JTextField(20);
JTextField txt3 = new JTextField(20);
JTextField txt4 = new JTextField(20);
JFrame f = new JFrame();
public Main()
{
txt1.addFocusListener(new MyFocusListener<JTextField>());
txt2.addFocusListener(new MyFocusListener<JTextField>());
txt3.addFocusListener(new MyFocusListener<JTextField>());
txt4.addFocusListener(new MyFocusListener<JTextField>());
txt1.addKeyListener(this);
txt2.addKeyListener(this);
txt3.addKeyListener(this);
txt4.addKeyListener(this);
f.setLayout(new FlowLayout());
f.add(txt1);
f.add(txt2);
f.add(txt3);
f.add(txt4);
f.pack();
f.setVisible(true);}
public static void main(String[] argv) throws Exception {
new Main();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent ke) {
if (ke.getSource() == txt1)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt2.requestFocusInWindow();
txt2.selectAll();
}
}
else if (ke.getSource() == txt2)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt3.requestFocusInWindow();
txt3.selectAll();
}
}
else if (ke.getSource() == txt3)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt4.requestFocusInWindow();
txt4.selectAll();
}
}
else if (ke.getSource() == txt4)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt1.requestFocusInWindow();
txt1.selectAll();
}
}
}
public void keyReleased(KeyEvent e) {
}
}
class MyFocusListener <C extends JTextField> extends FocusAdapter {
//boolean showingDialog = false;
public void focusGained(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
c.requestFocus();
c.selectAll();
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
c.setSelectionStart(i);
c.setSelectionEnd(i);
break;
}
}
}
public void focusLost(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
if (evt.isTemporary()) {
return;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
//System.out.println("must only contain digits");
c.requestFocus();
c.selectAll();
break;
}
}
}
}
Hi i used 4 JTextField in my swing form.i need to do is moving the focus from one JTextField to the other JTextField by hit the enter key and tab key.i done it successfully but the problem was the JTextField allows only numeric values if i give any value other than numeric value in the J Textfield the JTextField is locked and there is no focus transfer happen between the JTextFields.the code i was used .
package focus;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class Main implements KeyListener{
JTextField txt1 = new JTextField(20);
JTextField txt2 = new JTextField(20);
JTextField txt3 = new JTextField(20);
JTextField txt4 = new JTextField(20);
JFrame f = new JFrame();
public Main()
{
txt1.addFocusListener(new MyFocusListener<JTextField>());
txt2.addFocusListener(new MyFocusListener<JTextField>());
txt3.addFocusListener(new MyFocusListener<JTextField>());
txt4.addFocusListener(new MyFocusListener<JTextField>());
txt1.addKeyListener(this);
txt2.addKeyListener(this);
txt3.addKeyListener(this);
txt4.addKeyListener(this);
f.setLayout(new FlowLayout());
f.add(txt1);
f.add(txt2);
f.add(txt3);
f.add(txt4);
f.pack();
f.setVisible(true);}
public static void main(String[] argv) throws Exception {
new Main();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent ke) {
if (ke.getSource() == txt1)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt2.requestFocusInWindow();
txt2.selectAll();
}
}
else if (ke.getSource() == txt2)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt3.requestFocusInWindow();
txt3.selectAll();
}
}
else if (ke.getSource() == txt3)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt4.requestFocusInWindow();
txt4.selectAll();
}
}
else if (ke.getSource() == txt4)
{
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
txt1.requestFocusInWindow();
txt1.selectAll();
}
}
}
public void keyReleased(KeyEvent e) {
}
}
class MyFocusListener <C extends JTextField> extends FocusAdapter {
//boolean showingDialog = false;
public void focusGained(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
c.requestFocus();
c.selectAll();
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
c.setSelectionStart(i);
c.setSelectionEnd(i);
break;
}
}
}
public void focusLost(FocusEvent evt) {
final JTextComponent c = (JTextComponent) evt.getSource();
String s = c.getText();
if (evt.isTemporary()) {
return;
}
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
//System.out.println("must only contain digits");
c.requestFocus();
c.selectAll();
break;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像那样,因为你让它表现得像那样:)
修复: 在
focusLost
中,替换if (!Character.isDigit(s.charAt(i)) ))
与
if (!Character.isLetterOrDigit(s.charAt(i)))
或简单地,
It was like that because you made it behave like that :)
FIX : In
focusLost
, replace theif (!Character.isDigit(s.charAt(i)))
with
if (!Character.isLetterOrDigit(s.charAt(i)))
or simply,
focusGained()
中调用c.requestFoucs()
。组件已经获得焦点,这就是调用此方法的原因。focusLost()< 中调用
c.requestFocus()
/code> 方法。因此,一旦组件(textField)失去焦点,并且由于包含非数字字符,它会再次获得焦点,并且您看不到任何焦点遍历。”c.requestFoucs()
infocusGained()
. Component already got the focus, that's why this method is called.c.requestFocus()
infocusLost()
method. So once focus is lost from component(textField) and because contains non-numeric characters it again gains focus and you can't see any focus traversal."