gpt4 book ai didi

java - 我可以使用 BufferedReader 并在 actionListener 类中创建一个数组吗?

转载 作者:行者123 更新时间:2023-11-29 06:16:57 28 4
gpt4 key购买 nike

我已经创建了一个 actionListener 类。在 actionPerformed 方法中,我想运行一段代码。此代码涉及从多个文本文件导入数据,并将它们存储在二维数组中。然后它将打印出框架中的报价列表。然后它会打印出5个分析,让用户选择哪一个。但是,我目前被 IOException 困住了。另外,某些代码状态会给出错误“无法访问的代码”。这是什么意思?下面是我的 actionListener 类的代码

import java.awt.event.*;
import java.io.*;
import java.util.*;

public class CrucibleButtonListener implements ActionListener
{
private Swing g;

public CrucibleButtonListener (Swing g)
{
this.g = g;
}

public void actionPerformed(ActionEvent e)
{
this.g.updateTextField(getQuotes());
}

private String getQuotes() throws IOException
{
BufferedReader inputQuote;
BufferedReader inputTheme;
BufferedReader inputCharacterAnalysis;
BufferedReader inputSignificance;
BufferedReader inputPlotEnhancement;
BufferedReader inputSpeaker;

Scanner in = new Scanner (System.in);
int howMany=0;
int quoteSelection;
int analysisSelection;

// Count how many lines in the text file
FileReader fr = new FileReader ("CrucibleQuotations.txt");
LineNumberReader ln = new LineNumberReader (fr);
while (ln.readLine() != null)
{
howMany++;
}

//import information from the text file
inputQuote = new BufferedReader (new FileReader ("CrucibleQuotations.txt"));
inputTheme = new BufferedReader (new FileReader("CrucibleTheme.txt"));
inputCharacterAnalysis = new BufferedReader (new FileReader("CrucibleCharacterAnalysis.txt"));
inputSignificance = new BufferedReader (new FileReader("CrucibleSignificance.txt"));
inputPlotEnhancement = new BufferedReader (new FileReader("CruciblePlotEnhancement.txt"));
inputSpeaker = new BufferedReader (new FileReader("CrucibleSpeaker.txt"));

//Create array based on how many quotes available in the text file
String [][] quotes = new String [howMany][6];

//Store quote information in the array and display the list of quotations
for (int i=0; i<howMany; i++)
{
quotes [i][0] = inputQuote.readLine();
return (i+1 + ") " + quotes [i][0]);
quotes [i][1] = inputTheme.readLine();
quotes [i][2] = inputCharacterAnalysis.readLine();
quotes [i][3] = inputSignificance.readLine();
quotes [i][4] = inputPlotEnhancement.readLine();
quotes [i][5] = inputSpeaker.readLine();
}

//Ask user to choose the quote they want to analyze
return ("Choose a quotation by inputting the number: ");
quoteSelection = in.nextInt ();

if (quoteSelection!=0)
{
//Show user selections to analyze
return ("1. Theme");
return ("2. Character Analysis");
return ("3. Significance");
return ("4. Plot Enhancement");
return ("5. Speaker");
analysisSelection = in.nextInt ();

//Display the analysis
if (analysisSelection <= 5 || analysisSelection > 0)
{
return (quotes [quoteSelection-1][analysisSelection]);
}
}
}
}

这是 Swing 课。它只包含一个按钮,我似乎无法使其工作。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.*;

public class Swing
{
private JLabel label;
public Swing()
{
JFrame frame = new JFrame ("FRAME SAMPLE");
frame.setSize(500, 500);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JButton cButton = new JButton("The Crucible");//The JButton name.
frame.add(cButton);//Add the button to the JFrame.
cButton.addActionListener(new CrucibleButtonListener(this));
}
}

主类:

public class Main
{
public static void main (String [] args)
{
new Swing();
}
}

最佳答案

您正试图将不基于对象或类的简单线性控制台程序直接转换为事件驱动的 Swing GUI 应用程序,直白地说,这永远行不通;一个人的逻辑不能硬塞进另一个人的逻辑中。您的首要任务是将您的代码更改为具有一个或多个类的真正 OOP 程序,并将所有用户交互代码与程序逻辑代码分开。完成后,您将能够更轻松地在 GUI 程序中使用您的代码。

附录:在 try/catch block 中调用 getQuotes

  try {
String quotes = getQuotes();
g.updateTextField(quotes);
} catch (IOException e1) {
e1.printStackTrace();
// or perhaps better would be to display a JOptionPane telling the user about the error.
}

关于java - 我可以使用 BufferedReader 并在 actionListener 类中创建一个数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4769923/

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