Я пытаюсь открыть окно с указанием скорости и угла, а также указать количество выстрелов шара. Это дает мне ошибку времени выполнения, и я не уверен, почему. Часть, которая вызывает ошибку в JOptionPane.showInternalMessageDialogJOptionPane.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: parentComponent не имеет действительный родитель \t на javax.swing.JOptionPane.createInternalFrame (JOptionPane.java:1486) \t на javax.swing.JOptionPane.showInternalOptionDialog (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