问题
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;
}
}
I thank you in advance!
回答1:
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
.
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.
You can't simply use the add(...) method to add components to a group layout.
Our recommendation would be to NOT use the IDE to generate your layout code. Then you are in full control.
Read the Swing tutorial on Layout Managers for working examples to get you started.
来源:https://stackoverflow.com/questions/63667472/jscrollpane-dont-appear-on-jtextarea