如何将 JFreechart 添加到面板?

2024-04-02

我尝试寻找答案来使其正常工作,但没有成功,我快要失去理智了,所以这是我的问题。

我最近下载了 JFreeChart 来创建图表并在我的 GUI 中实现它们。 这是我想要插入图形的 GUI 和面板(以白色标记):

报告1 http://www.freeimagehosting.net/newuploads/eff3r.png http://www.freeimagehosting.net/newuploads/eff3r.png 报告2 http://www.freeimagehosting.net/newuploads/v5sty.png http://www.freeimagehosting.net/newuploads/v5sty.png

我使用 NetBeans 编辑器构建 GUI 和在线提供的 JFreeChart 示例。 下面是我的 GUI 的代码:

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;


public class Report extends JFrame implements Defaults {


public MMap j1map,j2map,j3map;
public int total_stations = 0, total_jobs = 0, total_parts = 0;
BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
             j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
             j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
public String sim_time;
BigDecimal systime = new BigDecimal(0);
BigDecimal worktime = new BigDecimal(0);
BigDecimal idletime = new BigDecimal(0);
BigDecimal queuetime = new BigDecimal(0);
/**
 * Creates new form Report
 */
public Report() {
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle(TITLE_Report);
    validate();
    setResizable(false);
    setVisible(true);
    //setLocationRelativeTo(null);
    initComponents();
    this.addWindowListener(new WindowAdapter() {
        @Override
       public void windowClosing(WindowEvent e) {
          e.getWindow().dispose();
          }
       }
    );
    centertables();
    //JLayeredPane layeredPane = new JLayeredPane(); 
    PieChart demo = new PieChart("Which operating system are you using?");
    demo.setOpaque(true); 
    demo.setBounds(0, 0, 300, 300);
    demo.setFocusable(true);
    demo.setBackground(Color.gray); 
    demo.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    //start();
    demo.setDoubleBuffered(true);        
    //demo.pack();
    //demo.setVisible(true);        
    //PieChart("teste");
    chartPanel1.add(demo, new Integer(0));
    //layeredPane.add(demo, new Integer(0));
    //this.getContentPane().add(jLayeredPane1); 
}

/* *************************************
 *          GRAPHICS
 *************************************/

public void PieChart(String chartTitle) {
    System.out.println("PieChart");
    // This will create the dataset 
    PieDataset dataset = createDataset();
    // based on the dataset we create the chart
    JFreeChart chart = createChart(dataset, chartTitle);
    // we put the chart into a panel
    ChartPanel chartPanel = new ChartPanel(chart);
    // default size
    chartPanel.setPreferredSize(new java.awt.Dimension(250, 270));
    // add it to our application
    //setContentPane(chartPanel);
    //StationsPanelGraph.add(chartPanel);
    //jLayeredPane1.add(chartPanel, new Integer(0), 0);

}

private  PieDataset createDataset() {
    System.out.println("PieDataset");
    DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("Linux", 29);
    result.setValue("Mac", 20);
    result.setValue("Windows", 51);
    return result;

}

private JFreeChart createChart(PieDataset dataset, String title) {
    System.out.println("Create Chart");
    JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
        dataset,                // data
        true,                   // include legend
        true,
        false);

    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setStartAngle(290);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);
    return chart;

}    

 /* *************************************
 *         END GRAPHICS
 *************************************/  

}

这是结果:

报告3 http://www.freeimagehosting.net/newuploads/unolz.png http://www.freeimagehosting.net/newuploads/unolz.png

我正在主类构造函数中初始化并添加图表,下面是图表的代码。我不得不避免发布整个代码,因为它超出了字符限制,但是这里有一个完整代码的 URL(由 NetBeans 初始化 Swing 组件):http://tny.cz/8a4e8b2f http://tny.cz/8a4e8b2f

有解决方案的提示吗?

提前致谢,如果还需要什么,请告诉我。


不要让 GUI 设计师决定您的设计。按照建议,使用它(如果有的话)来管理各个面板here https://stackoverflow.com/a/2561540/230513。如果没有.form文件;我刚刚使用添加了你的图表FlowLayout这样你就可以看到它,而不需要改变initComponents().

import java.awt.FlowLayout;
import java.math.BigDecimal;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

public class Report extends JFrame {

    public int total_stations = 0, total_jobs = 0, total_parts = 0;
    BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
        j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
        j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
    public String sim_time;
    BigDecimal systime = new BigDecimal(0);
    BigDecimal worktime = new BigDecimal(0);
    BigDecimal idletime = new BigDecimal(0);
    BigDecimal queuetime = new BigDecimal(0);

    private static final String title = "Which operating system are you using?";

    /**
    * Creates new form Report
    */
    public Report() {
        initComponents();
        jPanel2.removeAll();
        jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        jPanel2.add(createPieChart(title));
        this.setLocationRelativeTo(null);
    }

    /*
    * *************************************
    * GRAPHICS ***********************************
    */
    private ChartPanel createPieChart(String chartTitle) {
        System.out.println("PieChart");
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        return chartPanel;
    }

    private PieDataset createDataset() {
        System.out.println("PieDataset");
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;

    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        System.out.println("Create Chart");
        JFreeChart chart = ChartFactory.createPieChart3D(
            title, dataset, true, true, false);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setCircular(true);
        return chart;

    }

    /*
    * *************************************
    * END GRAPHICS ***********************************
    */
    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        // code elided
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Report().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    public javax.swing.JPanel chartPanel1;
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel10;
    public javax.swing.JLabel jLabel11;
    public javax.swing.JLabel jLabel12;
    public javax.swing.JLabel jLabel13;
    public javax.swing.JLabel jLabel14;
    public javax.swing.JLabel jLabel15;
    public javax.swing.JLabel jLabel16;
    public javax.swing.JLabel jLabel17;
    public javax.swing.JLabel jLabel18;
    public javax.swing.JLabel jLabel19;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel20;
    public javax.swing.JLabel jLabel21;
    public javax.swing.JLabel jLabel22;
    public javax.swing.JLabel jLabel23;
    public javax.swing.JLabel jLabel24;
    public javax.swing.JLabel jLabel25;
    public javax.swing.JLabel jLabel26;
    public javax.swing.JLabel jLabel27;
    public javax.swing.JLabel jLabel28;
    public javax.swing.JLabel jLabel29;
    public javax.swing.JLabel jLabel30;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel jLabel7;
    public javax.swing.JLabel jLabel8;
    public javax.swing.JLabel jLabel9;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    public javax.swing.JPanel jPanel3;
    public javax.swing.JPanel jPanel4;
    public javax.swing.JPanel jPanel6;
    public javax.swing.JPanel jPanel7;
    public javax.swing.JPanel jPanel8;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JScrollPane jScrollPane3;
    public javax.swing.JScrollPane jScrollPane5;
    public javax.swing.JScrollPane jScrollPane6;
    public javax.swing.JSeparator jSeparator1;
    public javax.swing.JSeparator jSeparator3;
    public javax.swing.JSeparator jSeparator4;
    public javax.swing.JSeparator jSeparator5;
    public javax.swing.JSeparator jSeparator6;
    public javax.swing.JTabbedPane jTabbedPane1;
    public javax.swing.JTable jTable1;
    public javax.swing.JTable jTable2;
    public javax.swing.JTable jTable3;
    public javax.swing.JTable jTable4;
    // End of variables declaration
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 JFreechart 添加到面板? 的相关文章

随机推荐

  • foreach 语句无法对“getenumerator”的公共定义类型的变量进行操作

    Task03Entities Entites entities new Task03Entities Entites Creat a object for my entites class Task03BAL BAL bal new Tas
  • PHP:帮助解码恶意代码[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 eval gzuncompress base64 decode eF5Tcffxd3L0CY5WjzcyNDG2NDc3MLGMV4 1d
  • 安装包时非零退出,仅 tidyverse

    我已经在 Ubuntu 上设置了托管 RStudio 并且已经加载了几个没有问题的软件包 包括 caret 和 lubridate 然而 当我尝试安装 tidyverse 时 我得到 gt install packages tidyvers
  • 设置与 Google 表单中的标签不同的值

    有没有办法使用 Google Forms Apps 脚本或 Google Sheets 公式来设置与 Google Forms 上的多项选择中的标签不同的值 我正在寻找类似于 html 的内容
  • 防止滚动 TVertScrollBox 时触发事件

    通常 当滚动 滚动框 的内容时 滚动框的子组件 例如 滚动框 不会触发任何事件函数 G 在本机应用程序中 但在 FireMonkey 中 如果 TVertScrollBox 包含像 TRectangle 这样的子元素 我想将其用作自定义菜单
  • 更改 rmarkdown 生成的 PDF 中的字体

    我正在使用 rmarkdown 生成报告 编织 PDF 时 title Untitled output pdf document I would like to specify the font to be used in creating
  • 如何在 vb.net 中使用 openfiledialog 打开文件?

    如何使用 openfiledialog 打开文件 下面是我的代码 Dim Fs As StreamReader With OpenFD FileName Title Open Text File InitialDirectory c Fil
  • 更改字符串字符时出现分段错误(核心转储)

    为什么更改字符串字符会导致分段错误 核心转储 char str string str 0 S segmentation fault core dumped 解决方案很简单 用以下方式声明你的字符串 char str string 您应该这样
  • AWS API Gateway 不存在“Access-Control-Allow-Origin”标头

    我遇到了 API 网关的问题 我已经浏览了 AWS 论坛上的所有其他答案 也浏览了他们的文档 但仍然没有任何乐趣 我正在尝试使用 AWS API 网关设置一个 API 该网关调用 Lambda 函数来读取 写入 DynamoDB 中的表 D
  • SSIS 中的别名参数

    我在 SSIS 中使用 OLE DB 命令 其 SQL 命令如下所示 UPDATE DBO CLIENT SET TimeZoneID DaylightSavingTime ModifiedBy MicrosPropertyID IsOff
  • Haskell 函子隐含定律

    类型分类百科全书 http www haskell org haskellwiki Typeclassopedia says 类似的论点还表明 任何满足第一定律 fmap id id 的 Functor 实例也将自动满足第二定律 实际上 这
  • 检测 Asp.net 上的浏览​​器关闭

    我想在注销时执行一些功能 如果用户直接关闭浏览器 则需要执行相同的功能 我们无法在页面卸载上执行此操作 因为我的网站中有 100 多个页面 因为这将在每个页面的重定向上起作用页 谢谢
  • 操作员 '??'不能应用于“System.DateTime”类型的操作数

    我收到以下错误 Operator cannot be applied to operands of type System DateTime foreach EndServReward r in reward if con State Co
  • R TwitteR 包授权错误

    我正在关注最新更新推特主页 https github com geoffjentry twitteR 我无法通过授权流程 library devtools install github twitteR username geoffjentr
  • php 中 eregi() 的替代方案 [重复]

    这个问题在这里已经有答案了 因此 我在邮件脚本中使用了 eregi 但最近 我收到该函数已弃用的错误 那么 替换以下代码的最简单方法是什么 if eregi A Z0 9 A Z0 9 A Z 2 4 trim POST email 任何帮
  • Pandas:如何创建年周变量?

    我有一个带有日期时间的数据框 dates pd date range 9 25 2010 periods 10 freq D df pd DataFrame col dates df col pd to datetime df col df
  • 连接无序线段

    我的算法生成一个 通常 数千条线段 全是二维 的列表 我需要将它们连接成大型折线 这些生成的折线可能是闭合的或开放的 但它们永远不会自相交 线段没有方向 即可能需要翻转线段才能将其连接到相邻线段 找到这些折线的极快方法是什么 我必须实时执行
  • 什么是可以轻松集成到现有应用程序的优秀 Ruby on Rails 论坛?

    什么是可以轻松集成到现有应用程序的优秀开源 RoR 3 论坛 可选功能 OpenID 支持 Haml SCSS 模板 支持表情符号 YouTube 图像等 我可能会对其进行很多更改 而且我对 Ruby 的了解仍然很弱 因此干净的 带注释的代
  • 如何反转 sed 输出?

    我正在阅读大约 500 个字符的行 我怎样才能让 sed 不用某些东西替换该字符串 而是用某些东西替换该行的其余部分 简而言之 我想删除指定字符串周围的所有文本 使用 awk 删除列不起作用 因为匹配的字符串前后有不确定数量的字符 有任何想
  • 如何将 JFreechart 添加到面板?

    我尝试寻找答案来使其正常工作 但没有成功 我快要失去理智了 所以这是我的问题 我最近下载了 JFreeChart 来创建图表并在我的 GUI 中实现它们 这是我想要插入图形的 GUI 和面板 以白色标记 报告1 http www freei