62,621
社区成员
发帖
与我相关
我的任务
分享public class JTextFieldtest5 extends JFrame {
public JTextFieldtest5() {
final JTextField field = new JTextField(10);
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
String str = "0123456789";// 在这里不对小数点进行屏蔽,在点击提交按钮时,若不能转化成数字则给提示
char ch =e.getKeyChar();
if (str.indexOf(ch) == -1)
{
e.consume();
JOptionPane.showMessageDialog(JTextFieldtest5.this, "数据不合法");
}
}
});
this.add(field);
this.add(new JLabel("用鼠标拖拽选择"));
this.setLayout(new FlowLayout());
this.setBounds(100, 200, 300, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JTextFieldtest5();
}
}
