如何制作一个有选择的gettext()

问题描述 投票:0回答:2

我正在尝试进行货币兑换计划,但如果第二个输入(此处为s1)为空,则程序会给出NumberFormatException:Empty String error。当第一个(此处)为空时它起作用。所以我想知道是否有另一种方法可以通过一个按钮选择它。当第二个字段为空时,为什么它不工作?

public class kanvas1 implements ActionListener,WindowListener{


    private JTextField tf;
    private JTextField tf1;
    private JTextField tf2;
    private JTextField tf3;
    private JLabel lb;
    private JLabel lb1;

    private JButton bt;
    private JButton bt1;

    public kanvas1()
    {

        tf=new JTextField();
        tf1=new JTextField();
        tf2=new JTextField();
        tf3=new JTextField();
        lb=new JLabel("$");
        lb1=new JLabel("TL");
        bt=new JButton("Cevir");
        bt1=new JButton("Sıfırla");


        pencere();


    }

    public void pencere() {
        tf.setBounds(50,20,150,50);
        tf1.setBounds(50,80, 150, 50);
        tf2.setBounds(220,20,150,50);
        tf3.setBounds(220,80,150,50);
        lb.setBounds(30,20,20, 50);
        lb1.setBounds(30,80,20,50);
        bt.setBounds(400,20,100, 50);
        bt1.setBounds(400,80,100,50);
        bt.addActionListener(this);
        bt1.addActionListener(this);
        JFrame ab=new JFrame();
        ab.setVisible(true);
        ab.setSize(600,200);
        ab.setLayout(null);
        ab.add(tf);ab.add(tf1);ab.add(tf2);ab.add(tf3);ab.add(bt);ab.add(bt1);ab.add(lb);ab.add(lb1);
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {


                String s=tf.getText(); //problem is here


                double a=Double.parseDouble(s);

                double c=a*5.44;

                String result=String.valueOf(c);

                tf2.setText(result);




            }

        });
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {



                String s1=tf1.getText();
                                          //and here

                double b=Double.parseDouble(s1);

                double d=b*0.18;

                String result1=String.valueOf(d);

                tf3.setText(result1);


            }

        });
        bt1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                tf.setText("");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");

            }
        });


    }


    @Override
    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosing(WindowEvent arg0) {
        System.exit(0);

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public static void main(String args[]) {

        new kanvas1();



    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }   
}
java swing select gettext
2个回答
0
投票

您可以在解析之前添加一个检查,如下所示:

if (s1.isEmpty())
     return;

它失败了因为Double.parseDouble(String arg)尝试将作为参数传递的字符串转换为字符串表示的Double。这意味着像“12”,“12.0”等字符串是有效输入,而“12a”,“ac”,“”等字符串必然会失败;因为,毕竟,12a不是双倍(它甚至不是一个数字!)。

如果文本字段为空并且您执行getText(),您将检索的是""(空字符串)而不是null

所以,如果你想做出任何有条件的决定,你的条件基于empty i.e. ""而不是null检索到的文本


0
投票

而不是像同一个按钮两次写addActionListener,你可以在单个addActionListener中写它。此外,避免保持任何字段为空,你得到NumberFormatException,因为你的一个JTextField给出空白值或任何其他不是双倍的值。使用Double.parseDouble(“12.34”)进行解析时,应始终包含字符串格式的double值。解析休息其他值抛出NumberFormatException

bt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {


        String s=tf.getText(); //problem is here


        double a=Double.parseDouble(s);

        double c=a*5.44;

        String result=String.valueOf(c);

        tf2.setText(result);




    }

});
bt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {



        String s1=tf1.getText();
                                  //and here

        double b=Double.parseDouble(s1);

        double d=b*0.18;

        String result1=String.valueOf(d);

        tf3.setText(result1);


    }

});

将以上代码替换为:

bt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {


        String s=tf.getText();


        double a=Double.parseDouble(s);

        double c=a*5.44;

        String result=String.valueOf(c);

        tf2.setText(result);



         String s1=tf1.getText();
         double b=Double.parseDouble(s1);

        double d=b*0.18;

        String result1=String.valueOf(d);

        tf3.setText(result1);



    }

});
© www.soinside.com 2019 - 2024. All rights reserved.