JAVA文本框监听
怎么让文本框只能输入数字输入其他字符就在旁边报错帮忙修改,谢谢importjava.awt.*;importjavax.swing.*;classDome{JFramef...
怎么让文本框只能输入数字 输入其他字符就在旁边报错 帮忙修改,谢谢
import java.awt.*;
import javax.swing.*;
class Dome{
JFrame frame;
JTextField text;
public Dome(){
frame = new JFrame("输入框");
text = new JTextField(10);
frame.add(text);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class input_box {
public static void main(String args[]){
Dome d = new Dome();
}
} 展开
import java.awt.*;
import javax.swing.*;
class Dome{
JFrame frame;
JTextField text;
public Dome(){
frame = new JFrame("输入框");
text = new JTextField(10);
frame.add(text);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class input_box {
public static void main(String args[]){
Dome d = new Dome();
}
} 展开
展开全部
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.*;
final class TextValidateDemo extends JFrame {
final static private Dimension SIZE = new Dimension(800, 600);
public String getTitle() {return "TextValidateDemo";}
public Dimension getPreferredSize() {return SIZE;}
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getSize() {return SIZE;}
private JTextField textField;
private final static Pattern pattern = Pattern.compile("([0-9])*");
private static final Observable validateObservable = new Observable(){
public void notifyObservers() {
setChanged();
super.notifyObservers();
}
public void notifyObservers(Object arg) {
setChanged();
super.notifyObservers(arg);
}
};
TextValidateDemo() throws HeadlessException {
init();
attachListeners();
doLay();
setVisible(true);
}
private void init() {
textField=new JTextField(50);
validateObservable.addObserver( new TextValidateObserer(textField) );
}
private void attachListeners() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
textField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
validateObservable.notifyObservers(null);
}
public void removeUpdate(DocumentEvent e) {
validateObservable.notifyObservers(null);
}
public void changedUpdate(DocumentEvent e) {
validateObservable.notifyObservers(null);
}
});
}
private void doLay() {
Container container = getContentPane();
container.add(textField, BorderLayout.NORTH);
pack();
}
private static boolean validateInput(String s){
return pattern.matcher(s).matches();
}
private class TextValidateObserer implements Observer{
private JTextComponent t;
public TextValidateObserer(JTextComponent t) {
this.t=t;
}
public void update(Observable o, Object arg) {
if (t!=null && arg == null) {
t.setForeground(validateInput(t.getText()) ?
Color.BLACK : Color.RED
);
}
}
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(TextValidateDemo::new);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询