I have a loop that generates some Titles and Description which are String values and I made Labels to contains theses two strings, I want to add these to a JScrollPane
, but for some reason my code isn't working, I'm not getting any error now, no item is being added to the scroll pane, here's my code:
package testa;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class test extends JFrame {
JLabel[] titles;
JLabel[] descriptions;
JPanel[] panels;
JScrollPane jScrollPane1 = new JScrollPane();
JPanel bigPanel = new JPanel();
public test() {
this.setLocationRelativeTo(null);
this.setSize(1000, 500);
this.jScrollPane1.setSize(1000, 500);
this.getContentPane().add(this.jScrollPane1);
this.setVisible(true);
requetezQuery();
}
public void requetezQuery() {
int resultsList = 10;
this.titles = new JLabel[resultsList];
this.descriptions = new JLabel[resultsList];
this.panels = new JPanel[resultsList];
for (int i = 0; i < resultsList; i++) {
String title = "Test Title " + i;
String resume = "Test Resume " + i;
this.titles[i] = new JLabel();
this.descriptions[i] = new JLabel();
this.panels[i] = new JPanel();
this.panels[i].setLayout(new FlowLayout());
this.titles[i].setText(title);
this.descriptions[i].setText(resume);
this.titles[i].setForeground(Color.red);
this.descriptions[i].setForeground(Color.red);
this.panels[i].add(this.titles[i]);
this.panels[i].add(this.descriptions[i]);
this.bigPanel.add(panels[i]);
}
this.jScrollPane1.add(this.bigPanel);
}
public static void main(String args[]) {
test a = new test();
}
}
I tried to System.out.println
the titles and resume variables and its working, so the problem isn't from them.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…