将一个Jpanel的内容添加到另一个Jpanel中-Java
我有一个方法,它返回一个jpanel,其布局集类似于图像旋转木马。我已经检查过了,当直接应用此代码时,它可以在单个将一个Jpanel的内容添加到另一个Jpanel中-Java,java,swing,Java,Swing,我有一个方法,它返回一个jpanel,其布局集类似于图像旋转木马。我已经检查过了,当直接应用此代码时,它可以在单个JPanel上正常工作。但同样的布局正在多个地方使用。因此,我想创建一个通用方法来设置此布局并返回jPanel。这就是我写的方法: public static JPanel loadImages(String Location) throws IOException{ JPanel ImagePanel = new JPanel(); File directory
JPanel
上正常工作。但同样的布局正在多个地方使用。因此,我想创建一个通用方法来设置此布局并返回jPanel
。这就是我写的方法:
public static JPanel loadImages(String Location) throws IOException{
JPanel ImagePanel = new JPanel();
File directory = new File(Location);
ImageIcon image;
String[] imageFiles = directory.list();
DefaultListModel model = new DefaultListModel();
JLabel lab1=new JLabel();
JCheckBox chkbox ;
ImagePanel.setLayout(new GridBagLayout());
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint = new GridBagConstraints();
image = new ImageIcon(imageFiles[ii]);
lab1 = new JLabel(image);
constraint.gridx = ii;
constraint.gridy =0;
ImagePanel.add(lab1,constraint);
}
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint1 = new GridBagConstraints();
constraint1.anchor = GridBagConstraints.SOUTH;
chkbox = new JCheckBox(imageFiles[ii]);
constraint1.gridx = ii;
constraint1.gridy =1;
ImagePanel.add(chkbox, constraint1);
}
return ImagePanel;
}
但是什么也没有显示。是因为没有设置布局吗 不要在JScrollPane上使用add 尝试:
jScrollPane3使用什么样的布局?我还没有设置布局。所以我猜测默认布局..简单回答,这就是JScrollPanes的工作原理。参见和
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jScrollPane3.add(loadImages("C://users//images"));
jScrollPane3.revalidate();
jScrollPane3.repaint();
}
jScrollPane3.setViewportView(loadImages("C://users//images"));