1

我从事股票市场项目。我想制作显示股市图表和新闻的桌面应用程序。到目前为止,我已经制作了一个具有 RSS 的主框架(类),第二个是显示蜡烛图的应用框架(2 类)。我想将图表放入我的主框架中。如何将它放入我的主框架/(或者我如何将应用程序框架调用到框架中)。我将非常感谢所有建议。这是主框架的代码:

import java.awt.BorderLayout;
public class Boeing extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Boeing frame = new Boeing();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

public static String readRSS(String urlAddress) {
         try{
            URL rssUrl = new URL(urlAddress);
            BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
            String sourceCode = "";
            String line;
            while((line = in.readLine())!=null) {
                if (line.contains("<title>")){
                    int firstPos = line.indexOf("<title>");
                    String temp = line.substring(firstPos);
                    temp = temp.replace("<title>", "");
                    int lastPos = temp.indexOf("</title>");
                    temp = temp.substring(0,lastPos);
                    sourceCode += temp + "\n" + "\n";
                }

            }
  in.close();
            return sourceCode;
        } catch (MalformedURLException ue){
            System.out.println("Malformed URL");
        } catch (IOException ioe) {
            System.out.println("Something went wrong reading the cotents");
        }
        return null;
    }

    public Boeing() {

        setResizable(false);
        setExtendedState(Frame.MAXIMIZED_BOTH);

        setTitle("StockIn");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1350, 695);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 1280, 22);
        contentPane.add(menuBar);

        JMenu mnStockin = new JMenu("StockIn");
        menuBar.add(mnStockin);

        JMenuItem mntmQuitStockin = new JMenuItem("Quit StockIn");
        mntmQuitStockin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                System.exit(0);
            }
        });

        JMenuItem mntmHome = new JMenuItem("Home");
        mntmHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Intro intro = new Intro();
                intro.setVisible(true);
                dispose();
            }
        });
mnStockin.add(mntmHome);

        JSeparator separator = new JSeparator();
        mnStockin.add(separator);
        mnStockin.add(mntmQuitStockin);

        JMenu mnLse = new JMenu("LSE");
        menuBar.add(mnLse);

        JMenuItem mntmBoeing = new JMenuItem("BOEING CO");
        mntmBoeing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Boeing boeing = new Boeing();
                boeing.setVisible(true);
                dispose();
            }
        });
        mnLse.add(mntmBoeing);



        JSeparator separator_16 = new JSeparator();
        mnTSE.add(separator_16);
        mnTSE.add(mntmSony);

        TextArea textArea = new TextArea(readRSS("http://boeing.mediaroom.com/news-releases-statements?pagetemplate=rss"));
        textArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            }
        });
        textArea.setEditable(false);
        textArea.setBounds(684, 396, 596, 277);
        contentPane.add(textArea);  

        Panel panel = new Panel();
        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent arg0) {

            }
        });
        panel.setBackground(Color.PINK);
        panel.setBounds(684, 28, 596, 368);
        contentPane.add(panel);



        /* JLabel dice1 = new JLabel();
        ImageIcon one = new ImageIcon("/Users/odzayaBatsaikhan/Desktop/chart.png");

        dice1.setLocation(20, 100);
        dice1.setSize(1000, 400);
        dice1.setIcon(one);
        contentPane.add(dice1);*/

    }
}

这是单独的烛台图表:

如果我只想创建一种在主框架中显示图表的方法,该怎么做?

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.xy.*;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.text.*;
import java.util.*;
import java.util.List;

public class CandlestickDemo extends JFrame {
        public CandlestickDemo(String stockSymbol) {
                super("CandlestickDemo");
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                DateAxis    domainAxis       = new DateAxis("Date");
                NumberAxis  rangeAxis        = new NumberAxis("Price");
                CandlestickRenderer renderer = new CandlestickRenderer();
                XYDataset   dataset          = getDataSet(stockSymbol);

                XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

                //Do some setting up, see the API Doc
                renderer.setSeriesPaint(0, Color.BLACK);
                renderer.setDrawVolume(false);
                rangeAxis.setAutoRangeIncludesZero(false);
                domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );

                //Now create the chart and chart panel
                JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false);
                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new Dimension(600, 300));

                this.add(chartPanel);
                this.pack();
        }
        protected AbstractXYDataset getDataSet(String stockSymbol) {
                //This is the dataset we are going to create
                DefaultOHLCDataset result = null;
                //This is the data needed for the dataset
                OHLCDataItem[] data;

                //This is where we go get the data, replace with your own data source
                data = getData(stockSymbol);

                //Create a dataset, an Open, High, Low, Close dataset
                result = new DefaultOHLCDataset(stockSymbol, data);

                return result;
        }
        //This method uses yahoo finance to get the OHLC data
        protected OHLCDataItem[] getData(String stockSymbol) {
                List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
                try {
                        String strUrl= "http://ichart.finance.yahoo.com/table.csv?s="+stockSymbol+"&a=0&b=1&c=2008&d=3&e=30&f=2008&ignore=.csv";
                        URL url = new URL(strUrl);
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        DateFormat df = new SimpleDateFormat("y-M-d");

                        String inputLine;
                        in.readLine();
                        while ((inputLine = in.readLine()) != null) {
                                StringTokenizer st = new StringTokenizer(inputLine, ",");

                                Date date       = df.parse( st.nextToken() );
                                double open     = Double.parseDouble( st.nextToken() );
                                double high     = Double.parseDouble( st.nextToken() );
                                double low      = Double.parseDouble( st.nextToken() );
                                double close    = Double.parseDouble( st.nextToken() );
                                double volume   = Double.parseDouble( st.nextToken() );
                                double adjClose = Double.parseDouble( st.nextToken() );

                                OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume);
                                dataItems.add(item);
                        }
                        in.close();
                }
                catch (Exception e) {
                        e.printStackTrace();
                }
                //Data from Yahoo is from newest to oldest. Reverse so it is oldest to newest
                Collections.reverse(dataItems);

                //Convert the list into an array
                OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);

                return data;
        }

        public static void main(String[] args) {
                new CandlestickDemo("MSFT").setVisible(true);
        }
}
4

1 回答 1

0

例如,IB TWS 使用 JFreeChart 来绘制图表。

您可以使用 .

于 2013-10-30T16:35:20.430 回答