簡體   English   中英

Java可運行文件,帶有文件讀取器

[英]Java runnable file, with file reader

當我嘗試在Eclipse中創建Java可運行文件時,我的班級沒有出現在“啟動配置”列表中嗎? 如何解決該問題以創建我的可運行文件。 我希望能夠從任何地方執行我的程序

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

我假設您要在html頁面中運行此applet。 這樣對嗎 :) ? 如果是這樣,您可以重寫paint(Graphics g)方法。 您可以在此處查看示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM