将 JScrollPane 添加到 JFreeChart_Facing 问题

问题描述 投票:0回答:1

我在将 JScrollPane 添加到我的 LineChart 时遇到问题,我无法完整的项目,因为它是一个很大的代码。这是我的项目中的代码片段。我能够生成图表,但底部滚动条和垂直滚动条不出现。我为此绞尽脑汁了几个小时。

private void generateChart(String chartType, String xAxis, String yAxis) {
            JFreeChart chart = null;
    
            if ("Bar Chart".equals(chartType)) {
                chart = createBarChart(xAxis, yAxis);
            } else if ("Pie Chart".equals(chartType)) {
                chart = createPieChart(xAxis, yAxis);
            } else if ("Line Graph".equals(chartType)) {
                chart = createLineChart(xAxis, yAxis);
            }
    
            if (chart != null) {
                JFrame chartFrame = new JFrame("Chart");
    
                chartFrame.setSize(800, 600);
                chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // Create chart panel with scroll
                
              
    
                ChartPanel chartPanel = createChartPanelWithScroll(chart);
    
               
                chartFrame.add(chartPanel);
                chartFrame.setVisible(true);
            }
        }
        
     // Method to create scrollable and zoomable ChartPanel
        public ChartPanel createChartPanelWithScroll(JFreeChart chart) {
            // Create the ChartPanel with the chart
            ChartPanel chartPanel = new ChartPanel(chart);
            
            // Set preferred size larger than the visible area to ensure scrollbars appear
           // chartPanel.setPreferredSize(new java.awt.Dimension(2000, 1200));  // Force a larger size
    
            // Enable mouse zooming and panning
            chartPanel.setMouseWheelEnabled(true);  // Enable zooming with mouse wheel
            chartPanel.setDomainZoomable(true);     // Allow zooming on X-axis
            chartPanel.setRangeZoomable(true);   
            // Allow zooming on Y-axis
    
        
    
            // Wrap the ChartPanel with a JScrollPane for scrollable bars
            JScrollPane scrollPane = new JScrollPane(chartPanel, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
            // Ensure that the JScrollPane size is set to occupy the window space
            scrollPane.setPreferredSize(new java.awt.Dimension(800, 600));
    
            // Create a panel that contains the scrollPane (with the chartPanel inside)
            JPanel chartWithScrollPanel = new JPanel(new BorderLayout());
            chartWithScrollPanel.add(scrollPane, BorderLayout.CENTER);
    
            return chartPanel;
        }

//由 Habeeb E Sadeed 开发

java swing javafx
1个回答
0
投票

更正方法:

private voidgenerateChart(字符串chartType,字符串x轴,字符串y轴){ JFreeChart 图表 = null;

    // Generate the chart based on the selected type
    if ("Bar Chart".equals(chartType)) {
        chart = createBarChart(xAxis, yAxis);
    } else if ("Pie Chart".equals(chartType)) {
        chart = createPieChart(xAxis, yAxis);
    } else if ("Line Graph".equals(chartType)) {
        chart = createLineChart(xAxis, yAxis);
    }

    if (chart != null) {
        JFrame chartFrame = new JFrame("Chart");

        // Set the size of the frame
        chartFrame.setSize(1200, 800);
        chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // Create the scrollable chart panel
        JScrollPane scrollPane = createChartPanelWithScroll(chart);

        // Add the scrollPane to the frame
        chartFrame.add(scrollPane, BorderLayout.CENTER);

        // Set the frame visibility
        chartFrame.setVisible(true);
    }
}

// Method to create scrollable and zoomable ChartPanel
public JScrollPane createChartPanelWithScroll(JFreeChart chart) {
    // Create the ChartPanel with the chart
    ChartPanel chartPanel = new ChartPanel(chart);

    // Set the preferred size of the ChartPanel larger to trigger scrollbars
    chartPanel.setPreferredSize(new Dimension(1600, 1200));  // Set a larger preferred size

    // Enable mouse zooming and panning
    chartPanel.setMouseWheelEnabled(true);  // Enable zooming with mouse wheel
    chartPanel.setDomainZoomable(true);     // Allow zooming on X-axis
    chartPanel.setRangeZoomable(true);      // Allow zooming on Y-axis

    // Wrap the ChartPanel with a JScrollPane for scrollable bars
    JScrollPane scrollPane = new JScrollPane(chartPanel,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    return scrollPane;  // Return the scrollable pane instead of ChartPanel directly
}
© www.soinside.com 2019 - 2024. All rights reserved.