本文目录
JAVA 判断字符串是否由(0-9,A-Z,a-z)组成
((c》=’0’ && c《=’9’) || (c》=’A’ && c《=’Z’) || (c》=’a’ && c《=’z’))这是最简单的写法了。
键盘监听,文本框之内 只允许输入1-99之间数字,怎么写呀
import java.awt.event.*;import java.awt.*;import javax.swing.*;import javax.swing.text.*;class CustomedDocument extends PlainDocument { private int limit; public CustomedDocument (int limit) { super(); this.limit = limit; } public void insertString (int offset, String str, AttributeSet attr) throws BadLocationException { if (this.getLength() + str.length() 》 limit) return; char charArray = str.toCharArray(); for (char c : charArray) if ((this.getLength() == 0 && c 《 ’1’) || (this.getLength() != 0 && c 《 ’0’) || c 》 ’9’) return; super.insertString(offset, str, attr); }}public class Demo { private JFrame mainFrame; private JTextField txtInput; public Demo () { Toolkit tk = Toolkit.getDefaultToolkit(); Dimension dms = tk.getScreenSize(); Font f = new Font("Arial", Font.PLAIN, 13); mainFrame = new JFrame("Demo"); mainFrame.setLayout(null); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setBounds((int)dms.getWidth()/2-95, (int)dms.getHeight()/2-50, 190, 73); txtInput = new JTextField(); txtInput.setBounds(5, 5, 165, 25); txtInput.setFont(f); txtInput.setDocument(new CustomedDocument(2)); mainFrame.add(txtInput); mainFrame.setVisible(true); } public static void main (String args) throws InstantiationException, ClassNotFoundException, IllegalAccessException, UnsupportedLookAndFeelException { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new Demo(); }}
Delphi调用onkeypress
sender就是edit本身,所以你写edit1就可以了第二个key 要传地址,所以不能用常量#13,你定义一个char类型c:=#13.然后txt1KeyPress(txt1,c);保你管用