簡體   English   中英

在JTextArea中打開和顯示文件數據

[英]Open and Display File Data in JTextArea

我遇到了一個我認為非常簡單的程序的問題。 我只想使用GUI,單擊和按鈕以文本文件形式顯示數據。 我似乎很近,但是遇到了一個我不明白的問題。 如果按原樣保留代碼,則會出現錯誤,即未聲明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();
}
}

OpenFile()方法將引發IOException,但永遠不會捕獲它。

我做了一些修改:

  1. highScores可以聲明為列表(因此您不必提前給出行數)

  2. 異常被捕獲

  3. 換行符是“ \\ n”,而不是“ / n”

可以進行更多修改,但這應該可行:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM