📄 numberfield.java
字号:
/*
$Author: $
$Date: $
$Revision: $
$NoKeywords: $
*/
package jp.co.ntl.swing;
import java.awt.AWTEvent;
///import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
public class NumberField extends JTextField {
/**
*
*/
private static final long serialVersionUID = -651231065726750554L;
public static final int UINT = NumDocument.UINT;
public static final int UFLOAT = NumDocument.UFLOAT;
public static final int INT = NumDocument.INT;
public static final int FLOAT = NumDocument.FLOAT;
static final int VALID_NUMBER = 0;
static final int NO_NUMBER = 1;
static final int SMALL_NUMBER = 2;
static final int BIG_NUMBER = 3;
int figure; //寘悢
int type;
boolean validRange = false;
float minimum = 0;
float maximum = 0;
/// boolean validEffective = false;
/// int effective = 0; //桳岠悢帤
public NumberField(int figure, int type, float minimum, float maximum, boolean validRange) {
super(figure);
this.figure = figure;
this.type = type;
this.validRange = validRange;
this.minimum = minimum;
this.maximum = maximum;
Document doc = new NumDocument(figure, type);
setDocument(doc);
enableInputMethods(false);
enableEvents(AWTEvent.KEY_EVENT_MASK);
}
public NumberField(int figure, int type, float minimum, float maximum) {
this(figure, type, minimum, maximum, true);
}
public NumberField(int figure, int type) {
this(figure, type, 0, 0, false);
}
public NumberField(int figure) {
this(figure, UINT, 0, 0, false);
}
/* public void setEffective(int effective) {
validEffective = true;
this.effective = effective;
}*/
public float getMinimum() {
return minimum;
}
public float getMaximum() {
return maximum;
}
public void setValue(float value) {
switch (type) {
case UINT:
case INT:
int i = (int)value;
setText("" + i);
break;
case UFLOAT:
case FLOAT:
setText("" + value);
break;
}
}
public void setValue(int value) {
setText("" + value);
}
public float getFloatValue() throws NumberFormatException {
return Float.parseFloat(getText());
}
public int getIntValue() throws NumberFormatException {
return Integer.parseInt(getText());
}
public void clear() {
setText("");
}
private static class NumDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int UINT = 0;
public static final int UFLOAT = 1;
public static final int INT = 2;
public static final int FLOAT = 3;
protected int figure;
protected int type;
public NumDocument(int figure, int type) {
this.figure = figure;
this.type = type;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if (str == null) {
return;
}
// 嵟戝寘悢偑擖椡偝傟偰偄傞偲偒
if (figure < getLength()) {
return;
}
if (figure < getLength() + str.length()) {
return;
}
String text = new String("");
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
text += c;
}
if (type == INT || type == FLOAT) {
if (c == '-') {
text += c;
}
}
if (type == FLOAT || type == UFLOAT) {
if (c == '.') {
text += c;
}
}
}
super.insertString(offs, text, a);
}
}
/*
protected void processKeyEvent(KeyEvent ke) {
if (ke.getID() == KeyEvent.KEY_PRESSED) {
System.out.println("Key pressed");
System.out.println("Key char = " + ke.getKeyChar());
System.out.println("Key code = " + ke.getKeyCode());
String text = getText();
String selectedText = getSelectedText();
switch (ke.getKeyChar()) {
case '.':
if (type == INT || type == UINT) {
ke.consume();
}
break;
case '-':
if (type == INT || type == FLOAT) {
if ((selectedText != null && selectedText.length() != getText().length()) && (getCaretPosition() != 0)) {
ke.consume();
}
} else {
ke.consume();
}
break;
case '0':
if (text.length() != 0 && selectedText != null && selectedText.length() == 0) {
if ((getCaretPosition() == 0 && text.indexOf('.') != 0)
|| (getCaretPosition() == 1 && text.charAt(0) == '0')) {
ke.consume();
break;
}
}
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (selectedText != null && selectedText.length() == 0) {
if (text.length() >= figure) {
ke.consume();
} else if (validEffective) {
int pos = text.indexOf('.');
if (pos != -1) {
if (getCaretPosition() > pos && text.length() - pos - 1 >= effective) {
ke.consume();
}
}
}
}
break;
default:
switch (ke.getKeyCode()) {
case KeyEvent.VK_BACK_SPACE:
case KeyEvent.VK_DELETE:
case KeyEvent.VK_TAB:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_SHIFT:
case KeyEvent.VK_END:
case KeyEvent.VK_HOME:
case KeyEvent.VK_ENTER:
break;
default:
ke.consume();
}
}
}
super.processKeyEvent(ke);
}*/
/* private int validNumber(String text) {
if (text.length() == 0) {
return VALID_NUMBER;
}
try {
float f = Float.valueOf(text).floatValue();
/*
if (validRange) {
if (f < minimum) {
return SMALL_NUMBER;
} else if (f > maximum) {
return BIG_NUMBER;
}
}
**
return VALID_NUMBER;
} catch (Exception e) {
//惓偟偄悢抣偱偼側偄
return NO_NUMBER;
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -