在循环内的JscrollPane中添加标签 [英] Add labels inside JscrollPane inside loop
问题描述
我有一个循环,该循环生成一些Title和Description(它们是字符串值),并且使Labels包含这两个字符串,我想将它们添加到JScrollPane
,但是由于某种原因,我的代码无法正常工作,我'现在没有任何错误,没有任何项目被添加到滚动窗格,这是我的代码:
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();
}
}
我试图System.out.println
标题和简历变量及其工作,所以问题出在不是他们.
I tried to System.out.println
the titles and resume variables and its working, so the problem isn't from them.
推荐答案
我进行了一些更正,以使您的代码工作正常,并添加了注释以解释需要做的事情:
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.
这篇关于在循环内的JscrollPane中添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!