简体   繁体   English

JScrollPane 不会出现在 JTextArea 上

[英]JScrollPane don't appear on JTextArea

I have next problem, JScrollPane don't appear on JTextArea and I don't know why?我有下一个问题,JScrollPane 没有出现在 JTextArea 上,我不知道为什么? I tried in many ways but nothing, it don't want to show me!我尝试了很多方法但没有任何效果,它不想显示给我! I put below a part of the code.我把代码的一部分放在下面。 All appears correctly, JFame, JTextArea, text inside JTextArea, all without JScrollPane.一切都正确显示,JFame、JTextArea、JTextArea 内的文本,都没有 JScrollPane。 Please, can somebody help me?拜托,有人可以帮我吗?

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, 
                        GroupLayout.DEFAULT_SIZE, 
                        Short.MAX_VALUE);
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(lunaL)
                    .addComponent(lunaArea)
                    );
            sg.addPreferredGap(RELATED, 
                        GroupLayout.DEFAULT_SIZE, 
                        Short.MAX_VALUE);
            sg.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)  //de schimbat
                    .addComponent(totalL)
                    .addComponent(totalArea)
                    );
            sg.addPreferredGap(RELATED, 
                        GroupLayout.DEFAULT_SIZE, 
                        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, 
                        GroupLayout.DEFAULT_SIZE, 
                        Short.MAX_VALUE);
        sgv.addGroup(pg_etichete);
        sgv.addGroup(pg_arii_text);
        sgv.addPreferredGap(RELATED,25,25);
        
        gl.setVerticalGroup(sgv);
        
    return gl;    
    }
    
}

I thank you in advance!我提前谢谢你!

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

Look at all the code generated by your IDE when you use a GroupLayout .查看使用GroupLayout时 IDE 生成的所有代码。

There is all kinds of complex code to specify the constraints used by the GroupLayout and then the addComponent(...) method is used.有各种复杂的代码来指定GroupLayout使用的约束,然后使用addComponent(...)方法。 Because of this complexity the GroupLayout is typically only used by IDE generated code.由于这种复杂性, GroupLayout通常仅由 IDE 生成的代码使用。

You can't simply use the add(...) method to add components to a group layout.您不能简单地使用 add(...) 方法将组件添加到组布局。

Our recommendation would be to NOT use the IDE to generate your layout code.我们的建议是不要使用 IDE 来生成您的布局代码。 Then you are in full control.然后你就完全控制了。

Read the Swing tutorial on Layout Managers for working examples to get you started.阅读有关布局管理器的 Swing 教程,了解工作示例以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM