【发布时间】:2022-01-30 00:12:15
【问题描述】:
我正在尝试在函数“Initialize()”中添加来自 javaFX 窗口的按钮列表。但我只找到在显示窗口之前学习创建按钮的答案。我想添加到现有的 JavaFx 窗口。 对于这个窗口,我有一个 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLsControllers.sistemalogin.cadernos.cadernoController">
<children>
<Text fill="#3f435a" layoutX="51.0" layoutY="88.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Olá,">
<font>
<Font size="35.0" />
</font>
</Text>
<Text fill="#3e42599b" layoutX="51.0" layoutY="115.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Seus cadernos">
<font>
<Font size="13.0" />
</font>
</Text>
<Button fx:id="voltarBtn" mnemonicParsing="false" onAction="#voltaTela" prefHeight="47.0" prefWidth="63.0" style="-fx-background-color: transparent;" text="<" textFill="#3f435a">
<font>
<Font size="23.0" />
</font>
</Button>
<Button layoutX="613.0" layoutY="1.0" mnemonicParsing="false" onAction="#fechaAba" prefHeight="31.0" prefWidth="31.0" style="-fx-background-color: transparent;" text="X" textFill="#3f435a" fx:id="xBtn">
<font>
<Font size="16.0" />
</font>
</Button>
<Button fx:id="caderno001" alignment="TOP_LEFT" layoutX="51.0" layoutY="144.0" mnemonicParsing="false" onAction="#entraCaderno" prefHeight="146.0" prefWidth="107.0" style="-fx-background-color: #3f435a;" text="Banco de Dados" textAlignment="CENTER" textFill="WHITE">
<font>
<Font size="11.0" />
</font>
</Button>
<Button fx:id="addCadernoBtn" layoutX="538.0" layoutY="373.0" mnemonicParsing="false" onAction="#adicionaCadernoNovo" prefHeight="50.0" prefWidth="53.0" style="-fx-background-color: transparent;" text="+" textFill="#3f435a">
<font>
<Font size="24.0" />
</font>
</Button>
<Button fx:id="irDiarioBtn" layoutX="559.0" layoutY="50.0" mnemonicParsing="false" onAction="#abreDiario" prefHeight="12.0" prefWidth="53.0" style="-fx-background-color: #3f435a;" text="Diário" textFill="WHITE">
<font>
<Font size="13.0" />
</font>
</Button>
<Text fx:id="nomeLabelText" fill="#3f435a" layoutX="111.0" layoutY="88.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fulano">
<font>
<Font size="35.0" />
</font>
</Text>
</children>
</AnchorPane>
并为此 FXML 创建一个控件。这个控件:
package FXMLsControllers.sistemalogin.cadernos;
import java.io.IOException;
import application.sceneController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Button;
import javafx.scene.Parent;
public class cadernoController {
private String email;
private String nome;
public void initData(String email,String nome){
setEmail(email);
setNome(nome);
nomeLabelText.setText(getNome());
}
@FXML
public void initialize() throws IOException {
//I want to add a button in this function
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@FXML
private Text nomeLabelText;
@FXML
private Button addCadernoBtn;
@FXML
private Button caderno001;
@FXML
private Button irDiarioBtn;
@FXML
private Button voltarBtn;
@FXML
private Button xBtn;
@FXML
void abreDiario(ActionEvent event) throws IOException {
mostrarTelaDoDiario(event);
}
@FXML
void adicionaCadernoNovo(ActionEvent event) {
}
@FXML
void fechaAba(ActionEvent event) throws IOException {
Alert alertaFecharApp = new Alert(AlertType.CONFIRMATION);
alertaFecharApp.setHeaderText("Quer mesmo fechar o aplicativo?");
alertaFecharApp.setContentText("Bloquim sentirá a sua falta.");
if(alertaFecharApp.showAndWait().get() != ButtonType.OK){
return;
}
else{
System.exit(0);
fecharStage();
mostrarTelaLogin(event);
}
}
@FXML
void entraCaderno(ActionEvent event) throws IOException {
mostrarTelaDeAnotação(event);
}
@FXML
void voltaTela(ActionEvent event) throws IOException {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setHeaderText("Quer mesmo voltar à tela de Login?");
alert.setContentText("Voltar à tela de Login irá resultar em um processo de Logoff.");
if(alert.showAndWait().get() != ButtonType.OK){
return;
}
}
private void mostrarTelaDoDiario(ActionEvent event) throws IOException{
fecharStage();
sceneController sc = new sceneController();
sc.trocarParaTelaDiario(event,getEmail(),getNome());
}
private void mostrarTelaDeAnotação(ActionEvent event) throws IOException{
fecharStage();
sceneController sc = new sceneController();
sc.trocarParaTelaAnotacoes(event,getEmail(),getNome());
}
private void mostrarTelaLogin(ActionEvent event) throws IOException{
fecharStage();
sceneController sc = new sceneController();
sc.trocarParaTelaLogin(event);
}
private void fecharStage(){
Stage stage = (Stage) xBtn.getScene().getWindow();
stage.close();
}
}
我可以访问所有现有元素,但我想添加一个新按钮。在我对 DB 进行咨询之后,需要加载这个按钮,并且在我需要在这个表上创建一个来自哪个注册表的按钮之后。所以,因此,我需要在加载窗口后创建一个按钮。
【问题讨论】:
-
通过有关 javafx 的教程学习如何将节点添加到土地 - 请遵守 java 命名约定