简体   繁体   English

Javafx-旋转节点以在手持设备上以横向/纵向显示

[英]Javafx - Rotate node for display in landscape/portrait orientation on handheld device

The javafx application I am developing will eventually be running on a handheld device with 800x480 resolution. 我正在开发的javafx应用程序最终将在分辨率为800x480的手持设备上运行。 The application will typically be running in portrait mode, but for some features (eg displaying charts and tables), it will need to switch to landscape mode to better display the data. 该应用程序通常将以纵向模式运行,但是对于某些功能(例如,显示图表和表格),将需要切换到横向模式以更好地显示数据。

My question is, is there a straight forward way to operate with nodes that are rotated by multiples of 90 degrees? 我的问题是,是否有直接的方法来操作以90度的倍数旋转的节点?

I can rotate the table by calling setRotate(), although this introduces several new issues: 我可以通过调用setRotate()来旋转表,尽管这引入了几个新问题:

To resize column widths when rotated, the user has to drag the column dividers left to right (orthogonal to the row of headers), The table still expands its width/height to the size of its parent, although this doesn't work as well when rotated -90 degrees (or other multiples of 90). 要在旋转时调整列宽的大小,用户必须将列分隔符从左向右拖动(与标题行垂直),表格仍然将其宽度/高度扩展到其父级的大小,尽管这样做不起作用旋转-90度(或90的其他倍数)时。

The other constraint is that the chart content is contained in the center of a BorderPane, where the top and bottom of the BorderPane contain toolbars, which prevents rotating the entire scene. 另一个约束是,图表内容包含在BorderPane的中心,BorderPane的顶部和底部包含工具栏,这会阻止旋转整个场景。

Here is my SSCCE; 这是我的SSCCE; please correct me if there are any problems with the code below. 如果下面的代码有任何问题,请纠正我。



    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class TablePanelTrial extends Application {

        BorderPane root = new BorderPane();
        private boolean isRotated=false;

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

        @Override
        public void start(final Stage primaryStage) {
            primaryStage.setTitle("Table Panel Trial");

            final TablePanel tp = new TablePanel();

            Button btnRotate = new Button("Rotate");
            btnRotate.setOnAction(new EventHandler() {
                @Override
                public void handle(ActionEvent event) { 
                    double r = -90;        
                    if(isRotated){
                        r=0;
                        isRotated = !isRotated;
                    }
                    else{
                        r=-90;
                        tp.tv.setMinHeight(200);
                        isRotated = !isRotated;
                    }
                    tp.rotate(r);
                }
            });        

            root.setTop(btnRotate);
            root.setCenter(tp.getVB());
            primaryStage.setScene(new Scene(root, 480, 800));
            primaryStage.show();
        }  

        class TablePanel{
            private VBox vbox = new VBox();
            private TableView tv = new TableView();
            private String[] labelVal = {"Column 1", "Element", "Difference", "File Name", "Report Number"};

            public TablePanel(){
                TableColumn column1 = new TableColumn(labelVal[0]);
                TableColumn column2 = new TableColumn(labelVal[1]);
                TableColumn column3 = new TableColumn(labelVal[2]);
                TableColumn column4 = new TableColumn(labelVal[3]);
                TableColumn column5 = new TableColumn(labelVal[4]);
                tv.getColumns().addAll(column1, column2, column3, column4,column5);
                vbox.getChildren().add(tv);
                tv.setPrefHeight(2000);
            }

            public Pane getVB(){
                return vbox;
            }

            public void rotate(double r){
                vbox.setRotate(r);
            }
        }
    }
    

Android does not perform a rotate function when the device goes to landscape mode, I think. 我认为,当设备进入横向模式时,Android不执行旋转功能。 One has to redraw the layout again to display it. 必须重新绘制布局才能显示它。 With JavaFX, what you can do is add a Listener to the scene to listen for a change in width since that's what basically happens. 使用JavaFX,您可以做的是在场景中添加一个侦听器以侦听宽度的变化,因为这基本上就是这种情况。 You can do it like this most probably: 您最有可能这样做:


scene.widthProperty().addListener((observable, oldValue, newValue) -> {
         ....initialize the changes to layout here....
    );
});

May not be the best solution but I guess its something. 可能不是最好的解决方案,但我想它是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM