0

我只是尝试编写一个包含已加载 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);
        }
    }


}

我希望有人可以帮助我并原谅我糟糕的英语技能。

4

1 回答 1

1

这不起作用的原因是您正在创建SlideOut类的新实例。当您调用 时changeContent,您是在该新实例上调用它,而不是在表示所显示应用程序的实例上调用它。

我会通过让您SlideBarContentController公开当前内容的属性来解决此问题。然后,您的应用程序可以观察此属性并做出响应。这也将消除控制器和应用程序类之间的耦合,这是可取的。

控制器看起来像这样:

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{

    String Home = "Home.fxml";
    String Example = "FXMLExampelConntent.fxml";
    Pane dustbin;

    private final StringProperty content = new SimpleStringProperty(this, "content", "");

    public StringProperty contentProperty() {
        return content ;
    }
    public final String getContent() {
        return contentProperty().get();
    }
    public final void setContent(String content) {
        contentProperty().set(content);
    }

     @FXML void loadHomeAction(ActionEvent event) {
        content.set(Home);
    }

     @FXML void loadFXMLConntentExampleAction(ActionEvent event) {
        content.set(Example);
    }


}

现在在您的应用程序类中,您需要访问控制器并观察属性:

    FXMLLoader fxmlMainLoader = new FXMLLoader(getClass().getResource("Home.fxml"));
    try {
        mainView = (Pane) fxmlMainLoader.load();
        SlideBarContentController contentController = (SlideBarContentController) fxmlMainLoader.getController();
        contentController.contentProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
                changeContent(newValue);
            }
        });
    } catch (IOException ex) {
        Logger.getLogger(SlideOut.class.getName()).log(Level.SEVERE, null, ex);
    }

(您将需要添加几个导入。)

于 2014-09-22T15:54:26.927 回答