Java runnable文件,带文件阅读器

时间:2014-04-20 17:16:12

标签: java eclipse runnable

当我尝试在eclipse中创建一个java可运行文件时,我的类没有出现在Launch Configuration列表中?我该如何解决这个问题来创建我的可运行文件。我希望能够从任何地方执行我的程序

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.AncestorListener;




public class StartActivity extends Applet implements ActionListener{
/**
 * 
 */
private static final long serialVersionUID = -1470898831456146593L;
private JTextField textField;
public JTextField[] textStock = new JTextField[190];
public tags[] tags = new tags[90];
public String[] tg = new String[90];
public JList set = new JList();

   public StartActivity(){

    }
public void init() {

    setSize(1800, 950); // create an instance 
    setBackground(Color.gray);
    setFocusable(true);
    setLayout(null);

    FileReader file = null; // get the tags and their name
    FileReader file2 = null;
    try {
        file = new FileReader("resource/tagsName.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br = new BufferedReader(file);
    try {
        file2 = new FileReader("resource/ref.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br2 = new BufferedReader(file2);



    //      textField = new JTextField();
    //      textField.setBounds(35, 136, 86, 20);
    //      add(textField);
    //      textField.setColumns(10);
    int x = 35;
    int  y= 136;

    for (int i=0 ; i < 190 ; i++){ // add the text field to enter the quote
        textStock[i] = new JTextField("");
        textStock[i].setBounds(x,y,86,20);
        add(textStock[i]);
        textStock[i].setColumns(10);
        y = y + 25;
        if (y > 900){
            y = 136;
            x = x + 91;
        }
    }

    JLabel label1 = new JLabel("Enter Stock Symbols");
    label1.setBounds((x-35)/2, 100, 200, 20);
    add(label1);

    //addKeyListener(this);


    String tg = "";
    String rf = "";
    x = x + 200;
    y= 136;
    int i = 0;
    int  z = x;
    try { // 
        while((tg = br.readLine()) != null){ // add textfields to enter their tags
            rf =br2.readLine();
            tags[i] = new tags(tg, rf); // tags are a separte method that has their own info
            tags[i].setBounds(x, y, 200, 23); // tags are declared as buttons
            add(tags[i]);
            y = y + 28;
            if (y > 900){
                y = 136;
                x = x + 205;
            }
            tg = "";
            i = i +1;
        }
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        br.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JLabel label2 = new JLabel("Choose stock tags Retrival");
    label2.setBounds((x+z)/2, 100, 200, 20);
    add(label2);

    //set = new JList();

    JButton list1= new JButton("List 1");
    list1.setBounds(35, 100, 89, 23);
    list1.addActionListener(this);
    add(list1);

    JButton run = new JButton("Run");
    run.setBounds(getWidth()/2, 32, 89, 23);
    run.addActionListener(this);
    add(run);

    JButton analyse = new JButton("Analyse");
    analyse.setBounds(getWidth()/2 - 200, 32, 89, 23);
    analyse.addActionListener(this);
    add(analyse);

    Frame frame = (Frame) this.getParent().getParent();
    frame.setTitle(" Financial Analysis");
    try {

    } catch (Exception e) {
        // TODO: handle exception
    }


}




public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub 
    /**
     * call method depending on their button
     */

    System.out.println(arg0.getActionCommand());
    if (arg0.getActionCommand()== "Run"){
        stockdata();
    }
    //System.out.println("true");
    if (arg0.getActionCommand()== "List 1"){
        listSet(1);
    }
    if (arg0.getActionCommand()== "Analyse"){
        choose();
        System.out.println(arg0.getActionCommand());
    }
}

private void choose() {
    /*
     * analyse quotes with parameters that I choose
     */
    int m =0;
    String[]   quotes = new String[200];
    int s =0;
    String[]  tq = new String[20];
        String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
        for (int i=0 ; i < 60 ; i++){
            String stock =textStock[i].getText();
            if (m == 0 && stock != ""){
                qurl = qurl + stock ;
                m =1 ;
                quotes[s] = stock;
                s=s+1;
            }
            else if(stock != ""){
                qurl = qurl +"+"+ stock ;
                quotes[s] = stock;
                s=s+1;
            }
        }
        qurl = qurl + "&f=l1vhgd1rp6";

        System.out.println(qurl);
        Analysis tab = new Analysis(qurl, quotes); // param are declared in another method
        tab.setVisible(true);
}

private void stockdata() {

    /*
     * retrieves stock data 
     */
int m =0;
String[]  tq = new String[20];
String[]   quotes = new String[200];
int s =0;
    String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
    for (int i=0 ; i < 60 ; i++){
        String stock =textStock[i].getText();
        if (m == 0 && stock != ""){
            qurl = qurl + stock ;
            quotes[s] = stock;
            s=s+1;
            m =1 ;
        }
        else if(stock != ""){
            qurl = qurl +"+"+ stock ;
            quotes[s] = stock;
            //System.out.println(quotes[s]);
            s=s+1;
        }
    }
    qurl = qurl + "&f=";
    int z =0;
    for (int i=0 ; i < 60 ; i++){
        if(tags[i].getStatus()){
            qurl = qurl + tags[i].getReference();
            tq[z]= tags[i].getName();
            z =z+1;
        }

    }   

    System.out.println(qurl);
    // this method retrives data and creates a table to show them
    StockData tab = new StockData(qurl, tq, quotes);
    tab.setVisible(true);
}
private void listSet(int fileNumber){
    // this method populates a set of stocks
    FileReader file = null;

    try {
        file = new FileReader("resource/set"+fileNumber + ".txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br = new BufferedReader(file);
    String tg;
    int i = 0;
    try {
        while((tg = br.readLine()) != null){
            if (i<190){
                textStock[i].setText(tg);
                i = i +1;

            }}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main( String args[] )
{
    // Create an instance of the test application

}

}

更新我做了这些更改:

   public class StartActivity extends JFrame implements ActionListener{
/**
 * 
 */
private static final long serialVersionUID = -1470898831456146593L;
private JTextField textField;
public JTextField[] textStock = new JTextField[190];
public tags[] tags = new tags[90];
public String[] tg = new String[90];
public JList set = new JList();

    public StartActivity(){

    setSize(1800, 950); // create an instance 
    setBackground(Color.gray);


    FileReader file = null; // get the tags and their name
    FileReader file2 = null;
    try {

        file = new FileReader("src/resource/tagsName.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br = new BufferedReader(file);
    try {
        file2 = new FileReader("src/resource/ref.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br2 = new BufferedReader(file2);



    //      textField = new JTextField();
    //      textField.setBounds(35, 136, 86, 20);
    //      add(textField);
    //      textField.setColumns(10);
    int x = 35;
    int  y= 136;

    for (int i=0 ; i < 190 ; i++){ // add the text field to enter the quote
        textStock[i] = new JTextField("");
        textStock[i].setBounds(x,y,86,20);
        add(textStock[i]);
        textStock[i].setColumns(10);
        y = y + 25;
        if (y > 900){
            y = 136;
            x = x + 91;
        }
    }

    JLabel label1 = new JLabel("Enter Stock Symbols");
    label1.setBounds((x-35)/2, 100, 200, 20);
    add(label1);

    //addKeyListener(this);


    String tg = "";
    String rf = "";
    x = x + 200;
    y= 136;
    int i = 0;
    int  z = x;
    try { // 
        while((tg = br.readLine()) != null){ // add textfields to enter their tags
            rf =br2.readLine();
            tags[i] = new tags(tg, rf); // tags are a separte method that has 
            tags[i].setBounds(x, y, 200, 23); // tags are declared as buttons
            add(tags[i]);
            y = y + 28;
            if (y > 900){
                y = 136;
                x = x + 205;
            }
            tg = "";
            i = i +1;
        }
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        br.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JLabel label2 = new JLabel("Choose stock tags Retrival");
    label2.setBounds((x+z)/2, 100, 200, 20);
    add(label2);

    //set = new JList();

    JButton list1= new JButton("List 1");
    list1.setBounds(35, 100, 89, 23);
    list1.addActionListener(this);
    add(list1);

    JButton run = new JButton("Run");
    run.setBounds(getWidth()/2, 32, 89, 23);
    run.addActionListener(this);
    add(run);

    JButton analyse = new JButton("Analyse");
    analyse.setBounds(getWidth()/2 - 200, 32, 89, 23);
    analyse.addActionListener(this);
    add(analyse);

    //Frame frame = (Frame) this.getParent().getParent();
    //frame.setTitle(" Financial Analysis");
    try {

    } catch (Exception e) {
        // TODO: handle exception
    }


}




public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub 
    /**
     * call method depending on their button
     */

    System.out.println(arg0.getActionCommand());
    if (arg0.getActionCommand()== "Run"){
        stockdata();
    }
    //System.out.println("true");
    if (arg0.getActionCommand()== "List 1"){
        listSet(1);
    }
    if (arg0.getActionCommand()== "Analyse"){
        choose();
        System.out.println(arg0.getActionCommand());
    }
}

private void choose() {
    /*
     * analyse quotes with parameters that I choose
     */
    int m =0;
    String[]   quotes = new String[200];
    int s =0;
    String[]  tq = new String[20];
        String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
        for (int i=0 ; i < 60 ; i++){
            String stock =textStock[i].getText();
            if (m == 0 && stock != ""){
                qurl = qurl + stock ;
                m =1 ;
                quotes[s] = stock;
                s=s+1;
            }
            else if(stock != ""){
                qurl = qurl +"+"+ stock ;
                quotes[s] = stock;
                s=s+1;
            }
        }
        qurl = qurl + "&f=l1vhgd1rp6";

        System.out.println(qurl);
        Analysis tab = new Analysis(qurl, quotes); // param are declared in another                
        tab.setVisible(true);
}

private void stockdata() {

    /*
     * retrieves stock data 
     */
int m =0;
String[]  tq = new String[20];
String[]   quotes = new String[200];
int s =0;
    String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
    for (int i=0 ; i < 60 ; i++){
        String stock =textStock[i].getText();
        if (m == 0 && stock != ""){
            qurl = qurl + stock ;
            quotes[s] = stock;
            s=s+1;
            m =1 ;
        }
        else if(stock != ""){
            qurl = qurl +"+"+ stock ;
            quotes[s] = stock;
            //System.out.println(quotes[s]);
            s=s+1;
        }
    }
    qurl = qurl + "&f=";
    int z =0;
    for (int i=0 ; i < 60 ; i++){
        if(tags[i].getStatus()){
            qurl = qurl + tags[i].getReference();
            tq[z]= tags[i].getName();
            z =z+1;
        }

    }   

    System.out.println(qurl);
    // this method retrives data and creates a table to show them
    StockData tab = new StockData(qurl, tq, quotes);
    tab.setVisible(true);
}
private void listSet(int fileNumber){
    // this method populates a set of stocks
    FileReader file = null;

    try {
        file = new FileReader("src/resource/set"+fileNumber + ".txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedReader br = new BufferedReader(file);
    String tg;
    int i = 0;
    try {
        while((tg = br.readLine()) != null){
            if (i<190){
                textStock[i].setText(tg);
                i = i +1;

            }}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main( String args[] )
{
    StartActivity mainFrame = new StartActivity();
    mainFrame.setVisible( true );

}

}

您必须将文件阅读器更改为此 http://alvinalexander.com/blog/post/java/read-text-file-from-jar-file

1 个答案:

答案 0 :(得分:0)

我假设您想在html页面中运行此applet。这样对吗 :) ?如果是这样,你可以覆盖paint(Graphics g)方法。您可以看到示例here