문제

I am making a small java Swing Applet that converts temperatures: TempConvert.java

Here is my code:

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);
        }
    }
}

I want to restrict the JTextField deg to only integers. Is there any way I can do that?

Any help would be appreciated!

도움이 되었습니까?

해결책

You just need to convert your -

JTextField deg;

to

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

This will return a general-purpose number format for the current default locale.

Thanks

다른 팁

"I want to restrict the JTextField deg to only integers."

Try this. Use a Document Filter

((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);
            }
        });

For a simple program like this, you can try to use Formatted Text Fields: http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top