该类包含 3 个按钮“btnSocieta”、“btnUnitaLocali”、“btnReparti”。 第一个是默认启用的,其他两个是禁用的。 在 stackPane 中,我显示了您可以立即找到的起始页面。
他们必须在调用方法 setEnableBtnUnitaLocale 或其他方法后更改状态
package View.Controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Creazione implements Initializable {
@FXML
private JFXButton btnSocieta;
@FXML
private JFXButton btnUnitaLocali;
@FXML
private JFXButton btnReparti;
@FXML
private StackPane stackPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
btnUnitaLocali.setDisable(true);
btnReparti.setDisable(true);
try {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_societa.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
} catch (IOException e) {
e.printStackTrace();
}
}
public void switchToSocieta(javafx.event.ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_societa.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
}
public void switchToUnitaLocali(javafx.event.ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_unitalocale.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
}
public void switchToReparti(javafx.event.ActionEvent event) {
System.out.println("You clicked me!");
}
public void setEnableBtnUnitaLocale() {
btnUnitaLocali.setDisable(false);
System.out.println("setEnableBtnUnitaLocale()");
}
public void setEnableBtnReparti() {
btnReparti.setDisable(false);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<BorderPane xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="View.Controllers.Creazione">
<top>
<Pane prefHeight="48.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" layoutX="181.0" layoutY="16.0" minWidth="34.0" prefHeight="17.0" text=">>" />
<Label alignment="CENTER" layoutX="382.0" layoutY="16.0" text=">>" />
<JFXButton fx:id="btnSocieta" alignment="CENTER" layoutX="53.0" layoutY="12.0" onAction="#switchToSocieta" text="Società / Ente" />
<JFXButton fx:id="btnUnitaLocali" alignment="CENTER" layoutX="255.0" layoutY="12.0" onAction="#switchToUnitaLocali" text="Unità locali" />
<JFXButton fx:id="btnReparti" alignment="CENTER" layoutX="453.0" layoutY="12.0" onAction="#switchToReparti" text="Reparti" />
</children>
</Pane>
</top>
<center>
<StackPane fx:id="stackPane" prefHeight="352.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
此控制器的唯一作用域是调用我们之前通过方法“salvaSocieta”看到的方法“setEnableBtnUnitaLocale”
package View.Controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
public class CreazioneSocieta implements Initializable {
@FXML
JFXButton btnSalva;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void salvaSocieta(javafx.event.ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/fxml/main_creazione.fxml"));
Parent root = (Parent) loader.load();
Creazione controller = loader.getController();
controller.setEnableBtnUnitaLocale();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<Pane xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="View.Controllers.CreazioneSocieta">
<children>
<Label layoutX="51.0" layoutY="139.0" prefHeight="17.0" prefWidth="93.0"
text="Ragione Sociale:" />
<TextField layoutX="153.0" layoutY="135.0" prefHeight="25.0" prefWidth="437.0" />
<Label layoutX="51.0" layoutY="203.0" prefHeight="17.0" prefWidth="56.0" text="Indirizzo:" />
<TextField layoutX="115.0" layoutY="199.0" prefHeight="25.0" prefWidth="330.0" />
<Label layoutX="51.0" layoutY="324.0" prefHeight="17.0" prefWidth="56.0" text="Telefono:" />
<TextField accessibleText="INTEGER" layoutX="115.0" layoutY="320.0" prefHeight="25.0"
prefWidth="330.0" />
<Label layoutX="51.0" layoutY="262.0" prefHeight="17.0" prefWidth="43.0" text="e-mail:" />
<TextField layoutX="115.0" layoutY="258.0" prefHeight="25.0" prefWidth="330.0" />
<Label layoutX="467.0" layoutY="203.0" prefHeight="17.0" prefWidth="56.0" text="Provincia:" />
<TextField accessibleText="INTEGER" layoutX="533.0" layoutY="199.0" prefHeight="25.0"
prefWidth="56.0" />
<JFXButton onAction="#salvaSocieta" contentDisplay="CENTER" defaultButton="true"
graphicTextGap="0.0" layoutX="573.0" layoutY="421.0" prefHeight="45.0" prefWidth="0.0"
ripplerFill="#bfbdbd" textFill="WHITE" textOverrun="CLIP">
<graphic>
<ImageView fitHeight="37.0" fitWidth="37.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icons/save_icon.png" />
</image>
</ImageView>
</graphic>
</JFXButton>
</children>
</Pane>
使用MVC 方法。定义一个
Model
类,其中包含应用程序的状态,并具有观察值并对它们的更改做出响应的机制。您可以使用 JavaFX Properties 轻松完成此操作:
public class Model {
private final BooleanProperty societySaved = new SimpleBooleanProperty();
public BooleanProperty societySavedProperty() {
return societySaved ;
}
public final boolean isSocietySaved() {
return societySavedProperty().get();
}
public final void setSocietySaved(boolean societySaved) {
societySavedProperty().set(societySaved);
}
// similarly for other properties as needed
}
在您的
Creazione
控制器类中,为模型定义一个字段,并将按钮的状态绑定到模型中的状态:
public class Creazione {
@FXML
private JFXButton btnSocieta;
@FXML
private JFXButton btnUnitaLocali;
@FXML
private JFXButton btnReparti;
@FXML
private StackPane stackPane;
private Model model ;
public void setModel(Model model) {
this.model = model ;
btnUnitaLocali.disableProperty().bind(model.societySavedProperty().not());
// similarly for other buttons and properties in the model
}
@Override
public void initialize(URL url, ResourceBundle rb) {
//btnUnitaLocali.setDisable(true);
//btnReparti.setDisable(true);
try {
Parent root = FXMLLoader.load(getClass().getResource("/View/fxml/creazione_societa.fxml"));
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
} catch (IOException e) {
e.printStackTrace();
}
}
// ... (further modifications to methods shown later)
// these are not needed
// public void setEnableBtnUnitaLocale() {
// btnUnitaLocali.setDisable(false);
// System.out.println("setEnableBtnUnitaLocale()");
// }
// public void setEnableBtnReparti() {
// btnReparti.setDisable(false);
// }
}
类似地,在其他控制器中,定义对模型的引用并更新属性。例如:
public class CreazioneSocieta {
private Model model ;
public void setModel(Model model) {
this.model = model;
}
public void salvaSocieta(ActionEvent event) {
model.setSocietySaved(true);
}
}
现在,在您的应用程序代码中,您需要创建一个
Model
实例,并且当您加载初始
view,将模型传递给控制器:
Model model = new Model();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/fxml/main_creazione.fxml"));
Parent root = (Parent) loader.load();
Creazione controller = loader.getController();
controller.setModel(model);
Scene scene = new Scene(root);
stage.setScene(scene);
// etc.
最后,回到您的
Creazione
类中,确保将相同的模型实例传递给其他控制器。例如:
public class Creazione {
// existing code ...
public void switchToSocieta(javafx.event.ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/fxml/creazione_societa.fxml"));
Parent root = loader.load();
CreazioneSocieta controller = loader.getController();
controller.setModel(model);
stackPane.getChildren().removeAll();
stackPane.getChildren().setAll(root);
}
}