我正在使用NetBeans IDE在javafx中开发一个项目。也是javafx的新手。我几乎创建了所有课程。现在我想尝试链接他们。我的意思是当从一个类按下按钮时,应该使用其他类的功能。我需要将该功能显示在同一窗口中。我将在这里提供我的一个班级的代码。我没有在这里包含import语句和代码的其他不必要的部分:
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);
}
}
答案 0 :(得分:1)
在JavaFX应用程序中,您应该只有一个扩展Application
的类。如果UserPage
应该是一个UI组件,为什么不将它作为其中一个提供的窗格的子类(例如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
}
对我来说似乎最合理。从Scene
中删除UserPage
相关代码。它不属于那里。使用修改后的UserPage
类,如果您想在同一个窗口中打开EventHandler
,Login
类中的UserPage
可以拥有此代码。
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
,而应该扩展一个提供的窗格。例如
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);
}
}
在我看来会更清洁。