如何获取Jcombobox项来编辑字符串Url

时间:2016-01-11 14:26:25

标签: java swing

我正在开发一个名为股市数据查看器的小项目。我现在卡住了。我有两个类:其中一个类从csv文件中读取和存储值。另一个是jframe类。我想如果我点击组合框中的项目,网址会被更改。我尝试了不同的方法似乎没有任何作用。下面是两个类的代码。

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;


public class StockMarketViewerPanel {

    private ArrayList<StoreData>csvarraylist;

    public StockMarketViewerPanel(){
        csvarraylist =new ArrayList<StoreData>();
    }

    public void getdata(){      
        try{
        String Line;
        StoreData store;

        URL data = new URL("http://www.google.com/finance/historical?q="+symbol+"&histperiod=daily&startdate="Jan"+"1"+"2013 "&enddate=" Dec"+"31"+"2013 "&output=csv"););
        Scanner s = new Scanner(data.openStream());
         "http://www.google.com/finance/historical?q="+symbol+"&histperiod=daily&startdate="Jan"+"1"+"2013 "&enddate=" Dec"+"31"+"2013 "&output=csv");

        s.nextLine(); // skip the first line
        while(s.hasNextLine()){
        Line = s.nextLine();            
        String[] csvdata = Line.split(",");
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
        Date newdate = null;        
        newdate = (Date)df.parse(csvdata[0]);

        double open = Double.parseDouble(csvdata[1]);
        double high = Double.parseDouble(csvdata[2]);
        double low = Double.parseDouble(csvdata[3]);
        double close = Double.parseDouble(csvdata[4]);
        double volume = Double.parseDouble(csvdata[5]); 

        store = new StoreData(newdate,open,high,low,close,volume);
        csvarraylist.add(store);    

        }       
        s.close();  

        printlist(csvarraylist);

        } catch(MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e){
            System.out.println("There is no such URL");

        } catch (ParseException e) {        
            e.printStackTrace();
    }
}

     public void printlist(ArrayList<StoreData> Print) {  
          for (int i = 0; i < Print.size(); i++) {  
           System.out.println("Date:" + Print.get(i).getNewdate()  
             + " , Open:" + Print.get(i).getOpen()  
             + " , High:" + Print.get(i).getHigh()  
             + " , Low:"  + Print.get(i).getLow() + " , Volume:"  
             + Print.get(i).getVolume() + "]");  
          }  



     }
}


the second class

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class MarketGUI extends JFrame {

    private JButton graphviewer;
    private TickerPanel selectticker;

    public MarketGUI() {
        setTitle ("Stock Market Data Viewer");
        setSize(300, 300);
        Container contentpane = this.getContentPane();
        selectticker = new TickerPanel();
        contentpane.add(selectticker);


        JLabel companylabel = new JLabel("Select a Company:");
        selectticker.add(companylabel, BorderLayout.WEST);

        String[] companytickers = new String[] { "AAPL", "GOOGL","GOOG" };
        // create a combo box with items 
        final JComboBox<String> company = new JComboBox<String>(companytickers);    
        selectticker.add(company, BorderLayout.EAST);   

        // Customizing the appearance of the combobox
        company.setFont(new Font("Arial", Font.BOLD, 10));
        company.setForeground(Color.BLUE);      
        company.setMaximumRowCount(10); 

        //Action Listener for the company combobox
        company.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {


            }
        });


        JLabel startdate = new JLabel("Start Date:");
        selectticker.add(startdate, BorderLayout.WEST);

        String[] startdays = new String[] { "1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16",
                                        "17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
        // create a combo box with items 
        final JComboBox<String> dddate = new JComboBox<String>(startdays);  
        selectticker.add(dddate, BorderLayout.EAST);    

        // Customizing the appearance of the combobox
        dddate.setFont(new Font("Arial", Font.BOLD, 10));
        dddate.setForeground(Color.BLUE);       
        dddate.setMaximumRowCount(10);  


        //Action Listener for the company combobox
        dddate.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {


            }
        });

        String[] startmonth = new String[] { "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",};
            // create a combo box with items 
            final JComboBox<String> months = new JComboBox<String>(startmonth); 
            selectticker.add(months, BorderLayout.EAST);    

            // Customizing the appearance of the combobox
            months.setFont(new Font("Arial", Font.BOLD, 10));
            months.setForeground(Color.BLUE);       
            months.setMaximumRowCount(10);  


            //Action Listener for the company combobox
            months.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {


                }
            });

            String[] startyear = new String[] { "2001","2002","2003","2004","2005", "2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","",};
            // create a combo box with items 
            final JComboBox<String> years = new JComboBox<String>(startyear);   
            selectticker.add(years, BorderLayout.EAST); 

            // Customizing the appearance of the combobox
            years.setFont(new Font("Arial", Font.BOLD, 10));
            years.setForeground(Color.BLUE);        
            years.setMaximumRowCount(10);   


            //Action Listener for the company combobox
            years.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {


                }
            });

        // iam now writing the java program for the END DATE comboboxes

            JLabel enddate = new JLabel("End Date:");
            selectticker.add(enddate, BorderLayout.WEST);

            String[] enddays = new String[] { "1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16",
                                            "17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
            // create a combo box with items 
            final JComboBox<String> endday = new JComboBox<String>(enddays);    
            selectticker.add(endday, BorderLayout.SOUTH);   

            // Customizing the appearance of the combobox
            endday.setFont(new Font("Arial", Font.BOLD, 10));
            endday.setForeground(Color.BLUE);       
            endday.setMaximumRowCount(10);  


            //Action Listener for the company combobox
            endday.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {


                }
            });

            String[] endmonth = new String[] { "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",};
                // create a combo box with items 
                final JComboBox<String> endmonths = new JComboBox<String>(endmonth);    
                selectticker.add(endmonths, BorderLayout.SOUTH);    

                // Customizing the appearance of the combobox
                endmonths.setFont(new Font("Arial", Font.BOLD, 10));
                endmonths.setForeground(Color.BLUE);        
                endmonths.setMaximumRowCount(10);   


                //Action Listener for the company combobox
                endmonths.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent event) {


                    }
                });

                String[] endyear = new String[] { "2001","2002", "2003", "2004", "2005", "2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","",};
                // create a combo box with items 
                final JComboBox<String> endyears = new JComboBox<String>(endyear);  
                selectticker.add(endyears, BorderLayout.SOUTH); 

                // Customizing the appearance of the combobox
                endyears.setFont(new Font("Arial", Font.BOLD, 10));
                endyears.setForeground(Color.BLUE);     
                endyears.setMaximumRowCount(10);    


                //Action Listener for the company combobox
                endyears.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent event) {


                    }
                });

                graphviewer = new JButton("Submit");
                selectticker.add(graphviewer);
                contentpane.add(selectticker, BorderLayout.EAST);
                graphviewer.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event){


                    }
                });


    }













    public static void main (String[]args) {
        JFrame frm = new MarketGUI();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);

    }   
}

1 个答案:

答案 0 :(得分:0)

试试这个:

//Action Listener for the company combobox
    company.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            URL data = new URL("whateveryouwant.com/example");
        }
    });
相关问题