JScrollPane不会出现在JTextArea上 [英] JScrollPane don't appear on JTextArea

查看:73
本文介绍了JScrollPane不会出现在JTextArea上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have next problem, JScrollPane don't appear on JTextArea and I don't know why? 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. 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;    
    }
    
}

我先谢谢你!

推荐答案

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

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

Look at all the code generated by your IDE when you use a GroupLayout.

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

There is all kinds of complex code to specify the constraints used by the GroupLayout and then the addComponent(...) method is used. Because of this complexity the GroupLayout is typically only used by IDE generated code.

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

You can't simply use the add(...) method to add components to a group layout.

我们的建议是不要使用IDE生成布局代码.这样您就可以完全控制自己.

Our recommendation would be to NOT use the IDE to generate your layout code. Then you are in full control.

布局管理器上阅读Swing教程以进行工作入门示例.

Read the Swing tutorial on Layout Managers for working examples to get you started.

这篇关于JScrollPane不会出现在JTextArea上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆