[英]Open and Display File Data in JTextArea
I have run into a problem with what I thought was a pretty simple program. 我遇到了一个我认为非常简单的程序的问题。 I simply want to use a GUI, click and button to display data in a text file. 我只想使用GUI,单击和按钮以文本文件形式显示数据。 I seem to be close but am running into a problem I do not understand. 我似乎很近,但是遇到了一个我不明白的问题。 If I leave the code the way it is here I get the error that highScores is not declared (symbol not found) for line 72. But if I try to declare highSchores the I get the error "unreported exception java.io.IOException; must be caught or declared to be thrown" for line 69. Any idea what I am doing wrong and how I can fix it? 如果按原样保留代码,则会出现错误,即未声明72行的highScores(未找到符号)。但是,如果我尝试声明highSchores,则会收到错误“未报告的异常java.io.IOException;必须被捕获或声明将被抛出”(第69行)。您知道我在做什么错以及如何解决吗?
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class tetrisScores extends JFrame{
private JPanel contentPanel;
private JButton btnSearch;
private JButton btnLoad;
private JButton btnSort;
private JRadioButton firstSort;
private JRadioButton secondSort;
private JTextField searchInput;
private JTextArea output;
private String[] highScores;
private void add (Container con, Component widget, int left, int top, int width, int height) //creates variables for bounds
{
widget.setBounds(left, top, width, height); //sets setBounds to created variables
con.add(widget); //tells program container to use widget's bounds
}
tetrisScores()
{
contentPanel=(JPanel)getContentPane();
contentPanel.setLayout(null);
btnLoad = new JButton("Load File");
add(contentPanel, btnLoad, 10, 10, 360, 40);
searchInput = new JTextField("");
add(contentPanel, searchInput, 10, 60, 240, 40);
btnSearch = new JButton("Search");
add(contentPanel, btnSearch, 260, 60, 110, 40);
firstSort = new JRadioButton("Bubble Sort");
add(contentPanel, firstSort, 20, 110, 110, 40);
firstSort = new JRadioButton("Linear Sort");
add(contentPanel, firstSort, 140, 110, 110, 40);
btnSearch = new JButton("Sort");
add(contentPanel, btnSearch, 260, 110, 110, 40);
output = new JTextArea("");
add(contentPanel, output, 10, 160, 360, 150);
output.setEditable(false);
setTitle("High Scores");
setSize(500,500);
setLocation(new Point (150,150));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
btnLoad.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent add)
{
OpenFile();
for (int j = 0; j<10;j++)
{
output.append(highScores[j] + "/n");
}
}
});
}
public String [] OpenFile() throws IOException
{
FileReader fr = new FileReader("tetrishighscore.txt");
BufferedReader scoreReader = new BufferedReader (fr);
int numbLines = 10;
String[] textData = new String [numbLines];
int i;
for (i=0; i < numbLines; i++)
{
textData[i] = scoreReader.readLine();
}
scoreReader.close();
return textData;
}
public static void main (String [] args)
{
new tetrisScores();
}
}
The OpenFile() method throws an IOException, but it is never caught. OpenFile()方法将引发IOException,但永远不会捕获它。
I have made some modifications: 我做了一些修改:
highScores could be declared as a List (so you don't have to give the number of lines in advance) highScores可以声明为列表(因此您不必提前给出行数)
the Exception is caught 异常被捕获
the line break character is "\\n", not "/n" 换行符是“ \\ n”,而不是“ / n”
Some more modifications could be made, but this should work: 可以进行更多修改,但这应该可行:
import java.awt.Component;
import java.awt.Container;
import java.util.List;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class TetrisScores extends JFrame {
private JPanel contentPanel;
private JButton btnSearch;
private JButton btnLoad;
private JButton btnSort;
private JRadioButton firstSort;
private JRadioButton secondSort;
private JTextField searchInput;
private JTextArea output;
private List<String> highScores = new ArrayList<>();
private void add(Container con, Component widget, int left, int top, int width, int height) // creates variables for
// bounds
{
widget.setBounds(left, top, width, height); // sets setBounds to created variables
con.add(widget); // tells program container to use widget's bounds
}
TetrisScores() {
contentPanel = (JPanel) getContentPane();
contentPanel.setLayout(null);
btnLoad = new JButton("Load File");
add(contentPanel, btnLoad, 10, 10, 360, 40);
searchInput = new JTextField("");
add(contentPanel, searchInput, 10, 60, 240, 40);
btnSearch = new JButton("Search");
add(contentPanel, btnSearch, 260, 60, 110, 40);
firstSort = new JRadioButton("Bubble Sort");
add(contentPanel, firstSort, 20, 110, 110, 40);
firstSort = new JRadioButton("Linear Sort");
add(contentPanel, firstSort, 140, 110, 110, 40);
btnSearch = new JButton("Sort");
add(contentPanel, btnSearch, 260, 110, 110, 40);
output = new JTextArea("");
add(contentPanel, output, 10, 160, 360, 150);
output.setEditable(false);
setTitle("High Scores");
setSize(500, 500);
setLocation(new Point(150, 150));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent add) {
try {
OpenFile();
for (int j = 0; j < highScores.size(); j++) {
output.append(highScores.get(j) + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void OpenFile() throws IOException {
FileReader fr = new FileReader("tetrishighscore.txt");
BufferedReader scoreReader = new BufferedReader(fr);
String line;
while((line = scoreReader.readLine()) != null) {
highScores.add(line);
}
scoreReader.close();
}
public static void main(String[] args) {
new TetrisScores();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.