【问题标题】:How to create a Chart Explorer with Java and JFreeChart?如何使用 Java 和 JFreeChart 创建图表资源管理器?
【发布时间】:2012-07-08 18:07:37
【问题描述】:

在 JFreeChart 库的帮助下,我创建了一个 Java 桌面程序。 我可以打开一个股票数据文件 (symbol.txt) 并让程序显示几个指标。 我想在一个新的 JFrame 窗口中创建一个资源管理器,该窗口将打开一个包含股票数据文件的文件夹并检查一些指标值。 我的问题是找到一种方法一次打开多个文件(在用户级别),但在程序级别一个接一个地处理每个文件(打开第一个文件,检查指示器,关闭第一个文件......继续第二个文件) . 谢谢。

几个小时后更新:

这是第一次尝试,当我尝试打开多个文件时会产生org.jfree.data.general.SeriesException 错误。

您正在尝试添加 2011 年 1 月 4 日期间的观察 但该系列已经包含对该时间段的观察。 不允许重复。尝试使用 addOrUpdate() 方法。
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nysemarketpick;

import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author skiabox
 */
public class ExplorerForm extends javax.swing.JFrame {

    PriceVolumeChart chart;

    /**
     * Creates new form ExplorerForm
     */
    public ExplorerForm() {
        initComponents();
    }

    /**
     * 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() {

        fileChooser = new javax.swing.JFileChooser();
        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        fileMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openItem = new javax.swing.JMenuItem();

        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.setMultiSelectionEnabled(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Stock Explorer");

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollPane.setViewportView(textArea);

        fileMenu.setText("File");

        openItem.setText("Open");
        openItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openItemActionPerformed(evt);
            }
        });
        fileMenu.add(openItem);

        fileMenuBar.add(fileMenu);

        setJMenuBar(fileMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(scrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 314, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(75, 75, 75)
                .add(scrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(103, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void openItemActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            File files[];

            //File myFile;

            files = fileChooser.getSelectedFiles();

            for (int i = 0; i < files.length; i++)
            {
                //myFile = files
                chart = new PriceVolumeChart(files[i].getAbsolutePath());

                System.out.println(files[i].getAbsolutePath());

                if (chart.getTLDirection().equals("(-1)"))
                {
                    textArea.append(files[i].getAbsolutePath() + "\n");
                }

                chart = null;
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ExplorerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ExplorerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ExplorerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ExplorerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ExplorerForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuBar fileMenuBar;
    private javax.swing.JMenuItem openItem;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
    // End of variables declaration
}

【问题讨论】:

  • 这个类创建一个图表和指标,然后传递一个JPane。你要我粘贴代码吗?
  • 我找到了解决方案……大约一个小时左右就会收到回复……敬请期待。
  • 我刚刚将时间序列的静态对象实例创建从类变量 list 移到类构造函数 (PriceVolumeChart) 中,以便每次都为每个股票文件构造时间序列对象.
  • 我一会儿贴出解决方案的代码作为回复

标签: java swing netbeans jfreechart netbeans-7


【解决方案1】:

JInternalFrame 可能是个不错的选择。可以在herehere 中找到示例。使用Action 使您的菜单和文档井井有条,如herehere 所示。

附录:我说的是 3200 个文件。

JTable 是这个数量级的不错选择。使用自定义 editor 创建一个作用于特定行的按钮。多选可以使用ListSelectionListener,显示为here,或复选框,显示为here

附录:您可以在模态对话框中打开JFileChooser,在AbstractAction 中看到here,或者嵌入到框架中,如图here

【讨论】:

  • 我说的是 3200 个文件。所以我不想显示任何图表。我只想将符合我条件的图表的文件名添加到文本框中。
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多