JavaFx Notes

源文章(国内不知道要不要用梯子)

Change sub fxml gui parts at runtime with Button Click

MainApp.java
Loads the MainView.fxml
MainView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


 <BorderPane xmlns:fx="http://javafx.com/fxml">
<center>
    <Pane fx:id="content"/>
    <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");
    content.getChildren().clear();
    content.getChildren().add(FXMLLoader.load(getClass().getResource("Content2.fxml"));
}
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

}

Locate fxml file in other package

example
Parent root = FXMLLoader.load(getClass().getResource("/jfxtest2/Screen.fxml"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值