我正在使用JavaFX构建一个程序,我有一个Accordion ac
注入@FXML
,我有一个按钮,可以在Accordion中创建一个标题窗格。问题是,单击此按钮会为手风琴添加一个标题窗格。
我正在使用此代码:
t1.setContent(l);
ac.getPanes().add(t1);
我想在每个按钮上添加一个窗格。请帮助我,谢谢你!
答案 0 :(得分:0)
这就是你要找的东西吗?
<强> Scene.fxml 强>
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="404.0" prefWidth="543.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.brocode.tg.magisterka.mavenproject1.FXMLController">
<children>
<Button fx:id="button" layoutX="193.0" layoutY="283.0" onAction="#handleButtonAction" text="Click Me!" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="193.0" AnchorPane.rightAnchor="193.0" />
<ScrollPane fitToHeight="true" fitToWidth="true" prefViewportHeight="160.0" prefViewportWidth="320.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<Accordion fx:id="accordion" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</content>
</ScrollPane>
</children>
</AnchorPane>
<强> FXMLController 强>
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
public class FXMLController implements Initializable {
@FXML
private Accordion accordion;
@FXML
private void handleButtonAction(ActionEvent event) {
AnchorPane newPanelContent = new AnchorPane();
newPanelContent.getChildren().add(new Label("Hello World"));
TitledPane pane = new TitledPane("World Pane", newPanelContent);
System.out.println(accordion);
accordion.getPanes().add(pane);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
每次单击按钮时,都会添加新面板。如果您将来对该面板做了一些事情,可以将其分配到字段(列表),或通过getChildren()
方法从手风琴中获取。
已对代码进行了编辑,以便更好地显示添加的窗格