2013-03-29 9 views
1

我必須做一個大學的項目,從外部文本文件中讀取數據,並有一個下一個/上一個/退出按鈕來遍歷各種文本塊。這是我第一次遇到gui,我發現顯示項目相當困難。在各種迭代中,我已經使正確的文本塊出現,但不能在它們之間循環。現在他們甚至不顯示。我有足夠的時間,所以如果我只是完全錯誤的方式,完全重建不是不可能的,任何建議都是值得歡迎的。它編譯得很好,但沒有顯示在JTextArea中。如何循環顯示java中的分割字符串中的元素?

這裏是我的代碼:

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(); 
} 

} 
+0

你得到的錯誤是什麼? –

+0

在java控制檯中?除非我不止一次地點擊下一個按鈕,否則我會遇到'數組越界'的錯誤,但目前只有足夠的數據用於0和1數組空間,所以這是可以預料的。 – Aezur

回答

0

嘗試增加

hero = output.split("@"); 

如果你的英雄數組是空的,我懷疑後

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

+0

謝謝!它的工作原理,但仍然不顯示第一個項目,直到我點擊'下一個',然後'先前'返回到第一個項目。一個問題......爲什麼它工作?該行是否將所有單獨的字符串分配給數組?我以爲'String [] hero = output.split(「@」);'反正呢? – Aezur

+0

在hero = output.split(「@」)後添加mainText.setText(hero [page]); page ++。這是因爲你在閱讀文件之前先創建英雄陣列。所以現在輸出是「」。所以分割將是空的。同樣是第一次不顯示的原因 –

+0

如果點擊幾次,您仍然會得到數組outoutofbounds異常。這是因爲你的頁面變量增加的數量大於數組的大小。您可能必須執行page = page%size來解決此問題。我相信你可以找出你必須把它放在哪裏。 –