📄 jtexttools.java
字号:
/* JTextTools.java (P)2002 Laurentiu Cristofor Modified by Dana Cristofor on August 2002 to receive a JTextArea object where error messages are displayed error messages*//*GAClust - Clustering categorical databases using genetic algorithmsCopyright (C) 2002 Dana CristoforThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307USAGAClust was written by Dana Cristofor (dana@cs.umb.edu).*/import javax.swing.*;/** * A number of useful methods for dealing with JTextFields. * * @version 1.0 * @author Laurentiu Cristofor; modified by Dana Cristofor */public class JTextTools{ /** * Method for dealing with an invalid value in a JTextField. The * method clears the text field and sets the focus on it, after * which it throws an IllegalArgumentException. * * @param jTxt the JTextField * @exception IllegalArgumentException always thrown */ public static void dealWithInvalidValue(JTextField jTxt) { jTxt.setText(""); jTxt.requestFocus(); throw new IllegalArgumentException(); } /** * Method for getting a double value from a JTextField. If the value * is not valid an error message is appended to the text area and * then dealWithInvalidValue is called resulting in an * IllegalArgumentException being thrown. Otherwise the value read * from the text field is returned. * * @param jTxt the JTextField * @param jTxtArea the JTextArea * @param errMessage the error message to be appended to the text area * @exception IllegalArgumentException if text field contains an * invalid value */ public static double getDouble(JTextField jTxt, JTextArea jTxtArea, String errMessage) { String s = jTxt.getText(); double value = 0.0; try { value = Double.parseDouble(s); } catch (NumberFormatException e) { jTxtArea.append(errMessage + "\n"); dealWithInvalidValue(jTxt); } return value; } /** * Method for getting an int value from a JTextField. If the value * is not valid then dealWithInvalidValue is called resulting in an * IllegalArgumentException being thrown. Otherwise the value read * from the text field is returned. * * @param jTxt the JTextField * @param jTxtArea the JTextArea * @param errMessage the error message to be appended to the text area * @exception IllegalArgumentException if text field contains an * invalid value */ public static int getInt(JTextField jTxt, JTextArea jTxtArea, String errMessage) { String s = jTxt.getText(); int value = 0; try { value = Integer.parseInt(s); } catch (NumberFormatException e) { jTxtArea.append(errMessage + "\n"); dealWithInvalidValue(jTxt); } return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -