簡體   English   中英

在jTextfield中鍵入值后,是否有可能在不使用jButton的情況下自動按下?

[英]Is it possible that after typing a value in jTextfield it will automatically press without using 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();
    }

}

因此,這使用了Swing Timer ,該Timer在文檔事件發生與觸發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());
    }
});

並根據需要進行修改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM