0

我试图在 JFrame 中对齐整个图表,但我知道的正常组件代码不适用于 JChartPanels。最初我尝试在 JChartPanel 上设置 AligmentX 和 Y 但这没有用。然后我尝试将 JChartPanel 添加到 JPanel 然后 setAlignment 但是一旦我将 JChartPanel 添加到 JPanel 图形不再可见。

下面的代码在默认左上角位置的 JFrame 中创建一个图形。我需要对齐图表 - 使用任何值,因为我稍后会更改它们以适应我的目的。

包含的代码没有上述任何尝试(错误):

import java.awt.*;
import javax.swing.*;
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.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

public ChartTest() {

    XYSeries Goals = new XYSeries("Goals Scored");
    Goals.add(1, 1.0);
    Goals.add(2, 3.0);
    Goals.add(3, 2.0);
    Goals.add(4, 0.0);
    Goals.add(5, 3.0);
    XYDataset xyDataset = new XYSeriesCollection(Goals);

    JFreeChart chart = ChartFactory.createXYLineChart("Goals Scored Over Time", "Fixture Number", "Goals", xyDataset, PlotOrientation.VERTICAL, true, true, false);

    JPanel jPanel = new JPanel();
    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
    jPanel.setVisible(true);
    jPanel.setSize(300, 300);

    ChartPanel CP = new ChartPanel(chart) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    CP.setMouseWheelEnabled(true);
    add(CP);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    initComponents();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ChartTest().setVisible(true);
        }
    });
}
4

1 回答 1

2

The result is just an empty window.

I suspect that your GUI editor's implementation of initComponents() is at fault. You'll have to examine the generated code to see. Here's a complete working example form for reference:

image

Minimal, Complete, Tested and Readable Example:

import java.awt.Dimension;
import javax.swing.BoxLayout;
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.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

    public ChartTest() {

        XYSeries Goals = new XYSeries("Goals Scored");
        Goals.add(1, 1.0);
        Goals.add(2, 3.0);
        Goals.add(3, 2.0);
        Goals.add(4, 0.0);
        Goals.add(5, 3.0);
        XYDataset xyDataset = new XYSeriesCollection(Goals);

        JFreeChart chart = ChartFactory.createXYLineChart(
            "Goals Scored Over Time", "Fixture Number", "Goals",
            xyDataset, PlotOrientation.VERTICAL, true, true, false);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
        jPanel.setVisible(true);
        jPanel.setSize(300, 300);

        ChartPanel CP = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        CP.setMouseWheelEnabled(true);
        add(CP);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChartTest().setVisible(true);
            }
        });
    }
}
于 2014-02-16T20:15:42.577 回答