0
我正在使用NetBeans IDE在javafx中開發項目。我想在同一個窗口中打開一個新頁面。怎麼做 ?我將提供下面的代碼。我不在這裏包括進口報表。它在我的實際代碼中。如何在javafx的相同窗口中打開新頁面
package welcomepage;
public class WelcomePage extends Application {
@Override
public void start(Stage stage) {
// Use a border pane as the root for scene
BorderPane border = new BorderPane();
border.setTop(addVBox());
border.setLeft(addVBox1());
Scene scene = new Scene(border,700,450);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(WelcomePage.class.getResource("WelcomePage.css").toExternalForm());
stage.show();
}
private VBox addVBox() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(5, 12, 5, 20));
vbox.setSpacing(10); // Gap between nodes
//vbox.setStyle("-fx-background-color: #999999;");
Image image = new Image(getClass().getResourceAsStream("logo11.png"));
Label lb1=new Label(" C - MARK AND ATTENDANCE CALCULATOR");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,28));
lb1.setTextFill(Color.BLACK);
lb1.setGraphic(new ImageView(image));
vbox.getChildren().addAll(lb1);
return vbox;
}
private VBox addVBox1()
{
VBox vbox1=new VBox();
vbox1.setPadding(new Insets(20, 2, 15, 20));
vbox1.setSpacing(20);
Button btnl2=new Button("SIGN IN");
btnl2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl2.setPrefSize(300,60);
btnl2.setStyle(" -fx-base: #0066cc;");
//Image imageOk = new Image(getClass().getResourceAsStream("icon22.png"));
//btnl2.setGraphic(new ImageView(imageOk));
final Tooltip tooltip2 = new Tooltip();
tooltip2.setText("If you have an account,\nSign in here.");
btnl2.setTooltip(tooltip2);
btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
signin();
}
});
Button btnl4=new Button("HELP");
btnl4.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl4.setPrefSize(300,60);
btnl4.setStyle(" -fx-base: #0066cc;");
final Tooltip tooltip4 = new Tooltip();
tooltip4.setText("Get help content\nabout this software.");
btnl4.setTooltip(tooltip4);
btnl4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
help();
}
});
Button btnl5=new Button("ABOUT");
btnl5.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl5.setPrefSize(300,60);
btnl5.setStyle(" -fx-base: #0066cc;");
final Tooltip tooltip5 = new Tooltip();
tooltip5.setText("Know about\nthis software.");
btnl5.setTooltip(tooltip5);
btnl5.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
about();
}
});
Button btnl6=new Button("EXIT");
btnl6.setFont(Font.font("Calibri",FontWeight.BOLD,16));
btnl6.setPrefSize(300,60);
btnl6.setStyle(" -fx-base: #0066cc;");
//Image imageOk = new Image(getClass().getResourceAsStream("cross.png"));
//btnr3.setGraphic(new ImageView(imageOk));
final Tooltip tooltip6 = new Tooltip();
tooltip6.setText("Exit if you had\nfinished your works.");
btnl6.setTooltip(tooltip6);
btnl6.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.exit(0);
}
});
vbox1.getChildren().addAll(btnl2,btnl4,btnl5,btnl6);
return vbox1;
}
public void signin()
{
Stage stage=new Stage();
BorderPane border = new BorderPane();
border.setTop(loginHBox1());
border.setLeft(loginVBox1());
border.setRight(loginVBox2());
Scene scene = new Scene(border,700,450);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(Login.class.getResource("Login.css").toExternalForm());
stage.show();
}
private HBox loginHBox1() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 10, 180));
hbox.setSpacing(10); // Gap between nodes
Label lb1=new Label("LOG IN OR CREATE NEW ACCOUNT");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,26));
lb1.setTextFill(Color.BLACK);
hbox.getChildren().addAll(lb1);
return hbox;
}
private VBox loginVBox1() {
VBox hbox = new VBox();
hbox.setPadding(new Insets(20,30,15,50)); // Set all sides to 10
hbox.setSpacing(10); // Gap between nodes
Label lb3=new Label("LOG IN");
lb3.setAlignment(Pos.CENTER);
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,24));
lb3.setTextFill(Color.BLACK);
Label lb1=new Label("Username");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb1.setTextFill(Color.BLACK);
TextField t1=new TextField();
t1.setPrefSize(150,30);
Label lb2=new Label("Password");
lb2.setAlignment(Pos.CENTER);
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb2.setTextFill(Color.BLACK);
PasswordField pw1=new PasswordField();
pw1.setPrefSize(150,30);
Button b1=new Button("LOG IN");
b1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
b1.setPrefSize(80,5);
hbox.getChildren().addAll(lb3,lb1,t1,lb2,pw1,b1);
return hbox;
}
private VBox loginVBox2()
{
VBox hbox1 = new VBox();
hbox1.setPadding(new Insets(15, 50, 15, 10));
hbox1.setSpacing(10);
Label lb4=new Label("CREATE NEW ACCOUNT");
lb4.setFont(Font.font("Calibri",FontWeight.BOLD,24));
lb4.setPrefSize(250,30);
lb4.setTextFill(Color.BLACK);
Label lb1=new Label("Full Name ");
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb1.setPrefSize(100, 30);
lb1.setTextFill(Color.BLACK);
TextField t1=new TextField();
t1.setPrefSize(50,30);
Label lb2=new Label("User Name ");
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb2.setPrefSize(150, 30);
lb2.setTextFill(Color.BLACK);
TextField t2=new TextField();
t2.setPrefSize(100,30);
Label lb3=new Label("Password ");
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb3.setPrefSize(150, 30);
lb3.setTextFill(Color.BLACK);
PasswordField t3=new PasswordField();
t3.setPrefSize(100,30);
Label lb5=new Label("Gender ");
lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb5.setPrefSize(150, 30);
lb5.setTextFill(Color.BLACK);
ObservableList<String> options2 =
FXCollections.observableArrayList(
"Male","Female");
final ComboBox comboBox2 = new ComboBox(options2);
comboBox2.setPrefSize(250,30);
Button btn1=new Button("CREATE");
btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
btn1.setPrefSize(100,30);
hbox1.getChildren().addAll(lb4,lb1,t1,lb2,t2,lb3,t3,lb5,comboBox2,btn1);
return hbox1;
}
public static void main(String[] args) {
launch(args);
}
}
試試這個在這裏,你可以在窗格中打開另一個文件 http://stackoverflow.com/questions/21252007/loading-fxml-files-inside-a-main-fxml-file –
@AnshulParashar我沒有任何fxml文件。現在做什麼 ? – TomJ
使用'scene.setRoot()' –