java - 如何处理空文本字段的异常?

标签 java

我想计算对数值,但空文本字段出现错误。

//Libraries
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.awt.Font; //For Font And Size
import java.awt.Color; //For Color Change
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChildClass extends JFrame {

    private JTextField textfield1;
    private JTextField textfield3;
    private JTextField textfield4;
    private JButton button1;

    public ChildClass() {
        super("Logrithm");
        getContentPane().setBackground(new Color(153, 204, 153));

        getContentPane().setLayout(null);

        textfield1 = new JTextField();
        textfield1.setBounds(62, 77, 62, 26);
        getContentPane().add(textfield1);

        textfield3 = new JTextField();
        textfield3.setBounds(127, 224, 153, 26);
        getContentPane().add(textfield3);

        textfield4 = new JTextField();
        textfield4.setBounds(175, 124, 62, 26);
        getContentPane().add(textfield4);

        button1 = new JButton();
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double number1,baseE,result;
                String text1 = textfield1.getText();
                String text3 = textfield4.getText();

                if (text1.isEmpty() && text3.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE);
                }

                if (text3.isEmpty()) {
                    number1 = Double.parseDouble(text1);
                    result = Math.log(number1);
                    textfield3.setText("" + result);
                }
                else if (!text3.isEmpty()) {
                    number1 = Double.parseDouble(text1);
                    baseE = Double.parseDouble(text3);
                    result = Math.log(number1) / Math.log(baseE);
                    textfield3.setText("" + result);
                }
            }
        });
        button1.setFont(new Font("Bullet", Font.PLAIN, 16));
        button1.setText("Click It");
        button1.setBounds(161, 172, 87, 26);
        getContentPane().add(button1);
    }
}

这是主要方法:

public class MainMethod {

    public static void main(String[] args) {

        ChildClass obj=new ChildClass();

        obj.setSize(450, 400);
        obj.setResizable(false);

        obj.setDefaultCloseOperation(obj.EXIT_ON_CLOSE);
        obj.setLocationRelativeTo(null);
        obj.setVisible(true);
    }
}

当我单击带有空文本字段的 jbutton 时,我遇到问题。单击“确定”后会出现弹出消息对话框,然后:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

如何处理这个错误?

最佳答案

problem i facing when click the jbutton with empty textfields. popup MessageDialog Come after i click ok and then Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String how to handle this error

如果 JTextArea 中的值为空(或无效 - 您可能希望检查有效数字,或使用 JFormattedTextField)

  1. 警告用户
  2. 不要尝试处理该值(从方法返回,或者将剩余代码放在 else 子句中)

例如:

//note the 'or' rather than 'and' below, presuming both must be valid
if( text1.isEmpty() || text3.isEmpty()){
    JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE);
    return;//return from the method to allow the user to edit the JTextField
}

关于java - 如何处理空文本字段的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292375/

相关文章:

java - Twitter 快速回复 - Twitter4J (Java) 示例

java - 无法从其他项目找到字符串资源

java - 如何在 JEditorPane 中插入 iframe(YouTube 视频)?

java - 如何读取 Tomcat JDBC 数据源资源工厂属性?

java - 我怎样才能使 SmartGWT 核心更小?

java - Spring 安全: How to allow access to only login page using OAuth 2. 0?

java - 解决 Activity 和 Fragment 之间循环依赖的最佳方法

java - 如何让 Android 应用程序与 MySQL 在线数据库一起工作?

java - 每个应用程序具有不同值的 Spring 公共(public)属性引用

java - 一个程序(eclipse Mars)需要java 8,一个程序(控制台应用程序)需要java 7 怎么办?