Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 NumberFormatException:单击JButton时是否为空字符串?_Java_Swing_Double_Jtextfield - Fatal编程技术网

Java NumberFormatException:单击JButton时是否为空字符串?

Java NumberFormatException:单击JButton时是否为空字符串?,java,swing,double,jtextfield,Java,Swing,Double,Jtextfield,我制作了一个程序,它有一个JFrame,其中包含一个JTextField、一个按钮和两个jlabel。当在JTextField中键入一个数字时,按enter键或单击JButton都会在第二个JLabel上以科学符号显示该数字。当我按enter键时,它工作,但是当我单击JButton时,它不工作。它给了我一个NumberFormatException:空字符串 import java.awt.*; import java.awt.event.*; import javax.swing.*; pu

我制作了一个程序,它有一个JFrame,其中包含一个JTextField、一个按钮和两个jlabel。当在JTextField中键入一个数字时,按enter键或单击JButton都会在第二个JLabel上以科学符号显示该数字。当我按enter键时,它工作,但是当我单击JButton时,它不工作。它给了我一个NumberFormatException:空字符串

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

public class MyMath extends JPanel implements ActionListener
{
   private JTextField textField;
   private static JLabel textArea;
   private static JLabel comment;
   private JButton button;
   private static JFrame frame;

public MyMath()
{
    comment = new JLabel("Enter a number");
    textField = new JTextField();
    textField.setSize(new Dimension(10 , 10));
    textField.addActionListener(this);
    button = new JButton("Go");
    button.addActionListener(this);
}

    public static void addComponentsToPane(Container pane) 
    {

    textArea = new JLabel(" ");
    pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
    pane.add(new MyMath().textField);
    pane.add(new MyMath().button);
    pane.add(new MyMath().comment);
    pane.add(textArea);
}

public void actionPerformed(ActionEvent evt) 
{
    String text = textField.getText();
    textArea.setText(SciNotation.convertToSciNotation(text));
    textArea.setHorizontalAlignment(SwingConstants.CENTER);
    comment.setText(text + " in Scientific Notation:");
    textField.selectAll();
}

private static void createAndShowGUI() 
{  
    frame = new JFrame("Scientific Notation");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addComponentsToPane(frame.getContentPane());
    Container bg = frame.getContentPane();
    Dimension d = new Dimension(300, 150);   
    bg.setPreferredSize(d);
    frame.setResizable(false);
    frame.getContentPane().setBackground(Color.GREEN);
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Point screenCenter = new Point (screen.width/2 , screen.height/2);
    Point center = new Point(screenCenter.x - (150), screenCenter.y - (75));
    frame.setLocation(center);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() 
        {
            createAndShowGUI();
        }
    });
  }
}
这里是scinonotation.java

    import java.awt.Component;
    import java.awt.font.TextAttribute;
    import java.text.AttributedString;

public class SciNotation
{
  public static String convertToSciNotation(String num)
  {
    num = num.replaceAll("," , "");
    if (num.contains("E"))                    //not working
    {
        String[] partsE = num.split("E"); 
        String beforeE = partsE[0];
        String afterE = partsE[1];
        char first = num.charAt(0);
        num = first + beforeE;
    }

    double number = Double.parseDouble(num);
    double resultNumber = 0;
    int power = 0;
    String numString = Double.toString(number); 
    String[] parts = numString.split("\\.");
    String decimals = parts[1];
    int decimalInt = Integer.parseInt(decimals);
    int numberInt = (int) number;

    if(number > -1 && number < 1)
    {
        String[] low = numString.split("\\.");
        String afterLow = low[1];
        for(int i = 1; i < 10; i++)
        {
            String decNums = Integer.toString(i);
            afterLow = afterLow.replaceAll(decNums, "");
            int zeros  = afterLow.length();
            power = -1 * (zeros + 1);
            resultNumber = number * Math.pow(10, zeros + 1);  
            decimals = "";
        }
    }

    if(decimalInt == 0)
    {
        decimals = "";
    }

    if( number >= 10)
    {
        power = Integer.toString(numberInt).length() - 1;   
        resultNumber =  numberInt/(Math.pow(10,(power)));
    }

    if((number >= 1 && number < 10) || (number <= -1 && number > -10))
    {
        resultNumber = number;
        decimals = "";
    }

    if(number <= -10)
    {
        power = Integer.toString(numberInt).length() - 2;
        resultNumber =  numberInt/(Math.pow(10,(power)));
    }

    return resultNumber + decimals + " x 10^" + power;

}
 }
导入java.awt.Component;
导入java.awt.font.texttribute;
导入java.text.AttributeString;
公共类符号
{
公共静态字符串convertToSciNotation(字符串编号)
{
num=num.replaceAll(“,”和“”);
if(num.contains(“E”)//不工作
{
字符串[]partsE=num.split(“E”);
字符串beforeE=partsE[0];
字符串afterE=partsE[1];
char first=num.charAt(0);
num=第一个+之前;
}
double number=double.parseDouble(num);
双结果编号=0;
整数幂=0;
字符串numString=Double.toString(数字);
String[]parts=numString.split(“\\”);
字符串小数=部分[1];
整数小数=整数.parseInt(小数);
int numberprint=(int)数字;
如果(编号>-1&&number<1)
{
String[]low=numString.split(“\\”);
字符串afterLow=低[1];
对于(int i=1;i<10;i++)
{
字符串decNums=Integer.toString(i);
afterLow=afterLow.replaceAll(十进制数,“”);
int zeros=afterLow.length();
功率=-1*(零+1);
resultNumber=number*Math.pow(10,零+1);
小数=”;
}
}
如果(小数==0)
{
小数=”;
}
如果(数字>=10)
{
幂=整数.toString(numberrint).length()-1;
结果编号=编号/(数学功率(10,(幂));
}
如果((数字>=1&&number<10)| |(数字-10))
{
结果编号=编号;
小数=”;
}

if(number此错误非常棘手。您构建了三个MyMath实例。因此,您的代码对JTextField实例的引用错误。这就是您无法获得正确输入的原因

public static void addComponentsToPane(Container pane) {

    textArea = new JLabel(" ");
    pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
    pane.add(new MyMath().textField);  //MyMath instance 1
    pane.add(new MyMath().button);  //MyMath instance 2
    pane.add(new MyMath().comment);  //MyMath instance 3
    pane.add(textArea);
}
以下是正确的代码:

public static void addComponentsToPane(Container pane) {

        MyMath myMath = new MyMath();

        textArea = new JLabel(" ");
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        pane.add(myMath.textField);
        pane.add(myMath.button);
        pane.add(myMath.comment);
        pane.add(textArea);
    }

:)

此错误非常棘手。您构建了三个MyMath实例。因此,您的代码对JTextField实例的引用错误。这就是您无法获得正确输入的原因

public static void addComponentsToPane(Container pane) {

    textArea = new JLabel(" ");
    pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
    pane.add(new MyMath().textField);  //MyMath instance 1
    pane.add(new MyMath().button);  //MyMath instance 2
    pane.add(new MyMath().comment);  //MyMath instance 3
    pane.add(textArea);
}
以下是正确的代码:

public static void addComponentsToPane(Container pane) {

        MyMath myMath = new MyMath();

        textArea = new JLabel(" ");
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        pane.add(myMath.textField);
        pane.add(myMath.button);
        pane.add(myMath.comment);
        pane.add(textArea);
    }

:)

请发布异常的堆栈跟踪。在黑暗中拍摄,检查Double.parseNumber。它可能试图解析一个无法解析为Double的字符串。使用
JTextField
,而不是
JSpinner
,如图所示。要更快获得更好的帮助,请发布一个(如链接答案中所示)。它不需要那么短,但应该是一个源文件,并且只包含一个文本字段或微调器、一个按钮,可能还包含一个显示结果的标签。请发布异常的堆栈跟踪。在黑暗中拍摄,请选中Double.parseNumber。它可能试图解析无法解析为Double的字符串。而不是
JText字段
,如图所示使用一个
JSpinner
。为了更快获得更好的帮助,请发布一个(如链接答案中所示)。它不需要那么短,但应该是一个源文件,并且只包含一个文本字段或微调器、一个按钮,可能还有一个标签来显示结果。@user3217494:不客气。别忘了接受我的回答。:P@user3217494:不客气。别忘了接受我的回答