java - 如何使用 Java 和 JFreeChart 创建图表浏览器?

标签 java swing netbeans jfreechart netbeans-7

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

几小时后更新:

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

You are attempting to add an observation for the time period 4-January-2011
but the series already contains an observation for that time period.
Duplicates are not permitted.  Try using the addOrUpdate() method.
/*
 * 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
}

最佳答案

JInternalFrame可能是一个不错的选择。示例可参见 herehere 。使用 Action 使菜单和文档井井有条,如图 here 所示。和 here .

image

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

JTable对于这个数量级来说是一个不错的选择。使用自定义editor创建一个作用于特定行的按钮。可以使用 ListSelectionListener 进行多项选择,如图 here ,或复选框,如图 here .

image

附录:您可以在模式对话框中打开JFileChooser,参见hereAbstractAction 中,或嵌入在框架中,如 here 所示.

关于java - 如何使用 Java 和 JFreeChart 创建图表浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385419/

相关文章:

java - 我如何在java中使用sql数据库中的日期来改进搜索

java - 如何使用 Selenium WebDriver 和 Java 从下拉列表中选择一个选项?

java - android fragment 的最佳实践?

java - 如何设置图标没有间隙?

java - 如何阻止 Java Swing JOptionPane OK_CANCEL_OPTION 在 OK 选项上关闭?

java - 如何在新打开的 jframe 上关闭以前的 jframe

java - 添加图像对象时出错

java - 为什么这段代码注释可以作为JFrame运行?

java - 使用CDI和OSGI时出错

java - 通过单击 jRadiobutton 更改 jComboBox 中的项目