如何使JTextField或JFormattedTextField接受带小数位的数字?

时间:2018-06-05 15:25:34

标签: java eclipse

我正在研究计算定积分的程序。我们的想法是将您想要的系数放入JTextField或JFormattedTextField,程序会计算您的答案。 我偶然发现的问题是我可以输入的唯一数字是整数。我正在寻找一种投入双打的方法。 我现在这样做的方式是使用 NumberFormat NumberFormatter ,但我找不到让它接受十进制格式的方法。

这是我的框架代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.NumberFormatter;



public class IntegrationFrame extends JFrame {

private static final long serialVersionUID = 1L;

private JFormattedTextField txtLwrBnd;
private JFormattedTextField txtUprBnd;
private JFormattedTextField txtA;
private JFormattedTextField txtB;
private JFormattedTextField txtC;
private JLabel lblLwrBnd;
private JLabel lblUprBnd;
private JLabel lblA;
private JLabel lblB;
private JLabel lblC;
private JTextField txtAnswer;

double uprBnd = 1;
double lwrBnd = 0;
double a = 1;
double b = 1;
double c = 1;


private double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmpUpr, tmpLwr, result;
private String answer;


//Creating the frame
public IntegrationFrame(int enabled, int fNumber){ //enabled - which const. are on
    setResizable(false);                            /// fNumber - number of integral function
    setLocationRelativeTo(null);

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setSize(335,480);
    getContentPane().setLayout(null);

    NumberFormat format = NumberFormat.getInstance();
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setValueClass(Integer.class);
    formatter.setMinimum(0);
    formatter.setMaximum(Integer.MAX_VALUE);
    formatter.setAllowsInvalid(true);
    // If you want the value to be committed on each keystroke instead of focus lost
    formatter.setCommitsOnValidEdit(true);

    txtLwrBnd = new JFormattedTextField(formatter);
    txtLwrBnd.setText("0");
    txtLwrBnd.setBounds(46, 118, 56, 22);
    getContentPane().add(txtLwrBnd);
    txtLwrBnd.setColumns(10);

    JLabel lblTitle = new JLabel("Enter coefficients\n");
    lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
    lblTitle.setBounds(12, 34, 299, 16);
    getContentPane().add(lblTitle);

    txtUprBnd = new JFormattedTextField(formatter);
    txtUprBnd.setText("1");
    txtUprBnd.setBounds(227, 118, 56, 22);
    getContentPane().add(txtUprBnd);
    txtUprBnd.setColumns(10);



    txtA = new JFormattedTextField(formatter);
    txtA.setText("1");
    txtA.setBounds(46, 202, 56, 22);
    getContentPane().add(txtA);
    txtA.setColumns(10);

    txtB = new JFormattedTextField(formatter);
    txtB.setText("1");
    txtB.setBounds(143, 202, 56, 22);
    getContentPane().add(txtB);
    txtB.setColumns(10);

    txtC = new JFormattedTextField(formatter);
    txtC.setText("1");
    txtC.setBounds(242, 202, 56, 22);
    getContentPane().add(txtC);
    txtC.setColumns(10);

    lblLwrBnd = new JLabel("Lower Bound");
    lblLwrBnd.setHorizontalAlignment(SwingConstants.CENTER);
    lblLwrBnd.setBounds(12, 89, 116, 16);
    getContentPane().add(lblLwrBnd);

    lblUprBnd = new JLabel("Upper Bound");
    lblUprBnd.setHorizontalAlignment(SwingConstants.CENTER);
    lblUprBnd.setBounds(195, 89, 116, 16);
    getContentPane().add(lblUprBnd);

    lblA = new JLabel("a =");
    lblA.setBounds(12, 205, 30, 16);
    getContentPane().add(lblA);

    lblB = new JLabel("b =");
    lblB.setBounds(114, 205, 30, 16);
    getContentPane().add(lblB);

    lblC = new JLabel("c =");

    lblC.setBounds(213, 205, 30, 16);
    getContentPane().add(lblC);



    if (enabled == 1) {
        txtC.setEnabled(false);
        lblC.setEnabled(false);
    }
        else if (enabled == 2) {
            txtB.setEnabled(false);
            lblB.setEnabled(false);
            txtC.setEnabled(false);
            lblC.setEnabled(false);
        }
        else {
            txtB.setEnabled(true);
            lblB.setEnabled(true);
            txtC.setEnabled(true);
            lblC.setEnabled(true);
        }


    JButton btnCalculate = new JButton("Compute");
    btnCalculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            if (fNumber == 1) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                tmpUpr = uprBnd;
                tmpLwr = lwrBnd;
                result = a*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 2) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/(b+1);
                tmpUpr = Math.pow(uprBnd, b+1);
                tmpLwr = Math.pow(lwrBnd, b+1);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 3) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                tmp1 = a/2;
                tmpUpr = Math.pow(uprBnd, 2);
                tmpLwr = Math.pow(lwrBnd, 2);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 4) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                tmp1 = a;
                tmpUpr = Math.log(uprBnd);
                tmpLwr = Math.log(lwrBnd);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 5) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                c = Double.parseDouble(txtC.getText());
                tmp1 = a/(c*Math.log(a));
                tmpUpr = Math.pow(b, uprBnd*c);
                tmpLwr = Math.pow(b, lwrBnd*c);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 6) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmpUpr = Math.exp(uprBnd*b);
                tmpLwr = Math.exp(lwrBnd*b);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 7) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmpUpr = -Math.cos(uprBnd*b);
                tmpLwr = -Math.cos(lwrBnd*b);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 8) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmpUpr = Math.sin(uprBnd*b);
                tmpLwr = Math.sin(lwrBnd*b);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 9) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmp2 = Math.cos(uprBnd*b);
                tmp3 = Math.cos(lwrBnd*b);
                tmpUpr = -Math.log(tmp2);
                tmpLwr = -Math.log(tmp3);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 10) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmp2 = Math.sin(uprBnd*b);
                tmp3 = Math.sin(lwrBnd*b);
                tmpUpr = Math.log(tmp2);
                tmpLwr = Math.log(tmp3);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 11) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmpUpr = Math.tan(uprBnd*b);
                tmpLwr = Math.tan(lwrBnd*b);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 12) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                tmp1 = a/b;
                tmp2 = Math.tan(uprBnd*b);
                tmp3 = Math.tan(lwrBnd*b);
                tmpUpr = -1/tmp2;
                tmpLwr = -1/tmp3;
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 13) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                c = Double.parseDouble(txtC.getText());
                tmp2 = Math.sqrt(b);
                tmp1 = a/(c*tmp2);
                tmpUpr = Math.atan(uprBnd*tmp2/c);
                tmpLwr = Math.atan(lwrBnd*tmp2/c);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 14) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                c = Double.parseDouble(txtC.getText());
                tmp2 = Math.sqrt(b);
                tmp1 = a/(2*c*tmp2);
                tmp3 = uprBnd*tmp2;
                tmp4 = lwrBnd*tmp2;
                tmpUpr = Math.log((tmp3-c)/(tmp3+c));
                tmpLwr = Math.log((tmp4-c)/(tmp4+c));
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 15) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                c = Double.parseDouble(txtC.getText());
                tmp2 = Math.sqrt(c);
                tmp1 = a/tmp2;
                tmpUpr = Math.asin(uprBnd*tmp2/b);
                tmpLwr = Math.asin(lwrBnd*tmp2/b);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else if (fNumber == 16) {
                lwrBnd = Double.parseDouble(txtLwrBnd.getText());
                uprBnd = Double.parseDouble(txtUprBnd.getText());
                a = Double.parseDouble(txtA.getText());
                b = Double.parseDouble(txtB.getText());
                c = Double.parseDouble(txtC.getText());
                tmp2 = Math.sqrt(b);
                tmp1 = 1/tmp2;
                tmp5 = Math.pow(uprBnd, 2);
                tmp6 = Math.pow(lwrBnd, 2);
                tmp3 = Math.sqrt(b*tmp5 + c);
                tmp4 = Math.sqrt(b*tmp6 + c);
                tmpUpr = Math.log(uprBnd + tmp3);
                tmpLwr = Math.log(lwrBnd + tmp4);
                result = tmp1*(tmpUpr - tmpLwr);
                answer = String.format("%.4f", result);
                txtAnswer.setText(answer);
            }
            else {
                System.out.println(":C");
            }

        }
    });
    btnCalculate.setBounds(114, 293, 97, 25);
    getContentPane().add(btnCalculate);

    txtAnswer = new JTextField();
    txtAnswer.setEditable(false);
    txtAnswer.setBounds(107, 375, 116, 22);
    getContentPane().add(txtAnswer);
    txtAnswer.setColumns(10);

    JLabel lblAnswer = new JLabel("Answer\r\n");
    lblAnswer.setHorizontalAlignment(SwingConstants.CENTER);
    lblAnswer.setBounds(107, 345, 116, 16);
    getContentPane().add(lblAnswer);

}


public static void main(String[] args) {
    IntegrationFrame frame = new IntegrationFrame(2, 1); 
    frame.setVisible(true);

}
}

0 个答案:

没有答案