如何在java中循环并显示拆分字符串中的元素?

时间:2013-03-29 09:57:26

标签: java string jtextarea

我必须为大学做一个项目,从外部文本文件中读取数据,并有一个下一个/上一个/退出按钮来循环遍历各个文本块。这是我与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();
}

}

1 个答案:

答案 0 :(得分:0)

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

尝试添加

hero = output.split("@");

我怀疑你的英雄阵列是否为空。