如何将Jframe调用为主Jfram [英] How to call Jframe into main Jfram

查看:46
本文介绍了如何将Jframe调用为主Jfram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是单独的烛台图:

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

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);
        }
}

解决方案

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

您可以使用插入HTML代码.

I have working on stock market project. I want to make desktop app which shows stock markets chart and news. I have made so far a main frame (class) which has RSS and the second is applcationframe which shows candle stick chart (2 class). I would like to place the chart into my main frame. How can place it into my main frame/ (or how can I call application frame into frame). I will truly appreciate all advices. Here is the code for main frame:

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);*/

    }
}

Here is separate Candlestick chart:

If i want to just create a method to show charts in main frame, how can do this?

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);
        }
}

解决方案

JFreeChart is used by IB TWS for example to draw charts.

You can insert HTML code into swing using .

这篇关于如何将Jframe调用为主Jfram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆