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



我正在开发的javafx应用程序最终将在800x480分辨率的手持设备上运行。该应用程序通常以纵向模式运行,但对于某些功能(例如显示图表和表格),它需要切换到横向模式以更好地显示数据。

我的问题是,对于旋转90度倍数的节点,是否有一种直接的操作方法?

我可以通过调用setRotate()来旋转表,尽管这会引入几个新问题:

为了在旋转时调整列宽,用户必须从左到右拖动列分隔符(与标题行正交),该表仍然将其宽度/高度扩展到其父表的大小,尽管在旋转-90度(或其他90的倍数)时效果不佳。

另一个限制是图表内容包含在边框窗格的中心,其中边框窗格的顶部和底部包含工具栏,这会阻止旋转整个场景。

这是我的SSCCE;如果下面的代码有任何问题,请纠正我。


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不会执行旋转功能。必须重新绘制布局才能显示它。使用JavaFX,您可以在场景中添加一个监听器来监听宽度的变化,因为这基本上就是发生的事情。你最有可能这样做:


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

也许不是最好的解决方案,但我想这是有意义的。

最新更新