简体   繁体   中英

How to automatically resize JFreeChart when window is resized

I have a chart inside a ChartPanel inside a JPanel inside a JFrame . I currently have BoxLayou t for the frame and FlowLayout for the JPanel . I want to get the chart to resize automatically when the window is resized. I've tried all solutions I found here, and 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).

Currently I have:

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);

I've tried the solution here but adding a subpanel did nothing for me. I don't think I will have to add extra panels just to make the chart resize. How can I do it with just the chartPanel , JPanel and the JFrame ?

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).

Then use GridLayout, or perhaps better, BorderLayout for the JPanel and place the other components within a new JPanel, one that uses, say FlowLayout, and add that JPanel to the main one. Nest JPanels, each using their own layouts to effect the most flexible GUI and design.

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

Yes, you do.

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.

Then use BorderLayout, place the chart in the BorderLayout.CENTER position and the buttons into a JPanel that is in the BorderLayout.PAGE_END position, one that uses default FlowLayout if desired, or any other layout that works best.

For example:

在此处输入图片说明

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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM