You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除JavaFX中JFreeChart的边框,使图表与窗口齐平?

解决JavaFX中JFreeChart的ChartViewer与面板无法齐平的问题

针对你遇到的ChartViewer始终存在边框/内边距、无法和父面板完全齐平的问题,核心原因是ChartViewer内部默认使用了ScrollPane,而ScrollPane的Viewport自带默认内边距,仅设置ChartViewer本身的padding/insets无法彻底解决。

你可以在创建ChartViewer后,添加以下代码消除内部ScrollPane的额外间距:

// 处理bottomChart
bottomChart.setViewportInsets(Insets.EMPTY);
ScrollPane bottomScrollPane = (ScrollPane) bottomChart.getChildrenUnmodifiable().get(0);
bottomScrollPane.setPadding(Insets.EMPTY);
bottomScrollPane.setBorder(Border.EMPTY);

// 处理topChart同理
topChart.setViewportInsets(Insets.EMPTY);
ScrollPane topScrollPane = (ScrollPane) topChart.getChildrenUnmodifiable().get(0);
topScrollPane.setPadding(Insets.EMPTY);
topScrollPane.setBorder(Border.EMPTY);

另外,可在你的setJFreeChartStyle方法中补充以下设置,彻底消除图表内部可能存在的预留空间:

protected void setJFreeChartStyle(JFreeChart chart) {
    // 保留原有所有设置
    
    // 补充:隐藏轴线条与刻度,彻底清除轴的预留空间
    chart.getXYPlot().getDomainAxis().setAxisLineVisible(false);
    chart.getXYPlot().getRangeAxis().setAxisLineVisible(false);
    chart.getXYPlot().getDomainAxis().setTickMarksVisible(false);
    chart.getXYPlot().getRangeAxis().setTickMarksVisible(false);
    
    // 确保轴的上下/左右边距完全为0
    chart.getXYPlot().getRangeAxis().setLowerMargin(0.0);
    chart.getXYPlot().getRangeAxis().setUpperMargin(0.0);
}

应用以上修改后,ChartViewer就能与StackPane完全齐平,不会再有多余的边框或间距。

内容的提问来源于stack exchange,提问作者ptan9o

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:35:34