当前位置: 技术问答>java相关
在JTextField 中控制输入数字?
来源: 互联网 发布时间:2015-05-04
本文导语: 如何在JTextField 中控制输入数字,只能输入数字! void jTextField6_keyPressed(KeyEvent e) { if (( e.getKeyChar () e.VK_9 )) { 这儿应改如何处理?谢谢! } | 这个已经...
如何在JTextField 中控制输入数字,只能输入数字!
void jTextField6_keyPressed(KeyEvent e) {
if (( e.getKeyChar () e.VK_9 ))
{
这儿应改如何处理?谢谢!
}
void jTextField6_keyPressed(KeyEvent e) {
if (( e.getKeyChar () e.VK_9 ))
{
这儿应改如何处理?谢谢!
}
|
这个已经测试过了。
import java.awt.*;
import javax.swing.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class Frame2 extends JFrame
{
BorderLayout borderLayout1 = new BorderLayout();
NumberTextField jTextField1 = new NumberTextField(10,10);;
public Frame2()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame2 frame2 = new Frame2();
}
private void jbInit() throws Exception
{
jTextField1.setText("jTextField1");
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTextField1, BorderLayout.NORTH);
this.setSize(new Dimension(400, 300));
this.setEnabled(true);
this.show();
this.validate();
}
}
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class NumberTextField extends JTextField
{
private Toolkit toolkit;
private NumberFormat integerFormatter;
public NumberTextField(int value, int columns)
{
super(columns);
toolkit = Toolkit.getDefaultToolkit();
integerFormatter = NumberFormat.getNumberInstance(Locale.getDefault());
integerFormatter.setParseIntegerOnly(true);
setValue(value);
}
public int getValue()
{
int retVal = 0;
try
{
retVal = integerFormatter.parse(getText()).intValue();
}
catch (ParseException e)
{
// This should never happen because insertString allows
// only properly formatted data to get in the field.
toolkit.beep();
}
return retVal;
}
public void setValue(int value)
{
setText(integerFormatter.format(value));
}
protected Document createDefaultModel()
{
return new WholeNumberDocument();
}
protected class WholeNumberDocument extends PlainDocument
{
public void insertString(int offs , String str , AttributeSet a)
throws BadLocationException
{
char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i
import java.awt.*;
import javax.swing.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class Frame2 extends JFrame
{
BorderLayout borderLayout1 = new BorderLayout();
NumberTextField jTextField1 = new NumberTextField(10,10);;
public Frame2()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame2 frame2 = new Frame2();
}
private void jbInit() throws Exception
{
jTextField1.setText("jTextField1");
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTextField1, BorderLayout.NORTH);
this.setSize(new Dimension(400, 300));
this.setEnabled(true);
this.show();
this.validate();
}
}
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class NumberTextField extends JTextField
{
private Toolkit toolkit;
private NumberFormat integerFormatter;
public NumberTextField(int value, int columns)
{
super(columns);
toolkit = Toolkit.getDefaultToolkit();
integerFormatter = NumberFormat.getNumberInstance(Locale.getDefault());
integerFormatter.setParseIntegerOnly(true);
setValue(value);
}
public int getValue()
{
int retVal = 0;
try
{
retVal = integerFormatter.parse(getText()).intValue();
}
catch (ParseException e)
{
// This should never happen because insertString allows
// only properly formatted data to get in the field.
toolkit.beep();
}
return retVal;
}
public void setValue(int value)
{
setText(integerFormatter.format(value));
}
protected Document createDefaultModel()
{
return new WholeNumberDocument();
}
protected class WholeNumberDocument extends PlainDocument
{
public void insertString(int offs , String str , AttributeSet a)
throws BadLocationException
{
char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i