2013-05-25 51 views
1

我如何設置全寬到JTextArea? 我試圖獲得JTextArea的最大可能的大小,並設置它,但它只是做一個JTextArea更小。我如何設置全寬到JTextArea?

JTextArea textArea = new JTextArea(10, ?); 
+2

它取決於您使用的佈局。 – duffy356

回答

2

您不應該明確地這樣做。達到此目的的最佳方法是使用正確的佈局管理器。例如,如果你把文本區域的BorderLayout中心將得到最大尺寸:

Panel p = new Panel(); 
p.setLayout(new BorderLayout()); 
p.add(new TextArea(), BorderLayout.CENTER); 
+0

這就是我所做的,但它顯示在最小尺寸。 'Panel p = new Panel(); p.setLayout(new BorderLayout()); JScrollPane scrollingArea = new JScrollPane(new JTextArea());添加(滾動區域,BorderLayout.CENTER);' – phpnuker

1

你必須把JTextArea中在JScrollPane。

下面是一個工作示例。

import java.awt.Dimension; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

public class SimpleFrame implements Runnable { 

    @Override 
    public void run() { 
     JFrame frame = new JFrame("JScroll Pane Test"); 

     JTextArea txtNotes = new JTextArea(); 
     txtNotes.setText("Hello World"); 
     JScrollPane scrollPane = new JScrollPane(txtNotes); 
     frame.add(scrollPane); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(new Dimension(800, 600)); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new SimpleFrame()); 
    } 

}