我只是尝试编写一个包含已加载 fxml 的滑动条。这些 FXML 是我的菜单。使用此菜单,我必须在特殊窗格中加载其他 FXML 文件,而无需使用类似线程的工作背景来请求或生成更新。
我的 SlideOut.java (run):`package slideout;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.web.WebEngine;
/**
* Example of a sidebar that slides in and out of view
*/
public class SlideOut extends Application {
public String mainConntent;
String currentPage;
Pane mainView;
Stage staged;
public void changeConntent(String Conntent){
FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource(Conntent));
try {
mainView = (Pane) fxmlMainLoader.load();
} catch (IOException ex) {
Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex);
}
mainView.setPrefSize(800, 600);
// create a sidebar with some content in it.
final Pane lyricPane = createSidebarContent();
SideBar sidebar = new SideBar(250, lyricPane);
VBox.setVgrow(lyricPane, Priority.ALWAYS);
// layout the scene.
final BorderPane layout = new BorderPane();
Pane mainPane = VBoxBuilder.create().spacing(10)
.children(
sidebar.getControlButton(),
mainView
).build();
layout.setLeft(sidebar);
layout.setCenter(mainPane);
// show the scene
Scene scene = new Scene(layout);
scene.getStylesheets().add(getClass().getResource("slideout.css").toExternalForm());
staged.setScene(scene);
staged.showAndWait();
}
public static void main(String[] args) throws Exception {
launch(args);
}
public void start(final Stage stage){
stage.setTitle("SLideOutExample");
// create a WebView to show to the right of the SideBar.
mainView = new Pane();
FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource("Home.fxml"));
try {
mainView = (Pane) fxmlMainLoader.load();
} catch (IOException ex) {
Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex);
}
mainView.setPrefSize(800, 600);
// create a sidebar with some content in it
final Pane lyricPane = createSidebarContent();
SideBar sidebar = new SideBar(250, lyricPane);
VBox.setVgrow(lyricPane, Priority.ALWAYS);
// layout the scene
final BorderPane layout = new BorderPane();
Pane mainPane = VBoxBuilder.create().spacing(10)
.children(
sidebar.getControlButton(),
mainView
).build();
layout.setLeft(sidebar);
layout.setCenter(mainPane);
// show the scene
Scene scene = new Scene(layout);
scene.getStylesheets().add(getClass().getResource("slideout.css").toExternalForm());
stage.setScene(scene);
stage.show();//showAndWait();? and something to do?
}
private BorderPane createSidebarContent() {// create some content to put in the sidebar.
final BorderPane lyricPane = new BorderPane();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SlideBarConntent.fxml"));
Pane cmdPane = null;
try {
cmdPane = (Pane) fxmlLoader.load();
} catch (IOException ex) {
Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex);
}
try {
lyricPane.setCenter(cmdPane);
} catch (Exception e) {
e.printStackTrace();
}
return lyricPane;
}
/**
* Animates a node on and off screen to the left.
*/
class SideBar extends VBox {
/**
* @return a control button to hide and show the sidebar
*/
public Button getControlButton() {
return controlButton;
}
private final Button controlButton;
/**
* creates a sidebar containing a vertical alignment of the given nodes
*/
SideBar(final double expandedWidth, Node... nodes) {
getStyleClass().add("sidebar");
this.setPrefWidth(expandedWidth);
this.setMinWidth(0);
// create a bar to hide and show.
setAlignment(Pos.CENTER);
getChildren().addAll(nodes);
// create a button to hide and show the sidebar.
controlButton = new Button("Collapse");
controlButton.getStyleClass().add("hide-left");
controlButton.setId("ControlButton");
// apply the animations when the button is pressed
controlButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// create an animation to hide sidebar
final Animation hideSidebar = new Transition() {
{
setCycleDuration(Duration.millis(250));
}
protected void interpolate(double frac) {
final double curWidth = expandedWidth * (1.0 - frac);
setPrefWidth(curWidth);
setTranslateX(-expandedWidth + curWidth);
}
};
hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
setVisible(false);
controlButton.setText("Show");
controlButton.getStyleClass().remove("hide-left");
controlButton.getStyleClass().add("show-right");
}
});
// create an animation to show a sidebar
final Animation showSidebar = new Transition() {
{
setCycleDuration(Duration.millis(250));
}
protected void interpolate(double frac) {
final double curWidth = expandedWidth * frac;
setPrefWidth(curWidth);
setTranslateX(-expandedWidth + curWidth);
}
};
showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
controlButton.setText("Collapse");
controlButton.getStyleClass().add("hide-left");
controlButton.getStyleClass().remove("show-right");
}
});
if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED) {
if (isVisible()) {
hideSidebar.play();
} else {
setVisible(true);
showSidebar.play();
}
}
}
});
}
}
}
`
在这些之后,我在 SlideBarConntent.fxml-Controller 的控制器中输入以下操作事件:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package slideout;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.Pane;
/**
* FXML Controller class
*
* @author tobiasg
*/
public class SlideBarConntentController{
SlideOut mainJava = new SlideOut();
String Home = "Home.fxml";
String Example = "FXMLExampelConntent.fxml";
Pane dustbin;
@FXML void loadHomeAction(ActionEvent event) {
try {
mainJava.changeConntent(Home);
} catch (Exception ex) {
Logger.getLogger(SlideBarConntentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML void loadFXMLConntentExampleAction(ActionEvent event) {
try {
mainJava.changeConntent(Home);
} catch (Exception ex) {
Logger.getLogger(SlideBarConntentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我希望有人可以帮助我并原谅我糟糕的英语技能。