【问题标题】:Can someone please provide step-by-step instructions for using jfreechart in an IntelliJ project有人可以提供在 IntelliJ 项目中使用 jfreechart 的分步说明吗
【发布时间】:2014-09-24 10:25:29
【问题描述】:

我无法让 JFreeChart 在 IntelliJ 中工作。

到目前为止我做了什么:

  • 使用 Win 7、IntelliJ 13.1.4 和 Java 1.7
  • 我对 IntelliJ 比较陌生
  • 在 IntelliJ 中启动了一个新项目,该项目创建了默认目录 root\out 和 root\src
  • 添加了我自己的目录 root\lib
  • 将外部库作为 .jar 和 .zip 文件(jcommon-1.0.23.zip、jfreechart-1.0.19.zip、junit-4.7.jar)放入其中
  • 在 IntelliJ 中,打开“项目结构”对话框 (Ctrl+Alt+Shift+S)
  • 在左侧面板中,选择“模块”
  • 在中间面板中选择了这个项目(那里唯一的东西)
  • 在右侧面板的“Sources”选项卡上,选择“src”目录
  • 切换到“依赖项”标签
  • 点击右侧的绿色“+”按钮
  • 已选择“2 库...> Java”
  • 导航到 root\lib 并选择 .zip / .jar 文件之一
  • 会弹出“检测到的根”对话框 - 我刚刚单击了确定
  • 弹出“配置库”对话框 - 我给库起了一个合理的名称,然后单击“确定”
  • 库出现在“依赖项”选项卡中,旁边有一个符号,看起来像一些书,并且与项目工具窗口中“外部库”节点旁边的符号相同(所以我猜这是库符号)
  • 对其他两个库重复此操作
  • 在项目的源文件中,我尝试键入“import org.jfree.*;”但是在键入“jfree”位时是红色的,当我在行尾按 Enter 时,该行消失了。
  • 我也试过键入“JFreeChart jFreeChart = new JFreeChart();”但是 JFreeChart 是红色的,当我点击它时,我没有选择导入;只创建类、接口、枚举等。
  • 顺便说一下,运行 jUnit 测试工作正常
  • 顺便提一下,项目工具窗口中的“外部库”节点确实显示 (即 JDK),但不显示 jUnit、jCommon 或 jFreeChart。

我见过类似的问题,答案似乎表明我所做的是正确的。我还检查了 IntelliJ 文档,这也表明我所做的是正确的。但是,它显然不适合我。也许我误解了什么。

有人可以帮我让 JFreeChart 工作吗?

谢谢

编辑:jUnit 可以工作,但现在不行。哦!

【问题讨论】:

    标签: intellij-idea jfreechart


    【解决方案1】:

    我以前从未使用过 IntelliJ,但我下载了它并立即创建了一个名为 JFreeChartExample File -> New Project 的新项目。接下来我单击File -> Project Structure... 并选择Libraries 条目,然后单击+ 以添加jcommon-1.0.23.jarjfreechart-1.0.19.jar(我在解压缩JFreeChart 发行版后在本地文件系统上拥有)。然后在src/ 目录中,我创建了一个新的Java 文件BarChartDemo.java(见下文)。现在我的项目是这样的:

    BarChartDemo1.java如下:

    /* ==================
     * BarChartDemo1.java
     * ==================
     *
     * Copyright (c) 2005-2014, Object Refinery Limited.
     * All rights reserved.
     *
     * http://www.jfree.org/jfreechart/index.html
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions are met:
     *   - Redistributions of source code must retain the above copyright
     *     notice, this list of conditions and the following disclaimer.
     *   - Redistributions in binary form must reproduce the above copyright
     *     notice, this list of conditions and the following disclaimer in the
     *     documentation and/or other materials provided with the distribution.
     *   - Neither the name of the Object Refinery Limited nor the
     *     names of its contributors may be used to endorse or promote products
     *     derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
     * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
     * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     * Original Author:  David Gilbert (for Object Refinery Limited);
     * Contributor(s):   -;
     *
     * Changes
     * -------
     * 09-Mar-2005 : Version 1 (DG);
     * 11-Mar-2014 : Use new ChartFactory method (DG);
     * 25-Jun-2014 : Update to use real data (DG);
     * 
     */
    
    import java.awt.Color;
    import java.awt.Dimension;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.StandardChartTheme;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.block.BlockBorder;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.renderer.category.BarRenderer;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    /**
     * A simple demonstration application showing how to create a bar chart.
     */
    public class BarChartDemo extends ApplicationFrame {
    
        private static final long serialVersionUID = 1L;
    
        static {
            // set a theme using the new shadow generator feature available in
            // 1.0.14 - for backwards compatibility it is not enabled by default
            ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
                    true));
        }
    
        /**
         * Creates a new demo instance.
         *
         * @param title  the frame title.
         */
        public BarChartDemo(String title) {
            super(title);
            CategoryDataset dataset = createDataset();
            JFreeChart chart = createChart(dataset);
            ChartPanel chartPanel = new ChartPanel(chart, false);
            chartPanel.setBackground(null);
            chartPanel.setFillZoomRectangle(true);
            chartPanel.setMouseWheelEnabled(true);
            chartPanel.setDismissDelay(Integer.MAX_VALUE);
            chartPanel.setPreferredSize(new Dimension(500, 270));
            setContentPane(chartPanel);
        }
    
        /**
         * Returns a sample dataset.
         *
         * @return The dataset.
         */
        private static CategoryDataset createDataset() {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.addValue(7445, "JFreeSVG", "Warm-up");
            dataset.addValue(24448, "Batik", "Warm-up");
            dataset.addValue(4297, "JFreeSVG", "Test");
            dataset.addValue(21022, "Batik", "Test");
            return dataset;
        }
    
        /**
         * Creates a sample chart.
         *
         * @param dataset  the dataset.
         *
         * @return The chart.
         */
        private static JFreeChart createChart(CategoryDataset dataset) {
            JFreeChart chart = ChartFactory.createBarChart(
                    "Performance: JFreeSVG vs Batik", null /* x-axis label*/,
                    "Milliseconds" /* y-axis label */, dataset);
            chart.addSubtitle(new TextTitle("Time to generate 1000 charts in SVG "
                    + "format (lower bars = better performance)"));
            chart.setBackgroundPaint(null);
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setBackgroundPaint(null);
    
            // ******************************************************************
            //  More than 150 demo applications are included with the JFreeChart
            //  Developer Guide...for more information, see:
            //
            //  >   http://www.object-refinery.com/jfreechart/guide.html
            //
            // ******************************************************************
    
            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            BarRenderer renderer = (BarRenderer) plot.getRenderer();
            renderer.setDrawBarOutline(false);
            chart.getLegend().setFrame(BlockBorder.NONE);
            return chart;
        }
    
        /**
         * Starting point for the demonstration application.
         *
         * @param args  ignored.
         */
        public static void main(String[] args) {
            BarChartDemo demo = new BarChartDemo("JFreeChart: BarChartDemo1.java");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
        }
    
    }
    

    我可以在项目中右击这个文件并运行它来得到这个:

    【讨论】:

    • 嗨。感谢那。很好的答案。似乎我缺少的一点是解压缩下载并在 unaipped 文件夹中找到 .jar 文件 - 我试图直接添加 .zip 。我现在已经开始工作了。谢谢
    【解决方案2】:

    我花了几个小时解决这个问题,终于解决了问题。

    您必须下载 .jar 文件 您从 JFreeChart 网站下载压缩文件。

    两个文件的链接如下。

    http://mvnrepository.com/artifact/org.jfree/jcommon/1.0.23 http://mvnrepository.com/artifact/org.jfree/jfreechart/1.0.19

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多