【问题标题】:JavaFx, how to add menuBar and drawingPaneJavaFx,如何添加 menuBar 和 drawingPane
【发布时间】:2015-10-26 15:27:45
【问题描述】:

我正忙于一些演示,在滚动窗口中绘制一些线条。到目前为止一切顺利,但现在可以在 menuBar 上画线,这当然是不可能的。请参阅下面的代码。请帮忙!

会发生这样的事情:

See output here

错误代码:

package Example12a;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Example12a extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Line curLine;

    @Override
    public void start(Stage stage) throws Exception {
        Pane drawingPane = new Pane();
        BorderPane theBorderPane = new BorderPane();

        drawingPane.setPrefSize(800, 800);
        drawingPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

        MenuBar menuBar = new MenuBar();
        // --- Menu File
        Menu menuFile = new Menu("File");
        MenuItem add = new MenuItem("Save");
        add.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                System.out.println("Save");
            }
        }); 
        menuFile.getItems().addAll(add);
        //yOffset = (int)menuBar.getHeight();        
        Menu menuEdit = new Menu("Edit");
        Menu menuView = new Menu("View");
        menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
        theBorderPane.setTop(menuBar);

        ScrollPane scrollPane = new ScrollPane(theBorderPane);
        scrollPane.setPrefSize(300, 300);
        scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);
        scrollPane.setStyle("-fx-focus-color: transparent;");

        theBorderPane.setOnMousePressed(event -> {
            if (!event.isPrimaryButtonDown()) {
                return;
            }

            curLine = new Line(
                event.getX(), event.getY(), 
                event.getX(), event.getY()
            );
            theBorderPane.getChildren().add(curLine);
        });

        theBorderPane.setOnMouseDragged(event -> {
            if (!event.isPrimaryButtonDown()) {
                return;
            }

            if (curLine == null) {
                return;
            }

            curLine.setEndX(event.getX());
            curLine.setEndY(event.getY());

            double mx = Math.max(curLine.getStartX(), curLine.getEndX());
            double my = Math.max(curLine.getStartY(), curLine.getEndY());

            if (mx > theBorderPane.getMinWidth()) {
                theBorderPane.setMinWidth(mx);
            }

            if (my > theBorderPane.getMinHeight()) {
                theBorderPane.setMinHeight(my);
            }
        });

        theBorderPane.setOnMouseReleased(event -> curLine = null);

        theBorderPane.setCenter(drawingPane);
        Scene scene = new Scene(scrollPane);
        stage.setMinWidth(100);
        stage.setMinHeight(100);
        stage.setScene(scene);

        stage.show();
    }
}

【问题讨论】:

    标签: scroll javafx menubar pane borderpane


    【解决方案1】:

    修正了你的布局。

    我所做的是:

    BorderPane 现在是您的根窗格。

    ScrollPane 是 BorderPane 的中心,其内容是 drawingPane

    MenuBar 仍然是 BorderPane 的顶部。

    我还将鼠标事件从borderPane 更改为drawingPane,并将线条添加到drawingPane 而不是borderPane。

    所以它工作正常。

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.MenuItem;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Line;
    import javafx.stage.Stage;
    
    public class Example12a extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        private Line curLine;
    
        @Override
        public void start(Stage stage) throws Exception {
            Pane drawingPane = new Pane();
            BorderPane theBorderPane = new BorderPane();
    
            drawingPane.setPrefSize(800, 800);
            drawingPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    
            MenuBar menuBar = new MenuBar();
            // --- Menu File
            Menu menuFile = new Menu("File");
            MenuItem add = new MenuItem("Save");
            add.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent t) {
                    System.out.println("Save");
                }
            }); 
            menuFile.getItems().addAll(add);
            //yOffset = (int)menuBar.getHeight();        
            Menu menuEdit = new Menu("Edit");
            Menu menuView = new Menu("View");
            menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
            theBorderPane.setTop(menuBar);
    
            ScrollPane scrollPane = new ScrollPane(drawingPane);
            scrollPane.setPrefSize(300, 300);
            scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            scrollPane.setFitToWidth(true);
            scrollPane.setFitToHeight(true);
            scrollPane.setStyle("-fx-focus-color: transparent;");
    
            drawingPane.setOnMousePressed(event -> {
                if (!event.isPrimaryButtonDown()) {
                    return;
                }
    
                curLine = new Line(
                    event.getX(), event.getY(), 
                    event.getX(), event.getY()
                );
                drawingPane.getChildren().add(curLine);
            });
    
            drawingPane.setOnMouseDragged(event -> {
                if (!event.isPrimaryButtonDown()) {
                    return;
                }
    
                if (curLine == null) {
                    return;
                }
    
                curLine.setEndX(event.getX());
                curLine.setEndY(event.getY());
    
                double mx = Math.max(curLine.getStartX(), curLine.getEndX());
                double my = Math.max(curLine.getStartY(), curLine.getEndY());
    
                if (mx > drawingPane.getMinWidth()) {
                    drawingPane.setMinWidth(mx);
                }
    
                if (my > drawingPane.getMinHeight()) {
                    drawingPane.setMinHeight(my);
                }
            });
    
            theBorderPane.setOnMouseReleased(event -> curLine = null);
    
            theBorderPane.setCenter(scrollPane);
            Scene scene = new Scene(theBorderPane);
            stage.setMinWidth(100);
            stage.setMinHeight(100);
            stage.setScene(scene);
    
            stage.show();
        }
    }
    

    注意:

    如果您尝试制作绘图程序,我会优先在 Canvas 中渲染所有线条,而不是使用 Line 类。画布有很多行,速度要快得多。

    【讨论】:

      猜你喜欢
      • 2016-06-30
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2014-10-13
      • 2022-10-22
      • 2013-04-27
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多