No errors, but crashing - JOptionPane - JavaFX

馋奶兔 提交于 2019-12-12 04:30:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!