JavaFX 改变场景不工作
JavaFX changing scene not working
我正在尝试在 JavaFX 中切换场景。到目前为止,我已经创建了 3 个场景 - 登录屏幕、个人资料和注册。出于某种原因,我无法从登录屏幕切换到配置文件。登录屏幕中的 "Register" 按钮以及注册屏幕中的 "Back" 按钮正在工作。单击 "Log in" 按钮时,没有任何反应。我错过了什么?这是主要的 class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import jedzenie.windows.LogInScreen;
import jedzenie.windows.ProfileInfoScreen;
import jedzenie.windows.RegisterScreen;
public class App extends Application
{
Stage stage;
Scene scene, scLogIn;
LogInScreen logInScreen;
RegisterScreen regis;
ProfileInfoScreen profileScreen;
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("FoodCalc");
stage = primaryStage;
logInScreen = new LogInScreen(this);
scLogIn = new Scene(logInScreen, 400, 400);
scLogIn.getStylesheets().add(getClass().getResource("logInScreen.css").toExternalForm());
primaryStage.setScene(scLogIn);
primaryStage.show();
}
public void moveToRegisterScreen(){
regis = new RegisterScreen(this);
Scene scRegister = new Scene(regis, 575, 250);
scRegister.getStylesheets().add(getClass().getResource("registerScreen.css").toExternalForm());
stage.setScene(scRegister);
}
public void logOut(){
stage.setScene(scLogIn);
}
public void logIn(){
profileScreen = new ProfileInfoScreen(this);
Scene scProfile = new Scene(profileScreen, 500, 400);
scProfile.getStylesheets().add(getClass().getResource("registerScreen.css").toExternalForm());
stage.setScene(scProfile);
}
public static void main( String[] args )
{
launch(args);
}
}
登录屏幕class:
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import jedzenie.app.App;
public class LogInScreen extends GridPane {
App main;
public LogInScreen(App main){
this.main = main;
setAlignment(Pos.CENTER);
//setGridLinesVisible(true);
setVgap(15);
setHgap(10);
Label welc = new Label("Welcome to FoodCalc. Please log in or register.");
add(welc, 0, 0, 2, 1);
Label lbUser = new Label("Username: ");
add(lbUser, 0, 1);
TextField tfUser = new TextField();
add(tfUser, 1, 1);
Label lbPass = new Label("Password: ");
add(lbPass, 0, 2);
PasswordField pfPass = new PasswordField();
add(pfPass, 1, 2);
Button logIn = new Button("Log in");
add(logIn, 1, 3);
setHalignment(logIn, HPos.RIGHT);
logIn.setOnAction((ActionEvent e) -> {
main.logIn();
});
Button register = new Button("Register");
add(register, 1, 4);
setHalignment(register, HPos.CENTER);
register.setOnAction((ActionEvent e) -> {
main.moveToRegisterScreen();
});
}
}
和 RegisterScreen class:
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import jedzenie.app.App;
public class RegisterScreen extends BorderPane {
App main;
Button register;
ComboBox age;
PasswordField pfPass, pfConfPass;
public RegisterScreen(App main){
this.main = main;
setCenter(gridPane());
setBottom(hbox());
setPadding(new Insets(25,25,25,25));
}
GridPane gridPane() {
GridPane gp = new GridPane();
gp.setGridLinesVisible(true);
gp.setVgap(15);
gp.setHgap(10);
gp.setAlignment(Pos.CENTER);
Label lbUser = new Label("Username: ");
gp.add(lbUser, 0, 1);
TextField tfUser = new TextField();
gp.add(tfUser, 1, 1);
Label lbPass = new Label("Password: ");
gp.add(lbPass, 0, 2);
pfPass = new PasswordField();
gp.add(pfPass, 1, 2);
Label lbConfPass = new Label("Confirm Password: ");
gp.add(lbConfPass, 0, 3);
pfConfPass = new PasswordField();
gp.add(pfConfPass, 1, 3);
Label lbEmail = new Label("Email: ");
gp.add(lbEmail, 0, 4);
TextField tfEmail = new TextField();
gp.add(tfEmail, 1, 4);
Label lbSex = new Label("Sex: ");
gp.add(lbSex, 2, 1);
ComboBox cbSex = new ComboBox();
gp.add(cbSex, 3, 1);
Label lbAge = new Label("Age: ");
gp.add(lbAge, 2, 2);
ComboBox cbAge = new ComboBox();
gp.add(cbAge, 3, 2);
Label lbWeight = new Label("Weight: ");
gp.add(lbWeight, 2, 3);
TextField tfWeight = new TextField();
gp.add(tfWeight, 3, 3);
return gp;
}
HBox hbox(){
HBox hbox = new HBox(10);
Button back = new Button("Back");
hbox.getChildren().add(back);
back.setOnAction((ActionEvent e) ->{
main.logOut();
});
Button register = new Button("Register");
hbox.getChildren().add(register);
return hbox;
}
}
我也一直在尝试寻找有效方法和无效方法及其实现之间的区别。还是找不到原因。
编辑:抱歉,我没有注意到我没有放置 ProfileInfoScreen class。这是:
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import jedzenie.app.App;
public class ProfileInfoScreen extends BorderPane {
App main;
Insets ins = new Insets(10,10,10,10);
public ProfileInfoScreen(App main){
this.main = main;
setPadding(ins);
setTop(lbWelcome);
setLeft(vbox());
}
Label lbWelcome = new Label("Welcome, " + "XXXXXXXXXXX");
VBox vbox(){
VBox vbox = new VBox();
vbox.setSpacing(10);
vbox.setPadding(ins);
Button btWeight = new Button("Change weight");
vbox.getChildren().add(btWeight);
btWeight.setOnAction((ActionEvent e) -> {
//XXXXXXXXXX
});
return vbox;
}
}
正如 James_D 评论的那样,我尝试了您的代码,它运行良好。当我点击注册按钮时,它确实显示了 table,其中包含用户名、密码、性别、年龄、电子邮件字段。
我做了一个空白的 profileScreen,所以点击登录显示空白 window。我看不出有什么问题。
请确保您所有的导入都来自 javafx 包。
只是为了测试,尝试不同的 IDE(我在 Intellij 上试过)。
因为我没有足够的声誉无法发表评论,所以我在这里写了。
我知道这个问题已经得到解答,但是在你的 stage.setScene
行之后你的代码中没有 stage.show
可能会遇到一些问题。
我正在尝试在 JavaFX 中切换场景。到目前为止,我已经创建了 3 个场景 - 登录屏幕、个人资料和注册。出于某种原因,我无法从登录屏幕切换到配置文件。登录屏幕中的 "Register" 按钮以及注册屏幕中的 "Back" 按钮正在工作。单击 "Log in" 按钮时,没有任何反应。我错过了什么?这是主要的 class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import jedzenie.windows.LogInScreen;
import jedzenie.windows.ProfileInfoScreen;
import jedzenie.windows.RegisterScreen;
public class App extends Application
{
Stage stage;
Scene scene, scLogIn;
LogInScreen logInScreen;
RegisterScreen regis;
ProfileInfoScreen profileScreen;
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("FoodCalc");
stage = primaryStage;
logInScreen = new LogInScreen(this);
scLogIn = new Scene(logInScreen, 400, 400);
scLogIn.getStylesheets().add(getClass().getResource("logInScreen.css").toExternalForm());
primaryStage.setScene(scLogIn);
primaryStage.show();
}
public void moveToRegisterScreen(){
regis = new RegisterScreen(this);
Scene scRegister = new Scene(regis, 575, 250);
scRegister.getStylesheets().add(getClass().getResource("registerScreen.css").toExternalForm());
stage.setScene(scRegister);
}
public void logOut(){
stage.setScene(scLogIn);
}
public void logIn(){
profileScreen = new ProfileInfoScreen(this);
Scene scProfile = new Scene(profileScreen, 500, 400);
scProfile.getStylesheets().add(getClass().getResource("registerScreen.css").toExternalForm());
stage.setScene(scProfile);
}
public static void main( String[] args )
{
launch(args);
}
}
登录屏幕class:
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import jedzenie.app.App;
public class LogInScreen extends GridPane {
App main;
public LogInScreen(App main){
this.main = main;
setAlignment(Pos.CENTER);
//setGridLinesVisible(true);
setVgap(15);
setHgap(10);
Label welc = new Label("Welcome to FoodCalc. Please log in or register.");
add(welc, 0, 0, 2, 1);
Label lbUser = new Label("Username: ");
add(lbUser, 0, 1);
TextField tfUser = new TextField();
add(tfUser, 1, 1);
Label lbPass = new Label("Password: ");
add(lbPass, 0, 2);
PasswordField pfPass = new PasswordField();
add(pfPass, 1, 2);
Button logIn = new Button("Log in");
add(logIn, 1, 3);
setHalignment(logIn, HPos.RIGHT);
logIn.setOnAction((ActionEvent e) -> {
main.logIn();
});
Button register = new Button("Register");
add(register, 1, 4);
setHalignment(register, HPos.CENTER);
register.setOnAction((ActionEvent e) -> {
main.moveToRegisterScreen();
});
}
}
和 RegisterScreen class:
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import jedzenie.app.App;
public class RegisterScreen extends BorderPane {
App main;
Button register;
ComboBox age;
PasswordField pfPass, pfConfPass;
public RegisterScreen(App main){
this.main = main;
setCenter(gridPane());
setBottom(hbox());
setPadding(new Insets(25,25,25,25));
}
GridPane gridPane() {
GridPane gp = new GridPane();
gp.setGridLinesVisible(true);
gp.setVgap(15);
gp.setHgap(10);
gp.setAlignment(Pos.CENTER);
Label lbUser = new Label("Username: ");
gp.add(lbUser, 0, 1);
TextField tfUser = new TextField();
gp.add(tfUser, 1, 1);
Label lbPass = new Label("Password: ");
gp.add(lbPass, 0, 2);
pfPass = new PasswordField();
gp.add(pfPass, 1, 2);
Label lbConfPass = new Label("Confirm Password: ");
gp.add(lbConfPass, 0, 3);
pfConfPass = new PasswordField();
gp.add(pfConfPass, 1, 3);
Label lbEmail = new Label("Email: ");
gp.add(lbEmail, 0, 4);
TextField tfEmail = new TextField();
gp.add(tfEmail, 1, 4);
Label lbSex = new Label("Sex: ");
gp.add(lbSex, 2, 1);
ComboBox cbSex = new ComboBox();
gp.add(cbSex, 3, 1);
Label lbAge = new Label("Age: ");
gp.add(lbAge, 2, 2);
ComboBox cbAge = new ComboBox();
gp.add(cbAge, 3, 2);
Label lbWeight = new Label("Weight: ");
gp.add(lbWeight, 2, 3);
TextField tfWeight = new TextField();
gp.add(tfWeight, 3, 3);
return gp;
}
HBox hbox(){
HBox hbox = new HBox(10);
Button back = new Button("Back");
hbox.getChildren().add(back);
back.setOnAction((ActionEvent e) ->{
main.logOut();
});
Button register = new Button("Register");
hbox.getChildren().add(register);
return hbox;
}
}
我也一直在尝试寻找有效方法和无效方法及其实现之间的区别。还是找不到原因。
编辑:抱歉,我没有注意到我没有放置 ProfileInfoScreen class。这是:
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import jedzenie.app.App;
public class ProfileInfoScreen extends BorderPane {
App main;
Insets ins = new Insets(10,10,10,10);
public ProfileInfoScreen(App main){
this.main = main;
setPadding(ins);
setTop(lbWelcome);
setLeft(vbox());
}
Label lbWelcome = new Label("Welcome, " + "XXXXXXXXXXX");
VBox vbox(){
VBox vbox = new VBox();
vbox.setSpacing(10);
vbox.setPadding(ins);
Button btWeight = new Button("Change weight");
vbox.getChildren().add(btWeight);
btWeight.setOnAction((ActionEvent e) -> {
//XXXXXXXXXX
});
return vbox;
}
}
正如 James_D 评论的那样,我尝试了您的代码,它运行良好。当我点击注册按钮时,它确实显示了 table,其中包含用户名、密码、性别、年龄、电子邮件字段。 我做了一个空白的 profileScreen,所以点击登录显示空白 window。我看不出有什么问题。 请确保您所有的导入都来自 javafx 包。 只是为了测试,尝试不同的 IDE(我在 Intellij 上试过)。
因为我没有足够的声誉无法发表评论,所以我在这里写了。
我知道这个问题已经得到解答,但是在你的 stage.setScene
行之后你的代码中没有 stage.show
可能会遇到一些问题。