Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java textcomponent中的ASCII不可打印字符_Java_Swing_Ascii_Jtextcomponent - Fatal编程技术网

Java textcomponent中的ASCII不可打印字符

Java textcomponent中的ASCII不可打印字符,java,swing,ascii,jtextcomponent,Java,Swing,Ascii,Jtextcomponent,我的情况是,我必须扫描文本字段中的qrcode,我必须解析该文本字段中的非可打印字符(顺便说一句,它总是同一个字符),这些字符永远不会出现在同一位置 我尝试获取此代码,但今年没有成功 textfield.getText()失败 textfield.getDocument().getText(0,textfield.getDocument().getLength())失败 我成功地使用了KeyBindings,然后用glyph替换为@trashgod注释 例如: public class Te

我的情况是,我必须扫描文本字段中的qrcode,我必须解析该文本字段中的非可打印字符(顺便说一句,它总是同一个字符),这些字符永远不会出现在同一位置

我尝试获取此代码,但今年没有成功

  • textfield.getText()失败
  • textfield.getDocument().getText(0,textfield.getDocument().getLength())失败
我成功地使用了
KeyBindings
,然后用glyph替换为@trashgod注释

例如:

public class Test {

    private JPanel panel;
    private JTextArea textArea;

    public static final String GS_UNICODE = "\u241D";

    public Test(){
        panel = new JPanel();
        textArea = new JTextArea(10,30);
        final JTextField textfield = new JTextField(30);

        textfield.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8,0), "test");
        textfield.getActionMap().put("test", new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent e) {
                textfield.setText(textfield.getText()+GS_UNICODE);                
            }

        });



        textfield.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {                
                    textArea.setText(textfield.getText());                                   
            }
        });
        panel.add(textfield);
        JButton button = new JButton("Press non-readable");
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Robot robot = new Robot();
                    textfield.requestFocusInWindow();                    
                    robot.setAutoDelay(10);
                    robot.keyPress(KeyEvent.VK_F8);
                    robot.keyPress(KeyEvent.VK_H);
                    robot.keyPress(KeyEvent.VK_E);
                    robot.keyPress(KeyEvent.VK_L);
                    robot.keyPress(KeyEvent.VK_L);
                    robot.keyPress(KeyEvent.VK_O);
                    robot.keyPress(KeyEvent.VK_ENTER);  

                    SwingUtilities.invokeLater(new Runnable(){

                        @Override
                        public void run() {
                            //check if string has glyph
                           System.out.println(textfield.getText().contains(GS_UNICODE));
                           System.out.println(textfield.getText());
                        }

                    });

                } catch (AWTException ex) {
                    ex.printStackTrace();
                }

            }

        });
        panel.add(button);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("DataMatrix example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setLocationByPlatform(Boolean.TRUE);
        Test example =new Test();
        frame.add(example.panel,BorderLayout.CENTER);
        frame.add(example.textArea,BorderLayout.SOUTH);

        //Display the window.
        frame.pack();
        frame.setVisible(Boolean.TRUE);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


}
但我的问题是:


1) 有没有一种方法可以捕获不可打印的ASCII码(你能在这里发布这样一个字符串吗?你不能捕获哪些字符?你是在寻找这样的标志符号:?我不一样****,:-)以便更快地发布SSCCE/MCVE/MCTRE短的、可运行的、可编译的局部变量中的硬编码值,顺便说一句(??我肯定??)@Andrew Thompson在您的帖子和/或question@trashgod我希望编辑的问题更容易理解,是的,字形帮助很大:)@mKorbel我发布了一个例子