问题
I am trying to get a pop-up message when the cake node is clicked on. It prints to the console, but "JOptionPane.showMessageDialog(null,"Test");" crashes the program when I click on the cake (no errors). Any ideas?
class Cake extends Item {
double dx=3,dy=1.6;
Cake(String imageFile, double x, double y) {
super(imageFile, x, y);
}
@Override
public void move() {
this.setX(this.getX()+dx);
if(this.getX()>749 || this.getX()<-20) {
dx=-dx;
}
this.setY(this.getY()+dy);
if(this.getY()>530 || this.getY()<0) {
dy=-dy;
}
}
@Override
public void collision() {
//System.out.println("Cake");
JOptionPane.showMessageDialog(null,"Test");
}
}
回答1:
Don't use Swing's JOptionPane
in a JavaFX application. Use Dialog, or in this case an Alert instead:
@Override
public void collision() {
//System.out.println("Cake");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("Test");
alert.showAndWait();
}
来源:https://stackoverflow.com/questions/37631357/no-errors-but-crashing-joptionpane-javafx