2012-02-15 89 views
0

我有JTextAreaJPanel我想使用JScrollPane。我正在使用GridBagLayout。當我運行它似乎框架爲JScrollPane騰出空間,但它不顯示,任何幫助將不勝感激。我一直在試圖研究docs.oracle頁面,並在這裏Add JScrollPane to a JPanel,但由於某種原因,它拒絕出現。JFramePlay JTextArea中JPanel JFrame中的問題

final JTextArea test= new JTextArea(5,30); 
test.setLineWrap(true); 
test.setWrapStyleWord(true); 
test.setEditable(false); 
JScrollPane spane = new JScrollPane(test); 
spane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);   

JFrame frame = new JFrame(); 

frame.setSize(800, 250); 
frame.setTitle("test1"); 
frame.setLocation(300, 300); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setResizable(false); 
frame.getContentPane().add(spane); 

GridBagConstraints k = new GridBagConstraints(); 
k.gridx = 4; 
k.gridy = 5; 
a.setConstraints(spane,k); 
container.add(spane); 
+0

如果我添加spane.setViewportView(panel);一個面板顯示出來,但面板應該用於試圖讓文字更大以填充文本而不是保持相同大小並讓我使用滾動窗格的JTextArea – Faud 2012-02-15 23:42:52

+0

爲了更快地獲得更好的幫助,請發佈[SSCCE](http:///sscce.org/)。 – 2012-02-15 23:52:08

回答

1

您的可變容器是JPanel?我想你忘了調用add()方法。但下面是我的建議代碼。

import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

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

public class MyScrollPane extends JPanel 
{ 

    public MyScrollPane() 
    { 
     GridBagConstraints k = new GridBagConstraints(); 
     k.gridx = 4; 
     k.gridy = 5; 



     final JTextArea test= new JTextArea(5, 30); 
     test.setLineWrap(true); 
     test.setWrapStyleWord(true); 
     test.setEditable(false); 

     JScrollPane spane = new JScrollPane(test); 
     spane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

     GridBagLayout gbl = new GridBagLayout(); 
     gbl.setConstraints(spane,k); 

     JPanel panel = new JPanel(gbl);  
     panel.add(spane); 
     add(panel); 

    } 


    private static void createAndShowGUI() 
    { 


     JFrame frame = new JFrame(); 
     frame.setSize(800, 250); 
     frame.setTitle("test1"); 
     frame.setLocation(300, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.getContentPane().add(new MyScrollPane()); 



     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() 
      { 
       createAndShowGUI();    
      } 
     }); 
    } 

} 
+0

謝謝,問題最終導致在GridBagLayout中我添加了帶有自己約束的TextArea,所以我對JScrollPanel和JTextArea的約束條件有所限制。一旦我從JTextArea中刪除所有格式,然後只用GridBagLayout中的JScrollPane替換JTextArea,它就像魅力一樣。當我看到您只爲spane添加gbl.SetConstraints而不是用於測試時,您的文章幫助了很多。謝謝! – Faud 2012-02-16 04:47:55

+0

沒問題,玩得開心編碼:-) – Jasonw 2012-02-16 05:01:39

0

我刪除了代碼的最後五行並將其更改了一點。我工作得很好。

public class MainFrame extends JFrame { 



private JTextArea test = new JTextArea(5, 30); 
private JScrollPane spane; 

public MainFrame() { 

    this.setSize(800, 250); 
    this.setTitle("test1"); 
    this.setLocation(300, 300); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setResizable(false); 

    test.setLineWrap(true); 
    test.setWrapStyleWord(true); 
    test.setEditable(false); 
    spane = new JScrollPane(test); 
    spane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

    this.getContentPane().add(spane); 

} 
+0

感謝您的回答,我需要GridBagConstraints,因爲我使用了GridBagLayout。 – Faud 2012-02-16 02:44:30

+0

您沒有使用GridBagLayout。您將滾動窗格添加到框架的內容窗格。默認情況下,內容窗格使用BorderLayout。如果你想使用一個GridBagLayout,那麼你應該閱讀[如何使用GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)的工作示例和解釋如何使用限制。 – camickr 2012-02-16 04:35:40