我正在嘗試爲一個大型複雜gui構建一個框架,所以我們的想法是在javafx 2.1中使用類似於mvc的樣式,因此每個組件都有一個fxml文件和如果需要的CSS,控制器和模型。我試圖找出如何更改子場景(在運行時的子fxml)。任何人都知道如何去做?我有點卡在這個。可能要添加MainViewController?場景:用戶點擊按鈕在任務欄和包括content1.fxml將content2.fxml如何在運行時使用按鈕更改sub fxml gui部件點擊
這裏的基本代碼來代替
MainApp.java
Loads the MainView.fxml
MainView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml">
<center>
<fx:include source="Content1.fxml"/>
</center>
<bottom>
<fx:include source="TaskBar.fxml"/>
</bottom>
</BorderPane>
Content1.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:id="content1">
<Label text="Hallo Java FX 2.1.1 Content1.fxml"/>
</StackPane>
Content2.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:id="content2">
<Label text="Hallo Java FX 2.1.1 Content2.fxml"/>
</StackPane>
TaskBar.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox xmlns:fx="http://javafx.com/fxml" spacing="10" alignment="center"
fx:id="taskBar" fx:controller="TaskBarController">
<children>
<Button fx:id="taskBarButton1" onAction="#handleTaskBarButton1Action"/>
<Button fx:id="taskBarButton2" onAction="#handleTaskBarButton2Action"/>
</children>
</HBox>
TaskBarCont roller.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
public class TaskBarController implements Initializable {
// Binding with the FXML
@FXML
private Button taskBarButton1;
@FXML
private Button taskBarButton2;
@FXML
private void handleTaskBarButton1Action(ActionEvent event) {
System.out.println("click! taskBarButton1");
}
@FXML
private void handleTaskBarButton2Action(ActionEvent event) {
System.out.println("click! taskBarButton2");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
}
票數圖片爲我所期望的Funktion http://www.alsphera.com/enter/howtodoit.png – Snow 2012-07-19 16:13:08