gpt4 book ai didi

java - 关于将 GUI 程序制作为 exe 文件.... (Java)

转载 作者:行者123 更新时间:2023-12-02 04:40:07 25 4
gpt4 key购买 nike

我最近做了一个随机句子生成器。它从两个文件(一个名词和一个动词文件)中读取,并使用文件中的 2 个名词作为主语和宾语,中间有一个动词。它作为一个java文件工作,但是当我按照wikihow上的教程将其制作成exe文件(链接-http://www.wikihow.com/Create-an-Executable-File-from-Eclipse)时,程序将运行,但标签不会按照指定的方式更改。我的名词和动词文本文件与 exe 文件位于同一文件夹中,所以我不明白我的错在哪里。非常感谢任何帮助:D

谢谢,弧海象

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 而不是文件访问。请参阅info. page for embedded resource了解如何构成 URL。

关于java - 关于将 GUI 程序制作为 exe 文件.... (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30283116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com