Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing JTextField-仅限数字_Java_Swing_Validation_Jtextfield_Temperature - Fatal编程技术网

Java Swing JTextField-仅限数字

Java Swing JTextField-仅限数字,java,swing,validation,jtextfield,temperature,Java,Swing,Validation,Jtextfield,Temperature,我正在制作一个转换温度的java Swing小程序:TempConvert.java 这是我的密码: package swing; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; /** Celcius to Fahrenheit Converter * @version 1.0 * @author Oliver Ni */ public cla

我正在制作一个转换温度的java Swing小程序:TempConvert.java

这是我的密码:

package swing;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/** Celcius to Fahrenheit Converter
 * @version 1.0
 * @author Oliver Ni
 */

public class TempConvert extends JApplet{
    JLabel result;
    JRadioButton ctof;
    JRadioButton ftoc;
    JTextField deg;
    JLabel degLab;
    JButton convert;

    public void convert() {
        if (ctof.isSelected() == true) {
            result.setText("<html><br>" + Integer.toString(Integer.parseInt(deg.getText()) * 9 / 5 + 32) + "<sup>o</sup> F</html>");
        } else if (ftoc.isSelected() == true) {
            result.setText("<html><br>" + Integer.toString((Integer.parseInt(deg.getText()) - 32) * 5 / 9) + "<sup>o</sup> C</html>");
        } else {
            result.setText("<html><br>Error.</html>");
        }
    }

    public void makeApplet() {
        setLayout(new FlowLayout());
        ctof = new JRadioButton("Celcius to Fahrenheit");
        ftoc = new JRadioButton("Fahrenheit to Celcius");
        convert = new JButton("Convert");
        result = new JLabel("");
        ButtonGroup group = new ButtonGroup();
        group.add(ctof);
        group.add(ftoc);

        deg = new JTextField(10);
        degLab = new JLabel("<html><sup>o</sup></html>");
        convert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                convert();
            }
        });
        add(ctof);
        add(ftoc);
        add(deg);
        add(degLab);
        add(convert);
        add(result);
    }

    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    makeApplet();
                }
            });
        } catch(Exception e) {
            System.out.println("Error loading because " + e);
        }
    }
}
package-swing;
导入javax.swing.*;
导入javax.swing.event.*;
导入java.awt.*;
导入java.awt.event.*;
/**Celcius至华氏转换器
*@version 1.0
*@作者奥利弗·尼
*/
公共类tempplet{
JLabel结果;
JRadioButton-ctof;
JRadioButton ftoc;
JTextField度;
JLabel-degLab;
按钮转换;
公共void convert(){
if(ctof.isSelected()==true){
result.setText(“
”+Integer.toString(Integer.parseInt(deg.getText())*9/5+32)+“of”); }else if(ftoc.isSelected()==true){ result.setText(“
”+Integer.toString((Integer.parseInt(deg.getText())-32)*5/9)+“oc”); }否则{ result.setText(“
错误”); } } 公共void makeApplet(){ setLayout(新的FlowLayout()); ctof=新JRadioButton(“Celcius到华氏度”); ftoc=新的JRadioButton(“华氏度到摄氏度”); convert=新的JButton(“convert”); 结果=新的JLabel(“”); ButtonGroup=新建ButtonGroup(); 添加组(ctof); 添加组(ftoc); deg=新的JTextField(10); degLab=新的JLabel(“o”); convert.addActionListener(新ActionListener(){ 已执行的公共无效行动(行动事件ae){ convert(); } }); 添加(ctof); 添加(ftoc); 添加(度); 添加(degLab); 添加(转换); 添加(结果); } 公共void init(){ 试一试{ SwingUtilities.invokeAndWait(新的Runnable(){ 公开募捐{ makeApplet(); } }); }捕获(例外e){ System.out.println(“加载错误,因为“+e”); } } }
我想限制
JTextField
deg
仅为整数。我有办法做到吗

任何帮助都将不胜感激

“我想限制JTextField deg仅为整数。”

试试这个。使用文档过滤器

((AbstractDocument) deg.getDocument()).setDocumentFilter(new DocumentFilter() {
            Pattern regEx = Pattern.compile("\\d+");

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                Matcher matcher = regEx.matcher(text);
                if (!matcher.matches()) {
                    return;
                }
                super.replace(fb, offset, length, text, attrs);
            }
        });

对于这样一个简单的程序,您可以尝试使用格式化的文本字段:

您只需要转换-

JTextField deg;

这将返回当前默认语言环境的通用数字格式


谢谢

这有帮助吗?请告诉我。请改用
JSpinner
,如图所示。
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault());
DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
decimalFormat.setGroupingUsed(false);
deg = new JFormattedTextField(decimalFormat);
deg.setColumns(15); //whatever size you wish to set