JavaFX 2.1 更改 ScrollPane 滚动条大小

2024-02-25

我试图弄清楚如何更改滚动窗格滚动条大小以使其在 javafx 2.1 中更宽。


ScrollBar 宽度基于 ScrollPane 的字体大小。

将 JScrollPane 的字体大小设置为较大的值,并(如果需要)将 JScrollPane 内容节点的字体大小恢复为正常值。

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(content);
scrollPane.setStyle("-fx-font-size: 40px;");  // set the font size to something big.
content.setStyle("-fx-font-size: 11px;");     // reset the region's font size to the default.

这是一个完整的可执行示例,基于我对之前的回答同一主题的论坛问题 https://forums.oracle.com/forums/thread.jspa?messageID=10409262.

import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.*;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Region;

public class BigScrollBars extends Application {
  @Override public void start(Stage stage) {
    // create a chart.
    ObservableList<PieChart.Data> pieChartData =
      FXCollections.observableArrayList(
        new PieChart.Data("Grapefruit", 13),
        new PieChart.Data("Oranges", 25),
        new PieChart.Data("Plums", 10),
        new PieChart.Data("Pears", 22),
        new PieChart.Data("Apples", 30)
      );
    final PieChart chart = new PieChart(pieChartData);
    chart.setTitle("Imported Fruits");
    chart.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
    chart.setPrefSize(800,600);

    // create a scrollpane.
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(chart);
    scrollPane.setStyle("-fx-font-size: 40px;");  // set the font size to something big.
    chart.setStyle("-fx-font-size: 11px;");       // reset the region's font size to the default.

    // show the scene.
    stage.setScene(new Scene(scrollPane, 400, 300));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaFX 2.1 更改 ScrollPane 滚动条大小 的相关文章

随机推荐