令人沮丧的格式错误的网址异常。作为测试,我添加了“网址网址=新网址(”http://www.google.com“); ”只是为了查看一个非常简单的网址是否会引发错误,而且确实如此。我迷失了造成这种情况的原因。
import javax.swing.*;
import java.net.*;
public class Train {
public static void main(String[] args) {
URL url = new URL("http://www.google.com");
ImageIcon icon = new ImageIcon(new URL("https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"));**
Integer miles = Integer.parseInt(JOptionPane.showInputDialog("Enter the miles"));
Double gallons = Double.parseDouble(JOptionPane.showInputDialog("Enter the gallons"));
Double mpg = miles / gallons;
JOptionPane.showMessageDialog(null, "Miles per gallon is " + mpg, "Gass Mileage", JOptionPane.PLAIN_MESSAGE, icon);
}
}
线程“main”中的异常java.lang.Error:未解决的编译问题: 未处理的异常类型MalformedURLException 未处理的异常类型MalformedURLException
at Train.main(Train.java:13)
答案 0 :(得分:0)
你有编译错误,并且没有抛出的异常的try / catch 新的ImageIcon()
试试这个:
import javax.swing.*;
import java.net.*;
public class Train {
public static void main(String[] args) {
try {
URL url = new URL("http://www.google.com");
ImageIcon icon = new ImageIcon(new URL("https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"));
Integer miles = Integer.parseInt(JOptionPane.showInputDialog("Enter the miles"));
Double gallons = Double.parseDouble(JOptionPane.showInputDialog("Enter the gallons"));
Double mpg = miles / gallons;
JOptionPane.showMessageDialog(null, "Miles per gallon is " + mpg, "Gass Mileage", JOptionPane.PLAIN_MESSAGE, icon);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}