1

我必须为大学做一个项目,从外部文本文件中读取数据,并有一个下一个/上一个/退出按钮来循环浏览各种文本块。这是我第一次遇到 gui,我发现显示项目非常困难。在各种迭代中,我使正确的文本块出现,但无法循环浏览它们。现在他们甚至没有显示。我有足够的时间,所以如果我只是走错了路,完全重建不是不可能的,欢迎提出任何建议。它编译得很好,但在 JTextArea 中没有显示任何内容。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.util.Scanner;

public class MainFrame extends JFrame
{
//Main panels declaration
private JPanel main1;
private JPanel main2;
private JPanel main3;

//Label Declaration
private JLabel topBar;

//Text area declaration
private JTextArea mainText;
int page = 0;

//Buttons declaration
private JButton prev;
private JButton next;
private JButton exit;

String output = "";
String [] hero = output.split("@");


//Constructor
public MainFrame() throws IOException
{   //Declare Layouts
    GridLayout grid = new GridLayout(2,1);
    BorderLayout bor = new BorderLayout(50, 10);
    FlowLayout flo = new FlowLayout();

    //Set frame layout
    setLayout(flo);
    setSize(800, 600);
    setVisible(true);

    //Initialise main panel 1
    main1 = new JPanel();
    main1.setLayout(grid);
    add(main1);
    main1.setBorder(new EtchedBorder());


    //Initialise main panel 2
    main2 = new JPanel();
    main2.setLayout(bor);
    main1.add(main2);


    //Text area
    mainText = new JTextArea(20,50);
    JScrollPane JSP = new JScrollPane(mainText);
    main2.add(bor.CENTER,mainText);
    mainText.setBorder(new EtchedBorder());
    mainText.setEditable(false);
    mainText.setText(hero[page]);

    //File Handling
    File inputFile = new File ("ProjectInputFile.txt");
    Scanner scanner = new Scanner(inputFile);


    while(scanner.hasNextLine())
    {
        output = output + (scanner.nextLine() + "\n");
    }

    //Top Bar
    topBar = new JLabel("Hero Viewer", SwingConstants.CENTER);
    main2.add(topBar, bor.PAGE_START);

    //Initialise main panel 3
    main3 = new JPanel();
    main3.setLayout(flo);
    main1.add(main3);

    //Prev Button
    prev = new JButton("Prev");
    main3.add(prev);
    ActionListener PrevList = new PrevButton(); //Call Button Listener
    prev.addActionListener(PrevList);

    //Exit Button
    exit = new JButton("Exit!");
    main3.add(exit);
    ActionListener ExitList = new ExitButton(); //Call Button Listener
    exit.addActionListener(ExitList);

    //Next Button
    next = new JButton("Next");
    main3.add(next);
    ActionListener NextList = new NextButton(); //Call Button Listener
    next.addActionListener(NextList);
}

//Prev button event listener
public class PrevButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page = page --;
        mainText.setText(hero[page]);

    }
}

//Exit button event listener
public class ExitButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

//Next button event listener
public class NextButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page++;
        mainText.setText(hero[page]);
    }
}

//Main Method
public static void main (String [] args) throws IOException
{
    MainFrame MF = new MainFrame();
}

}
4

1 回答 1

0

while(scanner.hasNextLine())
{
    output = output + (scanner.nextLine() + "\n");
}

尝试添加

hero = output.split("@");

我怀疑你的英雄数组是否为空。

于 2013-03-29T10:16:25.970 回答