📄 textfieldfloat.java
字号:
/********************************************************************** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Library General Public* License as published by the Free Software Foundation; either* version 2 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Library General Public License for more details.** You should have received a copy of the GNU Library General Public* License along with this library; if not, write to the* Free Software Foundation, Inc., 59 Temple Place - Suite 330,* Boston, MA 02111-1307, USA.** @author: Copyright (C) Tim Carver*********************************************************************/package org.emboss.jemboss.gui.form;import javax.swing.*; import javax.swing.text.*; import java.awt.Toolkit;import java.text.NumberFormat;import java.text.ParseException;import java.util.Locale;/**** JTextfield for float fields in the EMBOSS form**/public class TextFieldFloat extends TextFieldSink { private Toolkit toolkit; private NumberFormat decimalFormatter; public TextFieldFloat() { super(); toolkit = Toolkit.getDefaultToolkit(); decimalFormatter = NumberFormat.getNumberInstance(Locale.UK); } public double getValue() { double retVal = 0; try { retVal = decimalFormatter.parse(getText()).doubleValue(); } catch (ParseException e) { // This should never happen because insertString allows // only properly formatted data to get in the field. toolkit.beep();// System.err.println("TextFieldFloat getValue: " + retVal); } return retVal; } public void setValue(double value) { setText(decimalFormatter.format(value)); } protected Document createDefaultModel() { return new DecimalNumberDocument(); } protected class DecimalNumberDocument 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 < result.length; i++) { if (Character.isDigit(source[i]) || source[i] == '.' || source[i] == '-') result[j++] = source[i]; else { if(source[i] != ',') toolkit.beep();// System.err.println("insertString: " + source[i]); } } super.insertString(offs, new String(result, 0, j), a); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -