gpt4 book ai didi

java - 如何在 JFrame JLabel 中显示 JFreeChart

转载 作者:行者123 更新时间:2023-12-04 06:43:29 28 4
gpt4 key购买 nike

我花了一天的大部分时间阅读“JFreeChart 类库 v1.0.13 开发人员指南”和相关示例。
我相信我知道如何创建图表,只是不清楚如何在 JFrame JLabel 中显示它。
当我尝试这个时

LineChartDemo1 chart = new LineChartDemo1("Chart title");
jLabelChart.add(chart);
chart.setVisible(true);

我有

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container



我看到我正在尝试的是非法的,但是我应该使用什么方法来显示图表?我不应该使用 JLabel 吗?
/**
* A simple demonstration application showing how to create a line chart using
* data from a {@link CategoryDataset}.
*/
public class LineChartDemo1 extends ApplicationFrame {

/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}

/**
* Creates a sample dataset.
*
* @return The dataset.
*/
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(212, "Classes", "JDK 1.0");
dataset.addValue(504, "Classes", "JDK 1.1");
dataset.addValue(1520, "Classes", "JDK 1.2");
dataset.addValue(1842, "Classes", "JDK 1.3");
dataset.addValue(2991, "Classes", "JDK 1.4");
dataset.addValue(3500, "Classes", "JDK 1.5");
return dataset;
}

/**
* Creates a sample chart.
*
* @param dataset a dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {

// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"Java Standard Class Library", // chart title
null, // domain axis label
"Class Count", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);

chart.addSubtitle(new TextTitle("Number of Classes By Release"));
TextTitle source = new TextTitle(
"Source: Java In A Nutshell (5th Edition) "
+ "by David Flanagan (O'Reilly)");
source.setFont(new Font("SansSerif", Font.PLAIN, 10));
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(false);
URL imageURL = LineChartDemo1.class.getClassLoader().getResource(
"OnBridge11small.png");
if (imageURL != null) {
ImageIcon temp = new ImageIcon(imageURL);
// use ImageIcon because it waits for the image to load...
chart.setBackgroundImage(temp.getImage());
plot.setBackgroundPaint(null);
}
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

ChartUtilities.applyCurrentTheme(chart);

// customise the renderer...
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setSeriesStroke(0, new BasicStroke(3.0f));
renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
return chart;
}

/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
LineChartDemo1 demo = new LineChartDemo1(
"JFreeChart: LineChartDemo1.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}

}

最佳答案

您的示例代码运行良好,但我认为放置您的 ChartPanel 没有任何意义。 ( JPanel 的子类)在 JLabel 中.请注意 LineChartDemo1 extends ApplicationFrame ( JFrame 的子类),它继承了后者的默认值 BorderLayout .您可以将图表添加到中心并相应地放置其他组件。

public LineChartDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new Dimension(500, 270));
this.add(chartPanel, BorderLayout.CENTER);
this.add(new JLabel("Mouse wheel!", JLabel.CENTER), BorderLayout.SOUTH);
}

这是一个相关的 demo ,不要忽视 org.jfree.chart.demo 中的例子.为方便起见,每个 API 文档都链接到相应的源代码。

关于java - 如何在 JFrame JLabel 中显示 JFreeChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3963420/

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