我試圖讓輸入的速度和角度出現一個框,以及顯示球已被觸發的次數。它給我一個運行時錯誤,我不知道爲什麼。導致該錯誤的部分是JOptionPane.showInternalMessageDialog導致運行時錯誤的JOptionPane.showInternalMessageDialog
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CannonGame {
private static final int WIDTH=800;
private static final int HEIGHT=600;
private static final int WAIT_TIME=10;
public static void main(String[] args) {
JFrame frame=new JFrame("Cannon Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH,HEIGHT);
MyComponent comp=new MyComponent();
frame.add(comp);
frame.setVisible(true);
comp.initializeLocations();
frame.repaint();
boolean cont = true;
//prompt user for angle and velocity
while(cont){
String vel=JOptionPane.showInputDialog(frame,"Enter the Velocity (1-100)");
if(vel == null)
System.exit(0);
Double velocity=Double.parseDouble(vel);
while(velocity < 0 || velocity > 100){
String vel1 = JOptionPane.showInputDialog(frame,"Enter the Velocity (1-100)");
if(vel == null)
System.exit(0);
Double velocity1 = Double.parseDouble(vel1);
velocity = velocity1;
}
String ang=JOptionPane.showInputDialog(frame,"Enter the angle (0-90)");
if(ang == null)
System.exit(0);
Double angle=Math.PI*Double.parseDouble(ang)/180.0;
while(angle < 0 || angle > 1.57111111111111){
String ang1=JOptionPane.showInputDialog(frame,"Enter the angle (0-90)");
if(ang == null)
System.exit(0);
Double angle1=Math.PI*Double.parseDouble(ang1)/180.0;
angle = angle1;
}
JOptionPane.showInternalMessageDialog(frame, "Balls fired: " + comp.getNumBallsFired(),
"velocity: " + velocity + "angle: " + angle, JOptionPane.INFORMATION_MESSAGE);
//animate the cannonball until it hits the ground
comp.getReadyForShot(angle,velocity);
while(comp.stillInFlight()) {
comp.update(System.currentTimeMillis());
frame.repaint();
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//print how many tries and if they hit it
if(comp.isTargetHit()){
JOptionPane.showMessageDialog(frame, "You fired "+comp.getNumBallsFired()+" times\n and you "+
(comp.isTargetHit()?"":"did not")+" hit the target!");
int answer = JOptionPane.showConfirmDialog(null,
"Would you like to fire again?", "choose one", JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION){
cont = true;
comp.initializeLocations();
}
else {
cont = false;
System.exit(0);
}
}else{
JOptionPane.showMessageDialog(frame, "You fired "+comp.getNumBallsFired()+" times\n and you "+
(comp.isTargetHit()?"":"did not")+" hit the target!");
int answer = JOptionPane.showConfirmDialog(null,
"Would you like to fire again?", "choose one", JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION){
cont = true;
}
else {
cont = false;
System.exit(0);
}
}
}
}
}
什麼是錯誤? – BitNinja
請顯示完整的錯誤信息。這讓我們猜測是糟糕的形式。 –
異常在線程 「主」 了java.lang.RuntimeException:JOptionPane的:爲父級不必在javax.swing.JOptionPane.showInternalOptionDialog有效的父 \t在javax.swing.JOptionPane.createInternalFrame(JOptionPane.java:1486) \t( JOptionPane.java:1259) \t在javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1073) \t在javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1047) \t在CannonGame.main(CannonGame.java :54) – user3308067