java - JInternalFrame 中的 JFreechart 异常

标签 java jfreechart

基本上,我想要一个具有多个框架的 Java GUI,因此我使用 JInternalFrame,但是当我将图表(从 JFreeChart 创建)添加到其中一个框架时,框架,它给了我一个异常(exception)。

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.plaf.ColorUIResource cannot be cast to java.util.List
at javax.swing.plaf.metal.MetalUtils.drawGradient(Unknown Source)
at javax.swing.plaf.metal.MetalInternalFrameTitlePane.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source) ....

这是代码:

public class immobile extends JFrame {

    JDesktopPane desktop;

    public immobile() {
        desktop = new JDesktopPane();
        desktop.setDesktopManager(new No1DragDesktopManager());
        getContentPane().add(desktop);

        desktop.add(createInternalFrame(30, 50, Color.WHITE));
        desktop.add(createInternalFrame(30, 360, Color.WHITE));
        desktop.add(createInternalFrame(630, 50, Color.WHITE));
        desktop.add(createInternalFrame(630, 360, Color.WHITE));
    }

    private JInternalFrame createInternalFrame(
            int location1, int location2, Color background) {
        JInternalFrame internal =
            new JInternalFrame("Frame" + location1, true, true, true, true);
        internal.setBackground(background);
        internal.setVisible(true);
        internal.setResizable(false);
        internal.setBounds(location1, location2, 600, 310);
        internal.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        return internal;
    }

    public static void main(String args[]) {
        immobile frame = new immobile();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(1280, 720);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        try {
            JInternalFrame[] frames = frame.desktop.getAllFrames();
            JInternalFrame f = frames[0];

            String url = "http://www.cophieu68.com/export/excel.php?id=ABT";
            //create the chart from JFreechart//
            JFreeChart chart = garch_project.garch_chart(url);

            JPanel chartPanel = new ChartPanel(chart);
            f.add(chartPanel);
            f.putClientProperty("dragMode", "fixed");
            JInternalFrame f3 = frames[2];
            f3.putClientProperty("dragMode", "fixed");
            JInternalFrame f4 = frames[1];
            f4.putClientProperty("dragMode", "fixed");
            JInternalFrame f2 = frames[3];
            f2.putClientProperty("dragMode", "fixed");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class No1DragDesktopManager extends DefaultDesktopManager {

        public void beginDraggingFrame(JComponent f) {
            if (!"fixed".equals(f.getClientProperty("dragMode"))) {
                super.beginDraggingFrame(f);
            }
        }

        public void dragFrame(JComponent f, int newX, int newY) {
            if (!"fixed".equals(f.getClientProperty("dragMode"))) {
                super.dragFrame(f, newX, newY);
            }
        }

        public void endDraggingFrame(JComponent f) {
            if (!"fixed".equals(f.getClientProperty("dragMode"))) {
                super.endDraggingFrame(f);
            }
        }
    }
}

我该如何处理它们?谢谢。 (我使用的是 Eclipse,最新版本)。

最佳答案

这是一个简化的 sscce显示了如何将 JFreeChartJInternalFrame 一起使用。请注意,在使其可见之前,您应该 pack() (以及可选的大小)内部框架。相关示例可以参见 here .

enter image description here

/** @see https://stackoverflow.com/questions/9338466 */
public class InternalFreeChart {

    private static final Random rnd = new Random();

    public InternalFreeChart() {
        JFrame frame = new JFrame();
        JDesktopPane desktop = new JDesktopPane();
        frame.add(desktop);
        for (int i = 1; i < 9; i++) {
            desktop.add(createInternalFrame(i));
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JInternalFrame createInternalFrame(int n) {
        JInternalFrame jif = new JInternalFrame(
            "Frame" + n, true, true, true, true);
        jif.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Test", "Time", "Value", createDataset(), true, true, false);
        JPanel chartPanel = new ChartPanel(chart);
        jif.add(chartPanel);
        jif.pack();
        jif.setBounds(n * 25, n * 20, 400, 300);
        jif.setVisible(true);
        return jif;
    }

    private static XYDataset createDataset() {
        TimeSeries series1 = new TimeSeries("Series 1");
        TimeSeries series2 = new TimeSeries("Series 2");
        SerialDate sd = SerialDate.createInstance(new Date());
        for (int i = 1; i < 16; i++) {
            Day d = new Day(SerialDate.addDays(i, sd));
            series1.add(d, 100 + rnd.nextGaussian() / 2);
            series2.add(d, 101 + rnd.nextGaussian() / 2);
        }
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        return dataset;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                InternalFreeChart ifc = new InternalFreeChart();
            }
        });
    }
}

关于java - JInternalFrame 中的 JFreechart 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338466/

相关文章:

java - 将字符串发送到服务器,不带反斜杠

java - 如何在 JFreeChart 中实现居中的 FlowArrangement?

java - jfree 图表示例教程

java - JFreeChart - 标记未显示

java - 从压缩文本文件中获取文本行的阅读器

java - 无法从 Eclipse 将默认 Java libGDX 项目导出为 jar

java - xstream 在从 xml 转换为 Java 对象时正在将 & 转换为 &

java - 如何使用 JFreeChart 固定自动范围避免负值

java - jfreechart setbackgroundpaint 不做任何事情

java - MultiAutoCompleteTextView 自动将之前输入的字符添加到下一个选择中