我最近做了一个随机句子生成器。它从两个文件(一个名词和一个动词文件)中读取,并使用文件中的两个名词作为主语和宾语,中间有一个动词。它作为java文件工作,但是当我按照wikihow上的教程将其转换为exe文件(链接- http://www.wikihow.com/Create-an-Executable-File-from-Eclipse)时,程序将运行,但标签不会按照指定更改。我有名词和动词文本文件在同一文件夹的exe文件,所以我不明白我的错在哪里。如有任何帮助,不胜感激:D
谢谢,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);
应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。嵌入式资源必须通过URL而不是文件访问。请看信息。