Java 扩展JPanel将不会显示
我一直在寻找答案,但其他一切都很接近,但这不是我面临的问题 因此,我的主类创建了一个新的JFrame,添加了一个面板作为内容面板,并向该内容面板添加了一个scrollpanel 现在,我创建了一些扩展JPanel类,将它们添加到滚动窗格中,只看到一个空框架 我已经检查过了,以确保确实有FTPFile的列表 以下是主要代码:Java 扩展JPanel将不会显示,java,swing,jpanel,Java,Swing,Jpanel,我一直在寻找答案,但其他一切都很接近,但这不是我面临的问题 因此,我的主类创建了一个新的JFrame,添加了一个面板作为内容面板,并向该内容面板添加了一个scrollpanel 现在,我创建了一些扩展JPanel类,将它们添加到滚动窗格中,只看到一个空框架 我已经检查过了,以确保确实有FTPFile的列表 以下是主要代码: public browser(ftpHandler _FTP) { FTP = _FTP; Panels = new ArrayList<JPanel
public browser(ftpHandler _FTP) {
FTP = _FTP;
Panels = new ArrayList<JPanel>();
frame = new JFrame("File Browser");
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(750, 500));
frame.setSize(frame.getPreferredSize());
frame.setMinimumSize(frame.getSize());
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
listFiles();
frame.pack();
frame.setVisible(true);
}
public void listFiles(){
Panels.clear();
FTPFile[] list = null;
try {
list = FTP.listFiles();
} catch (Exception e) {
e.printStackTrace();
}
for(FTPFile file : list){
fileObject FO = new fileObject(file);
Panels.add(FO);
scrollPane.add(FO);
}
scrollPane.updateUI();
}
我让它工作了,结果是我的面板设置错误,它们是这样设置的: JFrame -主面板 --滚动窗格 我正在将扩展面板添加到滚动窗格中
这似乎不起作用,所以我在scrollPane中添加了另一个面板,并开始将扩展面板添加到这个新面板中,它开始工作了 1为了更快地获得更好的帮助,请发布一个最简单、完整、可验证的示例或简短、独立、正确的示例。2获取图像的一种方法例如,热链接到中看到的图像。3见是的。。。4请学习常见的Java命名约定-例如EachWordUpperCaseClass、firstWordLowerCaseMethod、firstWordLowerCaseAttribute,除非它是常量并一致使用。添加了github源代码链接,并更新了您提到的变量/方法名。还在寻找答案吗
public class fileObject extends JPanel {
private FTPFile file;
private JLabel Label;
private ImageIcon Icon;
private int FileType;
private final int IconSize = 25;
private final Dimension panelSize = new Dimension(150, 40);
public fileObject(FTPFile FILE){
file = FILE;
FileType = file.getType();
this.setSize(panelSize);
this.setPreferredSize(panelSize);
this.setMinimumSize(panelSize);
this.setLayout(new WrapLayout());
switch (FileType){
case FTPFile.TYPE_DIRECTORY:
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/folder.png")),IconSize);
break;
case FTPFile.TYPE_FILE:
try {
String FileExtension = file.getName().substring(file.getName().lastIndexOf(".")+1);
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/"+FileExtension+".png")),IconSize);
} catch(Exception e) {
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_blank.png")),IconSize);
}
break;
case FTPFile.TYPE_LINK:
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_page.png")),IconSize);
break;
}
Label = new JLabel(file.getName(), Icon, JLabel.LEFT);
this.add(Label);
}
private ImageIcon resizeImage(ImageIcon II, int Size){
Image img = II.getImage();
BufferedImage resizedImage = new BufferedImage(Size, Size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, Size, Size, null);
g2.dispose();
return new ImageIcon(resizedImage);
}
}