gpt4 book ai didi

java - 如何在同一个窗口中依次打开2个类(class)

转载 作者:行者123 更新时间:2023-12-01 13:25:40 24 4
gpt4 key购买 nike

我正在使用 NetBeans IDE 在 javafx 中开发一个项目。我也是javafx新手。我几乎创建了所有的类。现在我正在尝试将它们联系起来。我的意思是,当从一个类按下按钮时,其他类的功能应该起作用。我需要将该功能显示在同一窗口上。我将在这里提供我的一个类(class)的代码。我没有在此处包含导入语句和其他不必要的代码部分:

package login;

public class Login extends Application {

TextField t1,t2,t4,t5;
PasswordField t3;
ComboBox comboBox1,comboBox2,comboBox3;

private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;

@Override
public void start(Stage stage) {

BorderPane border = new BorderPane();

border.setTop(loginHBox1());
border.setLeft(loginVBox1());
border.setRight(loginVBox2());

Scene scene = new Scene(border,700,550);
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);

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 vbox = new VBox();
vbox.setPadding(new Insets(20,30,15,50));
vbox.setSpacing(10);

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 t11=new TextField();
t11.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 pw11=new PasswordField();
pw11.setPrefSize(150,30);

Button b1=new Button("LOG IN");
b1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
b1.setPrefSize(80,5);

btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// THIS IS THE PART WHERE AM CALLING THE FUNCTION OF OTHER CLASS. WHICH FUNCTION
// SHOULD I CALL HERE ?
}
});

vbox.getChildren().addAll(lb3,lb1,t11,lb2,pw11,b1);

return vbox;
}
public static void main(String[] args)
{
launch(args);
}
}

现在我将提供其他类的代码。这里也不包括整个代码。

package userpage;

public class UserPage extends Application {

TextField t1,t3,t4;
ComboBox comboBox1;

private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;

@Override
public void start(Stage stage) {

BorderPane border = new BorderPane();

HBox hbox = addHBox1();
border.setTop(hbox);
border.setRight(addVBox1());
border.setLeft(addVBox2());
border.setBottom(addHBox2());

Scene scene = new Scene(border,700,450);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(UserPage.class.getResource("UserPage.css").toExternalForm());
stage.show();
}

private HBox addHBox1() {

HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 280));
hbox.setSpacing(10); // Gap between nodes

Label lb1=new Label("HOME PAGE");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,22));
lb1.setTextFill(Color.BLUEVIOLET);

hbox.getChildren().addAll(lb1);

return hbox;
}

private VBox addVBox1() {

VBox vbox = new VBox();
vbox.setPadding(new Insets(20,40,30,4));
vbox.setSpacing(10);

MenuBar menuBar = new MenuBar();

Menu menuFile1 = new Menu("ADD");
Menu menuFile2 = new Menu("EDIT");
Menu menuFile3 = new Menu("VIEW");
Menu menuFile4 = new Menu("HELP");

MenuItem add1 = new MenuItem("ENTER STUDENT DETAILS");
MenuItem add2 = new MenuItem("ENTER C-MARK");
MenuItem add3 = new MenuItem("ENTER ATTENDANCE");

MenuItem add4 = new MenuItem("EDIT STUDENT DETAILS");
MenuItem add6 = new MenuItem("EDIT C-MARK");
MenuItem add8 = new MenuItem("EDIT ATTENDANCE");

MenuItem add10 = new MenuItem("STUDENT DETAILS");
MenuItem add11 = new MenuItem("C-MARK");
MenuItem add12 = new MenuItem("ATTENDANCE");

MenuItem add13 = new MenuItem("VIEW HELP");

menuFile1.getItems().addAll(add1,add2,add3);
menuFile2.getItems().addAll(add4,add6,add8);
menuFile3.getItems().addAll(add10,add11,add12);
menuFile4.getItems().addAll(add13);
menuBar.getMenus().addAll(menuFile1,menuFile2,menuFile3,menuFile4);

vbox.getChildren().addAll(menuBar);

return vbox;

}
public static void main(String[] args) {
launch(args);
}

}

最佳答案

在 JavaFX 应用程序中,您应该只有一个扩展 Application 的类。如果 UserPage 应该是一个 UI 组件,为什么不将其设为所提供 Pane 之一的子类(例如 AnchorPane、BorderPane 等)。对于你的情况

public class UserPage extends BorderPane{

TextField t1,t3,t4;
ComboBox comboBox1;

private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;

public UserPage(){
this.setTop(addHBox1());
this.setRight(addVBox1());
this.setLeft(addVBox2());
this.setBottom(addHBox2());
}

//.... Your methods
}

对我来说似乎最合理。从UserPage 中删除Scene 相关代码。它不属于那里。使用修改后的 UserPage 类,如果您想在以下位置打开 UserPage,则 Login 类中的 EventHandler 可以包含此代码同一个窗口。

btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
((Stage)btnl2.getScene().getWindow()).setScene(new Scene(new UserPage()));
}
});

如果您想在另一个窗口中打开 UserPage,则可以这样做。

btnl2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Stage usrpagestage = new Stage();
usrpagestage.setScene(new Scene(new UserPage()));
((Stage)btnl2.getScene().getWindow()).close();
usrpagestage.show();
}
});

如果您的 Login 类也应该是一个 UI 组件,那么它不应该扩展 Application 而应该扩展所提供的 Pane 之一。例如

public class Login extends GridPane{ .... }

您的应用程序启动可能如下所示

public class Main extends Application{

@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
Login loginview = new Login();
Scene scene = new Scene(loginview, 400, 200);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args){
launch(args);
}
}

我认为会更干净。

关于java - 如何在同一个窗口中依次打开2个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21819126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com