2012-02-15 91 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