[英]JavaFX 2.1 Change ScrollPane Scrollbar size
我試圖弄清楚如何更改滾動面板滾動條大小以使其在javafx 2.1中更寬。
ScrollBar的寬度基於ScrollPane的字體大小。
將ScrollPane的字體大小設置為較大,然后(如果需要)將ScrollPane內容節點的字體大小恢復為正常大小。
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.
這是一個完整的可執行示例,基於我對相同主題的先前論壇問題的回答。
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); }
}
以下解決方案為我工作:
@FXML
private ScrollPane myScrollPane;
// Where you need in your code do the following:
Set<Node> nodes = myScrollPane.lookupAll(".scroll-bar");
for (final Node node : nodes) {
if (node instanceof ScrollBar) {
ScrollBar sb = (ScrollBar) node;
if (sb.getOrientation() == Orientation.VERTICAL) { // HORIZONTAL is another option.
sb.setPrefWidth(40); // You can define your preferred width here.
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.