【问题标题】:JFreeChart doesen't work and block my codeJFreeChart 不起作用并阻止我的代码
【发布时间】:2019-01-06 08:52:24
【问题描述】:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class JfreeChartPie {

    System.out.println("piechart");

    public static void main(String[] args) {

        System.out.println("piechart");
        // TODO Auto-generated method stub
        // create a dataset...
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Category 1", 43.2);
        dataset.setValue("Category 2", 27.9);
        dataset.setValue("Category 3", 79.5);
        // create a chart...
        JFreeChart chart = ChartFactory.createPieChart(
        "Sample Pie Chart",
        dataset,
        true,
        // legend?
        true,
        // tooltips?
        false
        // URLs?
        );
        // create and display a frame...
        ChartFrame frame = new ChartFrame("First", chart);
        //frame.pack();
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

我运行 debian 9 gnu linux 和 eclipse 2018-09 我从控制台得到的只是关于 jfreechart 许可证的文本,它似乎没有执行代码。我没有问题。

【问题讨论】:

  • 该代码甚至无法编译。请edit您的问题提供minimal reproducible example来证明您的问题。
  • @Joe C 这是完整的代码,我编译了它。我不知道你想要什么,我阅读了你的建议,但我认为我的帖子还可以。谢谢
  • 除了对println() 的第一次(错位)调用外,您的示例在我的平台上运行。另请参阅 Initial Threads,这可能对您的平台很重要。

标签: java eclipse jfreechart


【解决方案1】:

为避免此类异常,请确认您在event dispatch thread构造和操作 Swing GUI 对象。正如here 所述,“程序可能看似正常工作,但在不同的环境中却神秘地失败了。”作为参考,我已经相应地修改了您的示例。

$ javac -cp .:$JFC JFreeChartPie.java && "$JRE" -cp .:$JFC JFreeChartPie
import java.awt.EventQueue;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartPie {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("piechart");
                DefaultPieDataset dataset = new DefaultPieDataset();
                dataset.setValue("Category 1", 43.2);
                dataset.setValue("Category 2", 27.9);
                dataset.setValue("Category 3", 79.5);
                JFreeChart chart = ChartFactory.createPieChart(
                    "Sample Pie Chart", dataset, true, true, false);
                ChartFrame frame = new ChartFrame("Title", chart);
                frame.pack();
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

【讨论】:

  • 非常感谢。我解决了,eclipse启动了另一个应用程序。抱歉
  • 很高兴你把它整理好了,但要注意潜在的同步问题。更多内容。
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 2014-06-20
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多