我试图让一个方框显示速度和角度的输入,以及显示球被射击的次数。它给了我一个运行时错误,我不知道为什么。导致错误的部分是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);
}
}
}
}
}
答案 0 :(得分:4)
根据API,您应该将showInternalMessageDialog与JInternalFrames一起使用,而您不是这样做的。解决方案:不要使用此JOptionPane,而是使用其他更合适的一个,例如showMessageDialog。
答案 1 :(得分:2)
您必须将JFrame的内容窗格作为方法中的第一个参数传递,而不仅仅是像现在这样的JFrame。所以你需要像这样调用你的消息语句(注意使用了frame.getContentPane()
,而不只是框架):
JOptionPane.showMessageDialog(frame.getContentPane(), "You fired "+comp.getNumBallsFired()+" times\n and you "+
(comp.isTargetHit()?"":"did not")+" hit the target!");
参考:Here。