简体   繁体   中英

How to call Jframe into main Jfram

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM