首页 文章

不会显示扩展JPanel

提问于
浏览
0

我一直在寻找答案,但其他一切都非常接近,但这不是我遇到的问题 .

所以我的主类创建了一个新的JFrame,添加了一个面板作为内容面板,并向该内容面板添加了一个滚动面板 .

现在我创建一些扩展的JPanel类,将它们添加到滚动窗格中,只看到一个空框架 .

我已经检查过确保确实有一个FTP文件列表

这是主要代码:

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();
}

和我扩展的JPanel,fileObject

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);
}
}

1 回答

  • 0

    我得到了这个工作,事实证明我的面板设置错误,他们设置如下:

    的JFrame

    --mainPanel

    ----滚动窗格

    我正在将我的扩展面板添加到scrollPane

    这似乎不起作用,所以我在scrollPane中添加了另一个面板,并开始将我的扩展面板添加到这个新面板,它开始工作!

相关问题