2010-10-09 78 views
0

一直試圖改變我的代碼來捕捉異常,但無法弄清楚如何用這個去解決它。我偶爾會遇到JOptionPane的雙重實例化,這取決於我放在哪裏。需要幫助捕獲這個程序的例外

import java.awt.Font; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

public class JCalculateWeight extends JFrame { 

    Font font = new Font("Arial", Font.BOLD, 14); 
    JFrame frame; 
    String name = JOptionPane.showInputDialog(frame, "What's your name?"); 
    String weight = JOptionPane.showInputDialog(frame, "What's your weight (lbs)"); 

    @Override 
    public void paint(Graphics brush) { 
     super.paint(brush); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     double lbs = Double.parseDouble(weight); 
     double oz = lbs * 16; 
     double kilo = lbs/2.204623; 
     double metricton = lbs/2204.623; 
     brush.setFont(font); 
     brush.drawString(name, 55, 50); 
     brush.drawString(String.valueOf(lbs), 55, 150); 
     brush.drawString(String.valueOf(oz), 55, 170); 
     brush.drawString(String.valueOf(kilo), 55, 190); 
     brush.drawString(String.valueOf(metricton), 55, 210); 
    } 

    public static void main(String[] args) { 
     JCalculateWeight frame = new JCalculateWeight(); 
     frame.setSize(500, 450); 
     frame.setVisible(true); 
    } 
} 
+0

你有問題嗎?你在說什麼例外? – 2010-10-09 19:37:50

回答

0

你在談論NumberFormatException每當終端用戶輸入無效weight,不是嗎?你想在用戶輸入無效時重新顯示對話框?

其中一種方法是隻要重量爲零,就可以在循環中執行此操作。在frame創建後,在main()方法內執行此操作。這是變化。

// ... 
String name; 
double weight; 

@Override 
public void paint(Graphics brush) { 
    // ... 
    double lbs = weight; // "weight" can also just be renamed to "lbs", then you can remove this line. 
    // ... 
} 

public static void main(String[] args) { 
    JCalculateWeight frame = new JCalculateWeight(); 
    frame.name = JOptionPane.showInputDialog(frame, "What's your name?"); 
    while (frame.weight == 0) { 
     try { 
      frame.weight = Double.parseDouble(JOptionPane.showInputDialog(frame, "What's your weight (lbs)")); 
     } catch (NumberFormatException e) { 
      // Maybe log it? Maybe change the question message? 
     } 
    } 
    // ... 
} 
0

你是很新的揮杆,我想。

我不知道你在說什麼異常,但我應該做這種方式:

public WeightCalculatorFrame extends JComponent 
{ 
    private String name; 
    private double lbs; 

    public WeightCalculatorFrame() 
    { 
     name = JOptionPane.showInputDialog("What is your name?"); 
     try 
     { 
      lbs = Double.parseDouble(JOptionPane.showInputDialog("What is your weight in lbs")); 
     } catch (NumberFormatException e) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid decimal format!\nWill now exit"); 
      System.exit(-1); 

     JFrame frame = new JFrame("Weight Calculator"); 
     frame.setPreferredSize(new Dimension(300, 400)); 
     frame.add(this); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    @Override 
    public void paintComponent(Graphics g) 
    { 
     g.setFont(yourFont); 
     g.drawString("Your Name: " + name, x, y); 
     double kilo = lbs/....; 
     double ... = lbs/....; 
     // Here all your calculations... 
     g.drawString("LBS: " + lbs, x, y); 
     g.drawString("Kilo: " + kilo, x, y); 
    } 
} 

這個初始化:

new WeightCalculatorFrame();