2011-04-05 101 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);