2011-04-05 98 views
0
public class Main { 

    private static void createAndShowGUI() { 

     JFrame frame1 = new JFrame("FINAL YEAR PROJECT VER 1.0"); 

     frame1.setSize(500,500); 
     frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     FlowLayout experimentLayout = new FlowLayout(); 
     experimentLayout.setAlignment(FlowLayout.CENTER); 
     frame1.setLayout(experimentLayout); 

     JTextArea jtextarea = new JTextArea(200,200); 
     JScrollPane scrollingArea = new JScrollPane(jtextarea); 

     frame1.getContentPane().add(jtextarea); 
     frame1.getContentPane().add(scrollingArea, FlowLayout.CENTER); 

     jtextarea.setText("Welcome to Document-Query Similarity System Based On Weblogs"); 



     frame1.setVisible(true); 
    } 


    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

我想在JTextarea上使用setText()方法顯示文本。但是,字符串不顯示。我錯過了什麼?不能附加字符串到jtextarea

回答

1

替換:中

JTextArea jtextarea = new JTextArea(); 

代替:

JTextArea jtextarea = new JTextArea(200, 200); 

文件爲您構造說,爭論的arent在像素:

構造一個新的空行文本控件 指定號碼行和 列。將創建一個默認模型, 並且初始字符串爲空。

+0

謝謝您的關注。你的回答解決了這個問題。謝謝你,祝你有美好的一天。 – lucian 2011-04-05 18:10:45

+2

@lucian如果這解決了您的問題,然後單擊答案左側的複選框,以便@smas獲得積分。 – I82Much 2011-04-05 18:16:54

+0

@ I82Much,確切地說,我的觀點在哪裏:) – smas 2011-04-05 21:29:17

3

它就在那裏,讓textarea變得更小。喜歡的東西:

JTextArea jtextarea = new JTextArea(20,20); 

有了它的電流的大小你不看文字。部分原因你不能滾動到文本是你添加textarea和滾動條的不正確的方式。更好的方法是:

frame1.setLayout(new BorderLayout()); 
    ... 
    //delete the addition of the textarea to the frame, you already put it in the scroll pane. 
    frame1.getContentPane().add(scrollingArea, BorderLayout.CENTER);