2012-06-24 63 views
3

我想輸出大量的數據(成員資格記錄)到JTextArea中,然後將其轉儲到scrollPane中。這是代碼。scrollpane不工作

package Ginfo; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.io.*; 


public class ReviewAll extends JFrame implements ActionListener { 
    Container RA; 
    JPanel ReviewAll; 
    JTextArea output; 
    JButton back, quit; 

    Message m; 
    ConnectInfo c; 
    ObjectOutputStream oout; 
    ObjectInputStream oin; 

    public ReviewAll(ObjectOutputStream oout2, ObjectInputStream oin2, 
      Message m2, ConnectInfo a) { 
     super("Full results"); 
     m = m2; 
     c = a; 
     oout = oout2; 
     oin = oin2; 
     try { 
      oout.reset(); 
     } catch (IOException e) { 

     } 
     RA = getContentPane(); 
     BuildGUI(); 
     RA.add(ReviewAll); 
     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 

     try { 
      m.type = Message.REVIEWALL; 
      oout.writeObject(m); 
      while (!m.response.equals("End of list")) { 
       m = (Message) oin.readObject(); 
       output.append("Member: " + m.main + "\n"); 
       output.append("Invited by: " + m.invitedBy + " on " + m.when 
         + "\n"); 
       output.append("Recommended by: " + m.HDTH + "\n\n"); 
      } 
      m.response = ""; 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 

    private void BuildGUI() { 
     ReviewAll = new JPanel(); 
     output = new JTextArea(); 
     output.setPreferredSize(new Dimension(400, 400)); 
     output.setEditable(false); 
     JScrollPane scrollPane = new JScrollPane(output); 
     scrollPane 
       .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     back = new JButton("Close window"); 
     quit = new JButton("Exit Program"); 

     ReviewAll.add(scrollPane, BorderLayout.NORTH); 
     ReviewAll.add(back, BorderLayout.SOUTH); 
     ReviewAll.add(quit, BorderLayout.SOUTH); 

     back.addActionListener(this); 
     quit.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent re) { 
     if (re.getSource() == back) { 
      new WhatToDo(oout, oin, m, c); 
      dispose(); 
     } else if (re.getSource() == quit) { 
      if (oout != null) { 
       try { 
        oout.flush(); 
        oout.close(); 
        oin.close(); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
      } 
      System.exit(0); 
     } 
    } 
} 

當我這樣做,雖然,我只看到前5條記錄(足以填充我的JTextArea),不能向下滾動查看其餘。對於記錄,當發送記錄保存在list.size()時,我在服務器代碼中將m.response =「End of List」傳送到客戶端(下面列出),我知道這是正確的。我知道這是一個滾動問題。先謝謝您的幫助。

回答

5

我知道這是用滾輪的問題。

問題不在於滾動條,而在於如何設置JTextArea的大小或首選大小,以防止其正確展開。而是設置它的列和行,因爲這會告訴JScrollPane應該在其視口中查看它應該查看的行數和列數。

即,改變這種:

ReviewAll = new JPanel(); 
output = new JTextArea(); 
output.setPreferredSize(new Dimension (400,400)); 

這樣:

ReviewAll = new JPanel(); 

// ROW_COUNT & COL_COUNT are int constants defined elsewhere 
output = new JTextArea(ROW_COUNT, COL_COUNT); // using whatever numbers work best. 

// don't set the text area's preferredSize: 
// output.setPreferredSize(new Dimension (400,400)); 

當然,這將是最好避免幻數,而使用常數Row和Col計數。

+0

工作就像一個魅力!謝謝! –

+0

@MattWestlake:你很受歡迎! –