JScrollPane中的JTextArea将不会显示

问题描述:

我的GUI一直存在很大问题。我的第一个问题是,当我按下一个按钮导致我的JTextArea显示一个字符串时,他的文本区域发生变化,从而推动我的GUI中的所有按钮和其他所有内容。我尝试了很多解决方案,但没有成功JScrollPane中的JTextArea将不会显示

我把我的文本区域放在一个可能或可能没有工作的滚动面板中。我不知道,因为没有文字显示在滚动面板中。有人可以看看我的代码,并告诉我我做错了什么?

感谢

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class myClass extends JFrame implements ActionListener{ 
    public JButton picture = new JButton(); 
    public JTextArea description = new JTextArea(10,30); 
    public JButton B1 = new JButton(); 
    public JTextArea C1 = new JTextArea(); 

    public myClass (String STP){ 

     super(STP); 
     makeGUI(); 
    } 

    public static void main(String[] args){ 
     myClass test = new myClass("story"); 
    } 

    public void actionPerformed(ActionEvent event){ 
     Object source = event.getSource(); 
     if (source==B1){ 
      description.setText("hello world"); 
      C1.setText("hello world"); 
     } 
    } 

    public void makeGUI(){ 

     JScrollPane scroll = new JScrollPane(description); 
     JPanel pane = new JPanel(new GridBagLayout()); 
     Container con = this.getContentPane(); 
     setBounds(50,50,600,600); //(x,-y,w,h) from north west 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // picture 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill=GridBagConstraints.NONE; 
     gbc.ipadx=260; 
     gbc.ipady=310; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=0; 
     gbc.gridy=0; 
     gbc.gridwidth=2; 
     gbc.gridheight=3; 
     pane.add(picture,gbc); 

     // button 1 
     B1.addActionListener(this); 
     gbc.fill=GridBagConstraints.NONE; 
     gbc.ipadx=0; 
     gbc.ipady=10; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=0; 
     gbc.gridy=3; 
     gbc.gridwidth=1; 
     gbc.gridheight=1; 
     pane.add(B1,gbc); 

     //caption 1 
     gbc.fill=GridBagConstraints.HORIZONTAL; 
     gbc.ipadx=0; 
     gbc.ipady=30; 
     gbc.insets=new Insets(5,5,5,5); 
     gbc.gridx=1; 
     gbc.gridy=3; 
     gbc.gridwidth=3; 
     gbc.gridheight=1; 
     C1.setEditable(false); 
     C1.setLineWrap(true); 
     C1.setWrapStyleWord(true); 
     pane.add(C1,gbc); 

     // description !this is the part im having a problem with! 
     gbc.fill=GridBagConstraints.BOTH; 
     gbc.ipadx=100; 
     gbc.ipady=170; 
     gbc.insets=new Insets(10,10,10,0); 
     gbc.gridx=2; 
     gbc.gridy=0; 
     gbc.gridwidth=2; 
     gbc.gridheight=1; 
     description.setEditable(false); 
     description.setLineWrap(true); 
     description.setWrapStyleWord(true); 

     scroll.add(description); 
     pane.add(scroll,gbc); 

     con.add(pane); 

     setVisible(true); 

    } 
} 

尝试在左下角按下的小按钮。你应该在两个文本区域看到文本,但只有一个正常。

+1

为什么你这样做'new JScrollPane(description);'_and_ this'scroll.add(description);'? – 2014-10-16 14:00:39

+0

对于'SSCCE'为+1。 – Reimeus 2014-10-16 14:14:48

+0

可能的重复[为什么我的表不可见时它是在JScrollPane?](http://*.com/questions/22593867/why-is-my-table-not-visible-when-its-in-a -jscrollpane) – 2014-10-22 12:51:04

这是一个常见的错误。所以要详细说明solution by Reimeus有点:

JScollPane是一个复杂的组件,它包含几个子组件。它可能包含Viewport和可能的JScrollBar s,如How to use Scroll Panes教程中的this image所示。这些组件安排了一个特殊的布局管理器(特别是,与ScrollPaneLayout)。

当您只需拨打scrollPane.add(componentThatShouldBeScrolled)时,新组件可能会替换现有组件之一,或者无法预料地拧紧整个布局。

,而无需手动添加用于滚动窗格的内容,但通常将它传递给JScollPane构造:

JScrollPane scrollPane = new JScrollPane(componentThatShouldBeScrolled); 

另外,如果你取代“应滚动组件”,你可以请致电

scrollPane.setViewportView(componentThatShouldBeScrolled); 

删除替换JScrollPane组件的ViewPortView的声明。

scroll.add(description); 

JTextAreadescription已经被设置为口的视图。