在jTextfield中输入一个值后,它是否可能在不使用jButton的情况下自动按下?



在jTextfield中输入一个值后,它是否可能在不使用jButton的情况下自动按下? 我不知道是否可能......扫描后在我的条形码扫描仪中使用它并显示jTextfield将自动接收的值并开始对数据库进行查询

答案取决于你对"新闻"的定义。 根据我的经验,期望是,当扫描条形码时,无需用户执行任何额外操作即可执行操作

基于这个假设,你有两个基本的选择

固定长度

如果您知道条形码的长度(并且它是恒定的),则可以使用DocumentFilter来检测何时达到长度并触发操作

public class BarCodeLengthDocumentListener implements DocumentListener {
private ActionListener actionListener;
private int barCodeLength;
public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) {
this.actionListener = actionListener;
this.barCodeLength = barCodeLength;
}
@Override
public void insertUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
doCheck(e);
}
protected void doCheck(DocumentEvent e) {
Document doc = e.getDocument();
if (doc.getLength() >= barCodeLength) {
try {
String text = doc.getText(0, doc.getLength());
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
actionListener.actionPerformed(evt);
} catch (BadLocationException exp) {
exp.printStackTrace();
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null);
actionListener.actionPerformed(evt);
}
}
}
}

因此,基本上,这允许您指定条形码的预期长度,当达到它时,它将触发ActionListener,通过ActionEvent传递文本

延迟操作

如果您不知道长度(或它是可变的),另一种选择是在文档事件发生和触发ActionListener之间注入某种延迟

public class DelayedDocumentListener implements DocumentListener {
private ActionListener actionListener;
private String text;
private Timer timer;
public DelayedDocumentListener(ActionListener actionListener) {
this.actionListener = actionListener;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
actionListener.actionPerformed(evt);
}
});
timer.setRepeats(false);
}
@Override
public void insertUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
doCheck(e);
}
protected void doCheck(DocumentEvent e) {
try {
Document doc = e.getDocument();
text = doc.getText(0, doc.getLength());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
timer.restart();
}
}

因此,这使用了一个 SwingTimer,它在文档事件发生和触发ActionListener之间生成延迟(在本例中为 1 秒),每个新文档事件都会中断Timer,导致它重新启动。 这意味着在最后一个文档事件和触发ActionListener之间必须至少有 1 秒。

因为有时人们需要手动输入条形码,所以您可能希望利用这种延迟。

可运行的示例...

因此,这基本上代表了这两种想法,它使用java.awt.Robot将击键注入键盘缓冲区,这应该模拟大多数条形码扫描仪。

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
BarCodeLengthDocumentListener lengthListener = new BarCodeLengthDocumentListener(7, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
JOptionPane.showMessageDialog(TestPane.this, text);
}
});
DelayedDocumentListener delayedListener = new DelayedDocumentListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
JOptionPane.showMessageDialog(TestPane.this, text);
}
});
JTextField field1 = new JTextField(7);
field1.getDocument().addDocumentListener(lengthListener);
JTextField field2 = new JTextField(7);
field2.getDocument().addDocumentListener(delayedListener);
add(field1);
add(field2);
JButton simLength = new JButton("Simulate Length");
simLength.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.setText(null);
field1.requestFocusInWindow();
Thread t = new Thread(new Simulator());
t.start();
}
});
JButton simDelay = new JButton("Simulate Delay");
simDelay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field2.setText(null);
field2.requestFocusInWindow();
Thread t = new Thread(new Simulator());
t.start();
}
});
add(simLength);
add(simDelay);
}
}
public class Simulator implements Runnable {
@Override
public void run() {
try {
Robot bot = new Robot();
type(KeyEvent.VK_1, bot);
type(KeyEvent.VK_2, bot);
type(KeyEvent.VK_3, bot);
type(KeyEvent.VK_4, bot);
type(KeyEvent.VK_5, bot);
type(KeyEvent.VK_6, bot);
type(KeyEvent.VK_7, bot);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
protected void type(int keyStoke, Robot bot) {
bot.keyPress(keyStoke);
bot.keyRelease(keyStoke);
}
}
public class BarCodeLengthDocumentListener implements DocumentListener {
private ActionListener actionListener;
private int barCodeLength;
public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) {
this.actionListener = actionListener;
this.barCodeLength = barCodeLength;
}
@Override
public void insertUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
doCheck(e);
}
protected void doCheck(DocumentEvent e) {
Document doc = e.getDocument();
if (doc.getLength() >= barCodeLength) {
try {
String text = doc.getText(0, doc.getLength());
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
actionListener.actionPerformed(evt);
} catch (BadLocationException exp) {
exp.printStackTrace();
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null);
actionListener.actionPerformed(evt);
}
}
}
}
public class DelayedDocumentListener implements DocumentListener {
private ActionListener actionListener;
private String text;
private Timer timer;
public DelayedDocumentListener(ActionListener actionListener) {
this.actionListener = actionListener;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text);
actionListener.actionPerformed(evt);
}
});
timer.setRepeats(false);
}
@Override
public void insertUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
doCheck(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
doCheck(e);
}
protected void doCheck(DocumentEvent e) {
try {
Document doc = e.getDocument();
text = doc.getText(0, doc.getLength());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
timer.restart();
}
}
}

在jTextfield中输入一个值后,它是否有可能在不使用jButton的情况下自动按下?

您可以向文本字段添加ActionListener

当文本字段具有焦点并按 Enter 键时,将调用侦听器。

初始化文本字段后添加一个 KeyListener:

textfield.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("typed: "+e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("released: "+e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("pressed: "+e.getKeyChar());
}
});

并根据需要进行修改

相关内容

最新更新