【发布时间】:2017-08-13 12:31:37
【问题描述】:
我将工作代码划分为 2 个文件以避免混乱。以前也可以,但是把所有场景都放在一个班级里是很不愉快的。
在您单击精灵之前,它会将您从菜单带到游戏。然后我将带有游戏组和游戏场景的代码提取到游戏类中。
现在我可以看到当我按下精灵时它显示“点击”。这意味着改变场景正在起作用。 问题是在我分割文件后,第二个场景(游戏场景)显示的是第一个场景的内容而不是它自己的内容。 可能没有重绘。我该如何解决?谢谢
主类代码(菜单窗口):
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class EventFiltersExample extends Application {
@Override
public void start(Stage stage) {
Image playerImage = new Image("body.png");
ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);
Text text = new Text("Zegelardo");
text.setFont(Font.font(null, FontWeight.BOLD, 40));
text.setX(150);
text.setY(50);
Group menuGroup = new Group(playerImageView,text);
//Group gameGroup = new Group();
Scene menuScene = new Scene(menuGroup, 600, 300);
//Scene gameScene = new Scene(gameGroup, 600, 300);
stage.setTitle("Zegelardo");
stage.setScene(menuScene);
stage.show();
GameGroup gamegroup = new GameGroup();
EventHandler <MouseEvent> eventHandler = new EventHandler <MouseEvent>() {
@Override
public void handle(MouseEvent e) {
stage.setScene(gamegroup.gameScene);
System.out.println("Clicked.");
}
};
playerImageView.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
}
public static void main(String args[]){
launch(args);
}
}
带有游戏窗口构造器/方法的类代码:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.Parent;
import javafx.scene.Group ;
import javafx.scene.Parent ;
import javafx.scene.shape.Line ;
import javafx.stage.Stage;
public class GameGroup {
public Group gameGroup;
public Scene gameScene;
public GameGroup() {
Image playerImage = new Image("body.png");
ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);
Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);
}
public Parent getView() {
return gameGroup ;
}
}
【问题讨论】: