简体   繁体   中英

About making a GUI program an exe file… (Java)

I recently made a random sentence generator. It reads from two files (a noun and a verb file) and uses 2 nouns from the file as a subject and an object, with a verb inbetween. It was working as a java file, but when I followed a tutorial on wikihow to make it into an exe file (link- http://www.wikihow.com/Create-an-Executable-File-from-Eclipse ), the program will run, but the label will not changed as specified. I have the noun and verb text files in the same folder as the exe file, so I do not understand where my fault it. Any help is much appreciated :D

Thanks, ArcWalrus

import java.util.Scanner;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import java.nio.charset.Charset;
import java.nio.file.*;


public class HelloWorld {

public static void main(String[] args) {

    JFrame sentenceFrame = new JFrame("Ayy Lmao");
    JLabel subjectLabel = new JLabel();
    subjectLabel.setHorizontalAlignment(JLabel.CENTER);
    subjectLabel.setText("Have fun and have a dirty, dirty mind");
    String defaultTextField = "Enter a word here!";
    JTextField inputField = new JTextField(defaultTextField);

    sentenceFrame.setLayout(new GridLayout(4,1));

    String[] optionList = {"Subject", "Verb", "Object"};
    JComboBox optionBox = new JComboBox(optionList);

    optionBox.setSelectedIndex(2);
    class ComboListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            JComboBox cb = (JComboBox)e.getSource();
            String selectedOption = (String)cb.getSelectedItem();
        }
    }

    ActionListener comboListener = new ComboListener();
    optionBox.addActionListener(comboListener);





    //sentencePanel.setLayout(new BorderLayout);
    sentenceFrame.setBackground(Color.MAGENTA);

    JButton createSentenceButton = new JButton("CREATE!");


    sentenceFrame.add(subjectLabel);
    sentenceFrame.add(inputField);
    sentenceFrame.add(optionBox);
    sentenceFrame.add(createSentenceButton);



    class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String subject;
            String verb;
            String object;
            if (inputField.getText().equals("") || inputField.getText().equals(defaultTextField)){
                String subjectNoun = findNoun();
                subject = subjectNoun.substring(0, 1).toUpperCase() + subjectNoun.substring(1, subjectNoun.length());
                verb = findVerb();
                object = findNoun();
                subjectLabel.setText(subject + " " + verb + " " + object + ".");
            }

            else{
                if(optionBox.getSelectedItem().equals("Subject")){
                    subject = inputField.getText().substring(0,1).toUpperCase() + inputField.getText().substring(1, inputField.getText().length());
                    verb = findVerb();
                    object = findNoun();
                    subjectLabel.setText(subject + " " + verb + " " + object + ".");
                }
                if(optionBox.getSelectedItem().equals("Verb")){
                    subject = findNoun();
                    subject.substring(0, 1).toUpperCase();
                    verb = inputField.getText();
                    object = findNoun();
                    subjectLabel.setText(subject + " " + verb + " " + object + ".");
                }
                if (optionBox.getSelectedItem().equals("Object")){
                    subject = findNoun();
                    subject.substring(0, 1).toUpperCase();
                    verb = findVerb();
                    object = inputField.getText();
                    subjectLabel.setText(subject + " " + verb + " " + object + ".");
                }
            }


        }
    }

    ActionListener listener1 = new ButtonListener();

    createSentenceButton.addActionListener(listener1);


    sentenceFrame.setSize(500, 200);
    sentenceFrame.setVisible(true);
    sentenceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static String findNoun(){
    /* // The name of the file to open.
    String fileName = "temp.txt";

    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }    

        // Always close files.
        bufferedReader.close();            
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                   
        // Or we could just do this: 
        // ex.printStackTrace();
    */
    Random random = new Random();

    String fileName = "src/Nouns.txt";

    String line = null;

    //int numNouns = 0;
    ArrayList<String> nounsArrayList = new ArrayList<String>();

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null){
            //System.out.println(line);
            //numNouns++;
            nounsArrayList.add(line);

        }
        bufferedReader.close();
    } catch(FileNotFoundException ex){
        System.out.println("ERROR- Unable to open file '" + fileName + "'");
        }
      catch(IOException ex) {
          System.out.println("Error reading file '" + fileName + "'");
      }

    int randomNounNum = random.nextInt(nounsArrayList.size() - 1);
    return nounsArrayList.get(randomNounNum);

    }

public static String findVerb(){
    Random random = new Random();

    String fileName = "src/Verbs.txt";

    String line = null;

    //int numNouns = 0;
    ArrayList<String> verbsArrayList = new ArrayList<String>();

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null){
            //System.out.println(line);
            //numNouns++;
            verbsArrayList.add(line);

        }
        bufferedReader.close();
    } catch(FileNotFoundException ex){
        System.out.println("ERROR- Unable to open file '" + fileName + "'");
        }
      catch(IOException ex) {
          System.out.println("Error reading file '" + fileName + "'");
      }

    int randomVerbNum = random.nextInt(verbsArrayList.size() - 1);
    return verbsArrayList.get(randomVerbNum);

    }

}

FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);

Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

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