2014-02-17 66 views
0

我正在使用NetBeans IDE在javafx中開發一個項目。我也是javafx的新手。我幾乎創建了所有的課程。現在我試圖鏈接它們。我的意思是當一個類按下按鈕時,其他類的函數應該工作。我需要將該功能顯示在同一個窗口中。我將在這裏提供我的課程的代碼。我沒有在這裏包括進口報表和其他不必要的代碼部分:如何在同一窗口中一個接一個地打開2個類

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); 
} 

} 

回答

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 
} 

對我來說似乎最合理。從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,而且所提供的窗格中的一個。例如

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); 
    } 
} 

將在我看來更清潔。

+0

所有這些我以錯誤的方式在做我的整個項目...你幫助了我..非常感謝.. – TomJ

相關問題