我需要将CSS文件添加到主场景中。这是第一年的JavaFX项目,我们不能使用FXML,所以这是我知道如何在不同表单之间导航的唯一方法,关于如何在JavaFX类之间切换的任何其他建议将不胜感激。
package javalearningcurve;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author Conor
*/
public class MainMenu extends Stage {
GridPane grid = new GridPane();
Text scenetitle = new Text("Main Menu");
Button btnQuiz = new Button("New Quiz");
Button btnViewResults = new Button("View Results");
Button btnLogout = new Button("Logout");
Button btnQuit = new Button("Quit");
HBox hbBtn = new HBox();
MainMenu() {
this.setTitle("Main Menu");
grid.setAlignment(Pos.TOP_CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10,25,25,25));
scenetitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
scenetitle.setFill(Color.RED);
grid.setHalignment(scenetitle, HPos.CENTER);
hbBtn.setPrefWidth(200);
hbBtn.setAlignment(Pos.CENTER);
btnQuiz.setMinWidth(hbBtn.getPrefWidth());
btnViewResults.setMinWidth(hbBtn.getPrefWidth());
btnLogout.setMinWidth(hbBtn.getPrefWidth());
btnQuit.setMinWidth(hbBtn.getPrefWidth());
grid.add(scenetitle, 0, 2, 2, 1);
grid.add(btnQuiz, 0, 3);
grid.add(btnViewResults,0 , 4);
grid.add(btnLogout,0 , 5);
grid.add(btnQuit,0 , 6);
//primaryStage.show();
this.setScene(new Scene(grid, 400, 275));
this.show();
}
}
答案 0 :(得分:0)
rdist
基本上,代替scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm());
执行此操作:
this.setScene(new Scene(grid, 400, 275));