2014-01-21 83 views
0

我做了一個程序,它有一個包含JTextField,一個按鈕和兩個JLabel的JFrame。在JTextField中鍵入數字時,按下Enter鍵或單擊JButton應在第二個JLabel上以科學記數法顯示數字。當我按下回車鍵時,它可以工作,但是,當我點擊JButton時,它不會。它給了我一個NumberFormatException:空字符串。NumberFormatException:單擊JButton時爲空字符串?

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

這裏是SciNotation.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; 

} 
} 
+0

請張貼堆棧跟蹤你的異常。在黑暗中拍攝,檢查Double.parseNumber。它可能試圖解析無法解析爲雙精度的字符串。 –

+0

而不是'JTextField',使用'JSpinner'如[這裏](http://stackoverflow.com/a/10021773/418556)所示。爲了更快提供更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(如鏈接的答案中所示)。它不需要那麼簡短,但它應該是一個源文件,並且只包含文本字段或微調器,按鈕以及可能的標籤以顯示結果。 –

回答

5

此錯誤是相當棘手。您構建了三個MyMath實例。所以你的代碼對你的JTextFiled實例持有一個錯誤的引用。這就是爲什麼你不能得到正確的輸入。

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

:)

+0

感謝作品按鈕現在完美! – user3217494

+0

@ user3217494:不客氣。不要忘記接受我的答案。 :P –

相關問題