我在动作监听器方法中编写程序的一部分时遇到了麻烦。这是我到目前为止的所有内容 - 任何帮助都会得到解释,并说明正在做什么。赋值需要使用JFileChooser。我有三个文件。一个包含名词,一个动词,一个包含一个故事,其中必须在随机选择后插入名词和动词。 在故事文件中,#被放置在名词必须去的地方,%放在动词必须去的地方。我需要取名词和动词并用它们替换符号。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class MadLibApp extends JFrame implements ActionListener
{
JButton storyButton = new JButton("SELECT STORY FILE");
JButton verbsButton = new JButton("SELECT VERBS FILE");
JButton nounsButton = new JButton("SELECT NOUNS FILE");
JFileChooser fileChooser = new JFileChooser();
JTextField output = new JTextField(20);
public static void main()
{
MadLibApp frame = new MadLibApp();
frame.setBackground(Color.PINK);
frame.setSize(300, 300);
frame.setTitle("MAD LIB GAME");
frame.createGUI();
frame.setVisible(true);
}
public void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
JPanel panel = new JPanel();
panel.setSize(300,300);
panel.setBackground(Color.PINK);
window.add(panel);
storyButton.addActionListener(this);
verbsButton.addActionListener(this);
nounsButton.addActionListener(this);
JLabel outputLabel = new JLabel("Your Mad-Lib is: ");
JButton button = new JButton("click to play");
panel.add(storyButton);
panel.add(verbsButton);
panel.add(nounsButton);
panel.add(outputLabel);
panel.add(output);
}
public void actionPerformed(ActionEvent e)
{
Random selectRandomVerb = new Random();
Random selectRandomNoun = new Random();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
if (e.getSource().equals(verbsButton))
{
try
{
File verbsFile = fileChooser.getSelectedFile();
if (verbsFile.exists())
{
Scanner verbsScanner = new Scanner(new FileInputStream(verbsFile));
ArrayList<String> verbLines = new ArrayList<String>();
String verbLine;
while ((verbLine = verbsScanner.nextLine()) != null) {
verbLines.add(verbLine);
}
int v = selectRandomVerb.nextInt(verbLines.size());
String verb = verbLines.get(v);
}
else
{
JOptionPane.showMessageDialog(null, "That file does not exist.");
}
}
catch (FileNotFoundException ev)
{
ev.printStackTrace();
}
}
if (e.getSource().equals(nounsButton))
{
try
{
File nounsFile = fileChooser.getSelectedFile();
if (nounsFile.exists())
{
Scanner nounsScanner = new Scanner(new FileInputStream(nounsFile));
ArrayList<String> nounLines = new ArrayList<String>();
String nounLine;
while ((nounLine = nounsScanner.nextLine()) != null) {
nounLines.add(nounLine);
}
int n = selectRandomNoun.nextInt(nounLines.size());
String noun = nounLines.get(n);
}
else
{
JOptionPane.showMessageDialog(null, "That file does not exist.");
}
}
catch(FileNotFoundException ev)
{
ev.printStackTrace();
}
}
if (e.getSource().equals(storyButton))
{
File storyFile = fileChooser.getSelectedFile();
try{
if (storyFile.exists())
{
Scanner storyScanner = new Scanner(new FileInputStream(storyFile));
ArrayList<String> storyArrayList = new ArrayList<String>();
for (String element : storyArrayList) {
if (storyScanner.nextLine() == "#")
{
}
}
for (String element : storyArrayList) {
if (storyScanner.nextLine() == "%")
{
}
}
// output.setText();
}
else
{
JOptionPane.showMessageDialog(null, "That file does not exist.");
}
}
catch(FileNotFoundException ev)
{
ev.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
创建Scanner
时,您应该传递FileInputStream
来阅读文件,因为如果您通过System.in
,Scanner
将从控制台输入中读取。
试试这个:
try (Scanner sc = new Scanner(new FileInputStream(verbsFile));){
ArrayList<String> lines = new ArrayList<String>();
String line;
while ((line = sc.nextLine()) != null) {
lines.add(line);
}
Random rd = new Random();
int i = rd.nextInt(lines.size());
String verb = lines.get(i); // this is a random verb from the verbsFile
} catch (FileNotFoundException e) {
e.printStackTrace();
}
仅当文件中的动词各自独立时才会起作用。
我看到你编辑了你的问题,以下是如何将更改写入storyFile
:
try (PrintWriter pw = new PrintWriter(new File("storyFile"));) {
for (String line : storyArrayList) {
if (line.equals("#")) {
pw.append(noun);
} else if (line.equals("%")) {
pw.append(verb);
} else {
pw.append(line);
}
pw.write(System.lineSeparator());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在您阅读Scanner
中的所有行并将storyFile
和nouns
变量设为全局后,您应该关闭verbs
。您的代码中还有其他位置可以创建Scanners
并将其保持打开状态。您的代码可以正常工作,但在EDT线程
答案 1 :(得分:0)
我看不到
JFileChooser.APPROVE_OPTION
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}