2017-01-09 8 views
0

現在のfxmlページからボタンをクリックして別のfxmlページを呼び出す簡単なツールを試しています。次のように 私のメインのJavaクラスが設計されています:ボタンクリックイベントで別のfxmlページを呼び出すことができません

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.stage.Stage; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TitledPane; 
import javafx.scene.layout.BorderPane; 


public class Main extends Application 
{ 

    @Override 
    public void start(Stage stage) { 
    try { 

     TitledPane page = (TitledPane)FXMLLoader.load(getClass().getResource("NewTest.fxml")); 
     Scene scene = new Scene(page); 
     stage.setScene(scene); 
      stage.setTitle("Welome Page"); 
      stage.show(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

public static void main(String[] args) { 
    Application.launch(Main.class, (java.lang.String[])null); 
}} 

とコントローラクラスとして設計されています

import javafx.event.ActionEvent; 
import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.control.Button; 
import javafx.scene.control.TitledPane; 
import javafx.stage.Stage; 


public class SimpleController implements Initializable 
{ 

    @FXML // fx:id="loginbtn" 
    private Button loginbtn; // Value injected by FXMLLoader 


    @Override // This method is called by the FXMLLoader when 
       initialization is complete 
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) { 
    assert loginbtn != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'."; 

    // initialize your logic here: all @FXML variables will have been injected 
    loginbtn.setOnAction(new EventHandler<ActionEvent>() { 

     public void handle(ActionEvent event) { 
      Stage stage; 
      Parent root; 
      if(event.getSource()==loginbtn){ 
       //get reference to the button's stage   
       stage=(Stage) loginbtn.getScene().getWindow(); 
       //load up OTHER FXML document 
       try{ 
        TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml")); 
       }catch(Exception e) 
       {e.printStackTrace();} 
       } 
     } 
    }); 
} 
} 

そして、FXMLページのようなものです:私は上をクリックしていた場合

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.PasswordField?> 
<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.control.TitledPane?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 

<TitledPane text="Welcome to the Vacation Planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"> 
    <content> 
     <AnchorPane prefHeight="330.0" prefWidth="497.0"> 
     <children> 
      <Label layoutX="14.0" layoutY="52.0" prefHeight="25.0" prefWidth="189.0" text="User Name"> 
       <font> 
        <Font size="24.0" /> 
       </font> 
      </Label> 
      <TextArea layoutX="53.0" layoutY="106.0" prefHeight="41.0" prefWidth="391.0" /> 
       <Label layoutX="14.0" layoutY="165.0" text="Password"> 
       <font> 
        <Font size="24.0" /> 
      </font> 
     </Label> 
     <Button fx:id="loginbtn" layoutX="114.0" layoutY="262.0" mnemonicParsing="false" text="Login"> 
      <font> 
       <Font size="18.0" /> 
      </font> 
     </Button> 
     <Button fx:id="exitbtn" layoutX="271.0" layoutY="262.0" mnemonicParsing="false" text="Exit"> 
      <font> 
       <Font size="18.0" /> 
      </font> 
      </Button> 
      <PasswordField layoutX="53.0" layoutY="207.0" prefHeight="39.0" prefWidth="391.0" /> 
     </children> 
     </AnchorPane> 
     </content> 
     </TitledPane> 

ボタンをクリックすると、ボタンをクリックして開こうとしている2番目のxmlページが開きません。

ありがとうございます。

答えて

1

Nodeをロードしてから何もしないでください。

既存のシーンに追加するか、そのシーンを交換する必要があります。

さらに
try{ 
    TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml")); 
    Scene newScene = new Scene(page); 
    stage.setScene(newScene); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

あなたはFXMLとコントローラが関連付けられていません。例えばmy.packageは、コントローラを含むパッケージとなり

<TitledPane text="Welcome to the Vacation Planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.package.SimpleController"> 

:これを行うにはFXMLのルートのコントローラクラスの完全修飾クラス名を持つfx:controller属性。

+0

こんにちは、私はあなたがまだ開こうとしているfxmlページを開いていないことを示唆しているコードを私達に試みました。 – priya89

+0

@ priya89どこかでコントローラクラスを使用していないのを見たことはありませんでした。今すぐ答えを編集...私は、コントローラが '初期化が完了しました'がコメントの中にないので、コンパイル時例外の代わりにコンパイルすると仮定します。 – fabian

+0

はい得ました..あなたの答えをありがとう.. :) – priya89

関連する問題