2013-10-30 85 views
1

我有股票市場項目的工作。我想製作顯示股市圖表和新聞的桌面應用程序。到目前爲止,我已經有一個主要的框架(類),其中有RSS,第二個是顯示蠟燭貼圖(2類)的應用框架。我想將圖表放入我的主框架中。如何將它放到我的主框架中(或者我怎樣才能將框架調用到框架中)。我會真正感謝所有的建議。 這裏是主框架的代碼: 如何將Jframe調用爲主Jfram

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

回答

0

JFreeChart的是例如繪製圖表用於由IB TWS。

您可以使用插入HTML代碼到swing中。