一直試圖改變我的代碼來捕捉異常,但無法弄清楚如何用這個去解決它。我偶爾會遇到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);
}
}
你有問題嗎?你在說什麼例外? – 2010-10-09 19:37:50