Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

java - Add labels inside JscrollPane inside loop

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I made some corrections to make you code work, and added comments to explain what needs to be done:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test extends JFrame{ //see https://www.javatpoint.com/java-naming-conventions

    private JLabel[] titles;
    private JLabel[] descriptions;
    private JPanel [] panels;
    private JScrollPane jScrollPane1;
    private JPanel bigPanel;
    private final static int NUM_OF_RESULTS =10;

    public Test() {

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setSize(1000,500);             no need to set size. use preferred sizes
        //jScrollPane1.setSize(1000, 500);    and layouts. see following comments

        bigPanel = new JPanel();
        //set layout to
        GridLayout layout = new GridLayout(NUM_OF_RESULTS, 0);
        bigPanel.setLayout(layout);
        jScrollPane1 = new JScrollPane(bigPanel);
        getContentPane().add(jScrollPane1);

        requetezQuery();
        pack(); //see https://stackoverflow.com/questions/22982295/what-does-pack-do
        setVisible(true); //set visible typically comes last
    }
    public void requetezQuery(){

        titles = new JLabel[NUM_OF_RESULTS];
        descriptions = new JLabel[NUM_OF_RESULTS];
        panels = new JPanel[NUM_OF_RESULTS];

        for(int i = 0; i<NUM_OF_RESULTS; i++){

            String title="Test Title "+i;
            String resume="Test Resume "+i;

            titles[i]= new JLabel();
            descriptions[i]= new JLabel();
            panels[i]= new JPanel();
            panels[i].setPreferredSize(new Dimension(250, 50));
            panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
            titles[i].setText(title);
            descriptions[i].setText(resume);
            titles[i].setForeground(Color.red);
            descriptions[i].setForeground(Color.red);
            panels[i].add(titles[i]);
            panels[i].add(descriptions[i]);
            bigPanel.add(panels[i],i, 0);
        }
    }

    public static void main(String args[]){
        new Test();
    }
}

The code can be further improved. I tried to minimize changes hoping that it makes it easier for you to see the changes.
Don't hesitate to ask for clarifications as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...