gpt4 book ai didi

java - 将 ChartPanel 添加到 JPanel

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

我这里有一些不起作用的代码:

    XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.HORIZONTAL, true, true, true);
ChartPanel chartpanel = new ChartPanel(chart);

chartpanel.setDomainZoomable(true);
jPanel4.setLayout(new BorderLayout());
jPanel4.add(chartpanel, BorderLayout.NORTH);

所以问题是带有图表的jPanel4不可见。当我将图表面板添加到框架并使其可见时,它就可以工作了。

有人知道我的错误是什么吗?

最佳答案

这对我来说非常有效:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Main {
public static void main(String[] args) {
XYSeries series = new XYSeries("asdf");
for (int i = 0; i < 100; i++)
series.add(i, Math.random());
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.HORIZONTAL, true, true, true);
ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setDomainZoomable(true);

JPanel jPanel4 = new JPanel();
jPanel4.setLayout(new BorderLayout());
jPanel4.add(chartpanel, BorderLayout.NORTH);

JFrame frame = new JFrame();
frame.add(jPanel4);
frame.pack();
frame.setVisible(true);
}
}

您能为我们提供更多代码吗?您是否在 jPanel4 中添加了其他内容?每个地点不能有多个组件(NORTH、SOUTH、WEST、EAST、CENTER)。您是否将面板放入框架中?

关于java - 将 ChartPanel 添加到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318488/

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