java JTextField编辑框自动获取焦点光标,以及对编辑框内容监听

    技术2023-12-22  76

    焦点:

    1、  jf是Jframe对象,in是JTextField对象 

    jf.addWindowListener(new WindowAdapter() { public void windowOpened( WindowEvent e ){ in.requestFocus(); //获取焦点 } public void windowClosing(WindowEvent e) { int result = JOptionPane.showConfirmDialog(null, "是否要退出?", "退出确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (result == JOptionPane.YES_OPTION) System.exit(0); } });

    2、

    // 当窗口关闭时,需要释放串口资源。 this.addWindowListener(new WindowAdapter() { public void windowOpened( WindowEvent e ){ in.requestFocus(); //获取焦点 } public void windowClosing(WindowEvent arg0) { if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog( FileEntry.this, "是否要退出?", "警告", JOptionPane.YES_NO_OPTION)) { System.out.println("system closed"); System.exit(0); } } });

    编辑框内容输入监听:

    1、此方法在扫码输入,数据录入较快时有效

    //编辑框的监听 taskCode.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String taskCodeNumber = taskCode.getText(); System.out.println("输入的内容="+taskCodeNumber); } });

    2、此方法,在手动输入时,可以监听到,输入的每一位数据

    // 对边框内容的实时监听 taskCode.getDocument().addDocumentListener(new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { // TODO Auto-generated method stub // 此处一般在 insertUpdate 中有耗时操作后,执行 try { Document doc = e.getDocument(); String strMoney; strMoney = doc.getText(0, doc.getLength()); System.out.println("监听removeUpdate-->" + strMoney); } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } // 这里实时监控到你输入的每一位 @Override public void insertUpdate(DocumentEvent e) { // TODO Auto-generated method stub try { Document doc = e.getDocument(); String strMoney = doc.getText(0, doc.getLength()); System.out.println("监听insertUpdate-->" + strMoney); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void changedUpdate(DocumentEvent e) { // TODO Auto-generated method stub try { Document doc = e.getDocument(); String strMoney = doc.getText(0, doc.getLength()); System.out.println("changedUpdate-->" + strMoney); } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } });

     结束;

     

    Processed: 0.010, SQL: 9