JScrollPane不会出现在JTextArea

问题描述

下一个问题是,JScrollPane不会出现在JTextArea上,我也不知道为什么吗? 我尝试了很多方法,但没有尝试,它不想告诉我!我把部分代码放在下面。 所有内容正确显示,JFame,JTextArea,JTextArea中的文本,所有这些都没有JScrollPane。 拜托,有人可以帮我吗?

package pachet;

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;



public class Design {
    private final JFrame x;
    
    private final JPanel panou_rezultate=new JPanel();
    
    private final JLabel aziL=new JLabel("Azi");
    private final JLabel saptamanaL=new JLabel("Ultimile 7 zile");
    private final JLabel lunaL=new JLabel("Ultimile 30 zile");
    private final JLabel totalL=new JLabel("De la inceput");
        
    private final JTextArea aziArea=new JTextArea(30,60);
    private final JTextArea saptamanaArea=new JTextArea(30,60);
    private final JTextArea lunaArea=new JTextArea(30,60);
    private final JTextArea totalArea=new JTextArea(30,60);
    
    private final JScrollPane totalScrol=new JScrollPane(totalArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //here it is created scroll

    public Design(JFrame x) {
        this.x = x;
        functie(); 
    }
    
    
    private void functie(){
       
        x.add(panou_rezultate,BorderLayout.CENTER);
       
        panou_rezultate.setLayout(metoda());
        
        panou_rezultate.add(totalScrol); //here is added scroll to panel
                
        PrelucrareDate y=new PrelucrareDate();
        aziArea.setText(y.getAziP());
        String abc="";
        for(int i=0; i<1000; i++){
            abc=abc+"she is beautiful\n";
        }
        totalArea.setText(abc);   //totalArea text area is filled by many sentences,so scroll must appear   
        
       
    }
   
   
    public GroupLayout metoda(){
        
        GroupLayout gl= new GroupLayout(panou_rezultate);
        panou_rezultate.setLayout(gl);
        
        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);       
        
        GroupLayout.SequentialGroup sg=gl.createSequentialGroup();
        
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(aziL)
                    .addComponent(aziArea)
                    );
            sg.addPreferredGap(RELATED,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE);
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(saptamanaL)
                    .addComponent(saptamanaArea)
                    );
            sg.addPreferredGap(RELATED,Short.MAX_VALUE);
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(lunaL)
                    .addComponent(lunaArea)
                    );
            sg.addPreferredGap(RELATED,Short.MAX_VALUE);
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(totalL)
                    .addComponent(totalArea)
                    );
            sg.addPreferredGap(RELATED,Short.MAX_VALUE);
             
        gl.setHorizontalGroup(sg);
        
        
        
        GroupLayout.ParallelGroup pg_etichete=gl.createParallelGroup(BASELINE);
        GroupLayout.ParallelGroup pg_arii_text=gl.createParallelGroup();
        
        
            pg_etichete.addComponent(aziL);
            pg_etichete.addComponent(saptamanaL);
            pg_etichete.addComponent(lunaL);
            pg_etichete.addComponent(totalL);
            
            pg_arii_text.addComponent(aziArea);
            pg_arii_text.addComponent(saptamanaArea);
            pg_arii_text.addComponent(lunaArea);
            pg_arii_text.addComponent(totalArea);
        

        GroupLayout.SequentialGroup sgv=gl.createSequentialGroup(); //secvential grup pe verticala
        
        sgv.addPreferredGap(RELATED,Short.MAX_VALUE);
        sgv.addGroup(pg_etichete);
        sgv.addGroup(pg_arii_text);
        sgv.addPreferredGap(RELATED,25,25);
        
        gl.setVerticalGroup(sgv);
        
    return gl;    
    }
    
}

我先谢谢你!

解决方法

    panou_rezultate.setLayout(metoda());
    panou_rezultate.add(totalScrol); //here is added scroll to panel

使用GroupLayout时,请查看IDE生成的所有代码。

可以使用各种复杂的代码来指定GroupLayout使用的约束,然后使用addComponent(...)方法。由于这种复杂性,GroupLayout通常仅由IDE生成的代码使用。

您不能简单地使用add(...)方法将组件添加到组布局中。

我们的建议是不要使用IDE生成布局代码。然后,您完全可以控制。

阅读Layout Managers上的Swing教程,以获取入门的实用示例。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...