【问题标题】:JavaFX : Draw on Center Pane based on user input in Left PaneJavaFX:根据左窗格中的用户输入在中心窗格上绘制
【发布时间】:2014-03-27 00:05:39
【问题描述】:

这是我的摘要代码

public class KlArgon extends Application {

    BorderPane border ;
    Scene scene ;
    Stage stage;

    @Override
    public void start(Stage stage) {
       :
       border = new BorderPane();
       : 
       HBox infoBox = addInfoHBox();
       border.setTop(infoBox);
       :
       VBox menuBox = addMenuVBox();
       border.setLeft(menuBox);
       :
       border.setCenter(addAnchorPane(addGridPane()));
       // setRight and setBottom is not used

       :
        scene = new Scene (border);
        stage.setScene(scene);
        stage.show();
    }

    private Node addAnchorPane(GridPane grid) {
         AnchorPane anchorpane = new AnchorPane();
         anchorpane.getChildren().add(grid);
         AnchorPane.setTopAnchor(grid, 10.0);
         return anchorpane;   
    }

    private GridPane addGridPane() {
         GridPane grid = new GridPane();
         grid.setHgap(10);
         grid.setVgap(10);
         grid.setPadding(new Insets(0, 10, 0, 10));
         grid.add(addWhiteboard(), 1, 0); 
         return grid;
    }

    private Node addWhiteboard() {
         Canvas canvas = new Canvas (wboardWd, wdboardHt);
         GraphicsContext gc = canvas.getGraphicsContext2D();
         drawShapes(gc);
         drawfromClipboard(gc);
         return canvas;
    }

}

我将中心窗格称为“白板”。除此之外,我在 menuBox - btnCopyFromClipboardbtnClearWhiteboard 中有两个按钮。

当用户按下btnCopyFromClipboard 时 - 用户应该只能在“白板”中绘制一个矩形(即仅中心窗格),然后剪贴板图像将被复制(缩放)到该矩形中。

所以我将borderscenestage 设置为全局变量,我正在努力让它发挥作用——不仅是错误的/丑陋的——对我来说它看起来像一个 hack。有没有更简洁的方法来做到这一点,即按下左窗格中的按钮时管理中心窗格? 基本上我希望中心窗格成为Canvas,并且在按下左窗格中的按钮时执行GraphicsContext 操作。

我的工作是按下btnCopyFromClipboard 让我可以在任何地方/任何地方绘制矩形(而不是将其限制在中心窗格/白板上)。我想限制仅在中心窗格/白板内绘制矩形。

非常感谢来自经历过这一切的人的一些输入/指针。

http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm 帮助我入门。

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    我也在修复中,这是我在 Oracle 论坛上提出的一个问题,James 给了我一个生动的回答。请看这里,它有你的答案

    https://community.oracle.com/thread/2598756

    【讨论】:

    • 谢谢 - 我会去阅读 FXML 以更好地理解控制器的概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多