我试图在JavaFX中切换场景。到目前为止,我创建了3个场景 - 登录屏幕,个人资料和注册。出于某种原因,我无法从登录屏幕切换到个人资料。 "注册"登录屏幕中的按钮正在工作,以及"返回"注册屏幕中的按钮。点击"登录"按钮,什么也没发生。我错过了什么?这是主要的课程:
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);
}
}
LogInScreen类:
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类:
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类。这是: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;
}
}
答案 0 :(得分:0)
正如James_D评论的那样,我尝试了你的代码,它运行正常。当我点击注册按钮时,它会显示一个包含用户名,密码,性别,年龄,电子邮件字段的表格。 我做了一个空白的profileScreen,所以点击login会显示一个空白窗口。我没有看到问题。 请确保您的所有导入都来自javafx包。 只是为了测试,尝试不同的IDE(我在Intellij上尝试过)。
无法评论,因为我没有足够的声誉,所以我在这里写了。
答案 1 :(得分:0)
我知道这个问题已得到解答,但是在您拥有stage.show
行后,代码中没有stage.setScene
可能会遇到一些问题。