如何在运行时使用Button Click更改子fxml gui部件 [英] How to change sub fxml gui parts at runtime with Button Click

查看:146
本文介绍了如何在运行时使用Button Click更改子fxml gui部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个大型的复杂gui构建框架,所以这个想法是使用mvc来制作所有东西,例如javafx 2.1中的样式,因此每个组件都有一个fxml文件,如果需要的话还有css,controller和model.我正在尝试找出如何更改子场景(运行时的子fxml).有人知道怎么做吗?我有点坚持.可以蜜蜂添加MainViewController吗?场景:用户单击任务栏上的按钮,包含的content1.fxml将替换为content2.fxml

I'm tryin to build a skeleton for a big complex gui, so the idea is to make everything with mvc like style in javafx 2.1, so every component has a fxml file and if needed css,controller and model. I'm tryin to figure out how to change sub scenes(sub fxml at runtime). Anybody know how to do it? I'm kinda stuck on this. May bee to add MainViewController? scenario: user clicks on button in taskbar and the included content1.fxml will be replaced with content2.fxml

此处是基本代码

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>

TaskBarController.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

}

}

推荐答案

不仅包括fxml,还为此创建一个业务逻辑层:

Don't just include fxml, create a business logic layer for that:

<BorderPane xmlns:fx="http://javafx.com/fxml">
<center>
    <Pane fx:id="content"/>
</center>

并在按钮单击处理程序中对其进行更新:

and update it in button click handlers:

@FXML
private void handleTaskBarButton2Action(ActionEvent event) {
   System.out.println("click! taskBarButton2");
   content.getChildren().clear();
   content.getChildren().add(FXMLLoader.load(getClass().getResource("Content2.fxml"));
}

这篇关于如何在运行时使用Button Click更改子fxml gui部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆