Java JOptionPane.showInternalMessageDialog导致运行时错误
我试图用输入的速度和角度来显示一个框,同时显示球被发射的次数。它给了我一个运行时错误,我不知道为什么。导致错误的部分是JOptionPane.showInternalMessageDialogJava JOptionPane.showInternalMessageDialog导致运行时错误,java,swing,joptionpane,Java,Swing,Joptionpane,我试图用输入的速度和角度来显示一个框,同时显示球被发射的次数。它给了我一个运行时错误,我不知道为什么。导致错误的部分是JOptionPane.showInternalMessageDialog import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JOptionPane; public class CannonGame { private static final int WIDTH=8
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);
}
}
}
}
}
导入java.awt.Graphics2D;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
公共类CannonGame{
专用静态最终整数宽度=800;
专用静态最终内部高度=600;
专用静态最终整数等待时间=10;
公共静态void main(字符串[]args){
JFrame=新JFrame(“加农炮游戏”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架尺寸(宽度、高度);
MyComponent comp=新的MyComponent();
帧。添加(comp);
frame.setVisible(true);
公司初始化位置();
frame.repaint();
布尔控制=真;
//提示用户输入角度和速度
while(续){
String vel=JOptionPane.showInputDialog(框架,“输入速度(1-100)”);
if(vel==null)
系统出口(0);
双速=双速。双速(vel);
而(速度<0 | |速度>100){
String vel1=JOptionPane.showInputDialog(框架,“输入速度(1-100)”);
if(vel==null)
系统出口(0);
Double velocity1=Double.parseDouble(vel1);
速度=速度1;
}
字符串ang=JOptionPane.showInputDialog(框架,“输入角度(0-90)”);
如果(ang==null)
系统出口(0);
双角度=Math.PI*Double.parseDouble(ang)/180.0;
而(角度<0 | |角度>1.5711111111){
字符串ang1=JOptionPane.showInputDialog(框架,“输入角度(0-90)”);
如果(ang==null)
系统出口(0);
Double angle1=Math.PI*Double.parseDouble(ang1)/180.0;
角度=角度1;
}
JOptionPane.showInternalMessageDialog(框架,“发射的球:+comp.getNumberAllsFired(),
“速度:“+velocity+”角度:“+angle,JOptionPane.INFORMATION_MESSAGE”);
//设置炮弹的动画,直到它击中地面
组件getReadyForShot(角度、速度);
while(comp.stillInFlight()){
组件更新(System.currentTimeMillis());
frame.repaint();
试一试{
线程。睡眠(等待时间);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
//打印尝试次数以及他们是否成功
if(comp.isTargetHit()){
JOptionPane.showMessageDialog(框架,“您已发射”+comp.getNumberlsFired()+“次\n和您”+
(comp.isTargetHit()?“”:“没有”)+“击中目标!”;
int answer=JOptionPane.showConfirmDialog(null,
“您想再次开火吗?”,“选择一个”,JOptionPane。是\否\选项);
如果(答案==JOptionPane.YES\u选项){
cont=真;
公司初始化位置();
}
否则{
cont=假;
系统出口(0);
}
}否则{
JOptionPane.showMessageDialog(框架,“您已发射”+comp.getNumberlsFired()+“次\n和您”+
(comp.isTargetHit()?“”:“没有”)+“击中目标!”;
int answer=JOptionPane.showConfirmDialog(null,
“您想再次开火吗?”,“选择一个”,JOptionPane。是\否\选项);
如果(答案==JOptionPane.YES\u选项){
cont=真;
}
否则{
cont=假;
系统出口(0);
}
}
}
}
}
根据API,您应该将showInternalMessageDialog与JInternalFrames一起使用,但您没有这样做。解决方案:不要使用此JOptionPane,而是使用其他更合适的JOptionPane,如showMessageDialog。根据API,您应该将showInternalMessageDialog与JInternalFrames一起使用,而不是这样做。解决方案:不要使用此JOptionPane,而是使用其他更合适的JOptionPane,如showMessageDialog。您必须将JFrame的内容窗格作为方法中的第一个参数传递,而不仅仅是像现在这样传递JFrame。因此,您需要像这样调用消息语句(注意使用的是frame.getContentPane()
,而不仅仅是frame):
参考:。您必须将JFrame的内容窗格作为方法中的第一个参数传递,而不仅仅是像现在这样传递JFrame。因此,您需要像这样调用消息语句(注意使用的是frame.getContentPane()
,而不仅仅是frame):
参考:。请显示完整的错误消息。让我们猜测它的形式很糟糕。线程“main”java.lang.RuntimeException:JOptionPane:parentComponent中的异常在javax.swing.JOptionPane.createInternalFrame(JOptionPane.java:1486)在javax.swing.JOptionPane.showInternalOptionDialog(JOptionPane.java:1259)在javax.swing.JOptionPane.showInternalMessageDialog中没有有效的父级(JOptionPane.java:1073)在javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1047)在CannonGame.main(CannonGame.java:54)请显示完整的错误消息。它的形式很差,无法让我们猜测。线程“main”中出现异常java.lang.RuntimeException:JOptionPane:parentComponent在javax.swing.JOptionPane.createInternalFrame(JOptionPane.java:1486)在javax.swing.JOptionPane.showInternalOptionDialog(JOp)中没有有效的父级
JOptionPane.showMessageDialog(frame.getContentPane(), "You fired "+comp.getNumBallsFired()+" times\n and you "+
(comp.isTargetHit()?"":"did not")+" hit the target!");