gpt4 book ai didi

java - 如何在调整窗口大小时自动调整 JFreeChart 的大小

转载 作者:行者123 更新时间:2023-12-02 10:17:09 25 4
gpt4 key购买 nike

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

目前我有:

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 中,该 JPanel 使用 FlowLayout,并添加那个 JPanel到主要的一个。 Nest 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,或者任何其他最有效的布局。

例如:

enter image description here

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

关于java - 如何在调整窗口大小时自动调整 JFreeChart 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com