实际上,该消息是自我解释:UIManager.setLookAndFeel抛出一堆检查例外因而需要捕获(用try/catch块)或宣布被抛出(在调用方法中)。
因此,无论周围的通话用一个try/catch:
public class KeyEventDemo {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO handle me
} catch (InstantiationException e) {
// TODO handle me
} catch (IllegalAccessException e) {
// TODO handle me
} catch (UnsupportedLookAndFeelException e) {
// TODO handle me
}
}
}
或者添加抛出声明:
public class KeyEventDemo {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
}
如果你不想来处理他们每个人以特定的方式
public class KeyEventDemo {
static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e) {
// TODO handle me
}
}
}
或用一抛:这可以通过使用Exception超进行更简洁的声明(注意,这传达的信息较少的方法的调用者,但主叫这里是JVM的,它并没有真正在这种情况下重要):
class KeyEventDemo {
static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
}