我正试图将 JFXDrawer 添加到我的程序中,我已经成功地做到了这一点,但我只有一个问题,我无法弄清楚为什么会发生这种情况。当我的程序首次运行时,JFXDrawer 应该完全隐藏,并在我点击汉堡包按钮时显示,但如果您看下面的图片,红色空间突出显示(我为抽屉添加了背景色,以使其清晰),该空间总是在程序启动时显示,但当我点击汉堡包打开和关闭时,它返回正常隐藏,它只是在启动时。
NavigationDrawerTest
public class NavigationDrawerTest extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
MainController:
public class MainController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML
private AnchorPane anchorPane;
@FXML
private JFXHamburger hamburger;
@FXML
private JFXDrawer drawer;
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
// TODO
VBox box = FXMLLoader.load(getClass().getResource("NavDrawer.fxml"));
drawer.setSidePane(box);
} catch (IOException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
HamburgerSlideCloseTransition task = new HamburgerSlideCloseTransition(hamburger);
task.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, (Event event) -> {
drawer.toggle();
});
drawer.setOnDrawerOpening((event) -> {
task.setRate(task.getRate() * -1);
task.play();
drawer.setMinWidth(220);
});
drawer.setOnDrawerClosed((event) -> {
task.setRate(task.getRate() * -1);
task.play();
drawer.setMinWidth(0);
});
}
}
Main.fxml
<BorderPane maxHeight="790.0" maxWidth="1280.0" prefHeight="790.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="navigationdrawertest.MainController">
<center>
<AnchorPane id="AnchorPane" fx:id="anchorPane" prefHeight="720.0" prefWidth="1280.0">
<children>
<JFXHamburger fx:id="hamburger" layoutX="14.0" layoutY="14.0" />
<JFXTextField fx:id="tf_search" focusColor="#ffc107" layoutX="80.0" layoutY="54.0" prefHeight="40.0" prefWidth="925.0" promptText="SEARCH HERE" unFocusColor="#c5c5c5" AnchorPane.leftAnchor="80.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="54.0">
<font>
<Font name="Exo Regular" size="18.0" />
</font>
</JFXTextField>
<TableView fx:id="personTable" layoutX="80.0" layoutY="132.0" prefHeight="577.0" prefWidth="1040.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="152.0">
<columns>
<TableColumn fx:id="T1Column" editable="false" prefWidth="195.0" sortType="DESCENDING" text="T1" />
<TableColumn fx:id="T2Column" editable="false" minWidth="6.0" prefWidth="220.0" sortType="DESCENDING" text="T2" />
<TableColumn fx:id="T3Column" editable="false" prefWidth="97.0" sortType="DESCENDING" text="T3" />
</columns>
</TableView>
</children>
</AnchorPane>
</center>
<left>
<JFXDrawer fx:id="drawer" defaultDrawerSize="220.0" prefWidth="0.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
NavDrawer.fxml
<VBox style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="navigationdrawertest.NavDrawerController">
<children>
<JFXButton alignment="BASELINE_LEFT" focusTraversable="false" graphicTextGap="15.0" prefHeight="32.0" prefWidth="220.0" ripplerFill="WHITE" text="Button 1" textFill="#c1fff9">
<padding>
<Insets bottom="5.0" left="30.0" top="5.0" />
</padding>
</JFXButton>
<JFXButton alignment="BASELINE_LEFT" focusTraversable="false" graphicTextGap="15.0" layoutX="10.0" layoutY="10.0" prefHeight="32.0" prefWidth="220.0" ripplerFill="WHITE" text="Button 2" textFill="#c1fff9">
<padding>
<Insets bottom="5.0" left="30.0" top="5.0" />
</padding>
</JFXButton>
<JFXButton alignment="BASELINE_LEFT" focusTraversable="false" graphicTextGap="15.0" layoutX="10.0" layoutY="45.0" prefHeight="32.0" prefWidth="220.0" ripplerFill="WHITE" text="Button 3" textFill="#c1fff9">
<padding>
<Insets bottom="5.0" left="30.0" top="5.0" />
</padding>
</JFXButton>
<JFXButton alignment="BASELINE_LEFT" focusTraversable="false" graphicTextGap="15.0" layoutX="10.0" layoutY="80.0" prefHeight="32.0" prefWidth="220.0" ripplerFill="WHITE" text="Button 4" textFill="#c1fff9">
<padding>
<Insets bottom="5.0" left="30.0" top="5.0" />
</padding>
</JFXButton>
<JFXButton alignment="BASELINE_LEFT" focusTraversable="false" graphicTextGap="15.0" layoutX="10.0" layoutY="115.0" prefHeight="32.0" prefWidth="220.0" ripplerFill="WHITE" text="Button 5" textFill="#c1fff9">
<padding>
<Insets bottom="5.0" left="30.0" top="5.0" />
</padding>
</JFXButton>
</children>
</VBox>
NavDrawerController
public class NavDrawerController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我也尝试过使用stackpane和其中的边框窗格,正如我在一些教程中看到的那样,但仍然得到同样的问题。
不幸的是,没有太多的例子,关于使用JFXDrawer,我不知道为什么。
我已经设法解决它感谢,hwowver我不知道这是最好的方式来解决它。如果有人有更好的解决方案,请分享它.所以我只是在MainController中添加了这行代码。
drawer.setMinWidth(0);
所以... MainController 应该是这样的。
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
// TODO
VBox box = FXMLLoader.load(getClass().getResource("NavDrawer.fxml"));
drawer.setSidePane(box);
drawer.setMinWidth(0); // this is the new code added
} catch (IOException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
HamburgerSlideCloseTransition task = new HamburgerSlideCloseTransition(hamburger);
task.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, (Event event) -> {
drawer.toggle();
});
drawer.setOnDrawerOpening((event) -> {
task.setRate(task.getRate() * -1);
task.play();
drawer.setMinWidth(220);
});
drawer.setOnDrawerClosed((event) -> {
task.setRate(task.getRate() * -1);
task.play();
drawer.setMinWidth(0);
});
} }