如何在调整 window 大小时自动调整 JFreeChart 的大小

How to automatically resize JFreeChart when window is resized

我在 ChartPanel 内的 JPanel 内有一张图表 JFrame。我目前有 BoxLayout 用于框架,FlowLayout 用于 JPanel。我想让图表在 window 调整大小时自动调整大小。我已经尝试了我在这里找到的所有解决方案,唯一有效的方法是将 JPanel 布局设置为 GridLayout,但我无法使用它,因为我在 [=13] 中还有其他组件=] 并且它使它们都具有相同的大小(我需要图表比其他图表更大)。

目前我有:

chartPanel.setPreferredSize(new java.awt.Dimension(500, 250));

frame = new JFrame();
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
mainPanel = new JPanel(new FlowLayout());
mainPanel.setPreferredSize(new Dimension(500, 500));
mainPanel.add(chartPanel);

frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

我已经尝试了 here 解决方案,但添加子面板对我没有任何帮助。我不认为我将不得不添加额外的面板来调整图表的大小。我怎样才能只使用 chartPanelJPanelJFrame

the only thing that worked is setting the JPanel layout to GridLayout, but I can't use it, because I have other components in the JPanel and it makes them all the same size (I need the chart to be bigger than the rest).

然后为 JPanel 使用 GridLayout,或者更好,BorderLayout,并将其他组件放在一个新的 JPanel 中,一个使用 FlowLayout 的组件,并添加 that JPanel 到主要的一个。嵌套 JPanel,每个都使用自己的布局来实现最灵活的 GUI 和设计。

I don't think I will have to add extra panels just to make the chart resize.

是的,你知道。

I have the chart and a bunch of buttons (below the chart) that let the user interact with the chart. Obviously, I don't want the buttons to be as big as the chart itself. Currently all buttons and the chartPanel are inside mainPanel.

然后使用 BorderLayout,将图表放在 BorderLayout.CENTER 位置,将按钮放在 BorderLayout.PAGE_END 位置的 JPanel 中,如果需要,可以使用默认的 FlowLayout,或者任何其他布局效果最好。

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.*;
import javax.swing.border.Border;

public class ChartAndButtons extends JPanel {
    private static final long serialVersionUID = 1L;

    public ChartAndButtons() {
        JPanel mockChartPanel = createMockChart();
        int gap = 3;
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, gap, gap));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        String[] buttonNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        for (String buttonName : buttonNames) {
            buttonPanel.add(new JButton(buttonName));
        }

        setLayout(new BorderLayout());
        add(mockChartPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    // ignore this -- it simply creates a JPanel as a placeholder
    // for your real JFreeChart panel   
    private JPanel createMockChart() {
        JLabel label = new JLabel("Mock Chart");
        label.setFont(label.getFont().deriveFont(Font.BOLD, 60f));
        Color color = Color.ORANGE;
        JPanel mockChartPanel = new JPanel(new GridBagLayout());
        mockChartPanel.add(label);
        Border outsideBorder = BorderFactory.createLineBorder(color, 40);
        int eb = 80;
        Border insideBorder = BorderFactory.createEmptyBorder(eb, eb, eb, eb);
        mockChartPanel.setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
        mockChartPanel.setBackground(color.brighter());

        return mockChartPanel;
    }

    private static void createAndShowGui() {
        ChartAndButtons mainPanel = new ChartAndButtons();

        JFrame frame = new JFrame("ChartAndButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}