Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让我的JLabel图标在我的JList中呈现?_Java_Swing_Jlabel_Embedded Resource - Fatal编程技术网

Java 如何让我的JLabel图标在我的JList中呈现?

Java 如何让我的JLabel图标在我的JList中呈现?,java,swing,jlabel,embedded-resource,Java,Swing,Jlabel,Embedded Resource,因此,我有一个JList的JLabel对象,这些对象附带了一个图像和一些文本 一切看起来都很好,除了图标没有显示图像 public static void createIcons(){ char[] extension = new char[4]; Image img; try { for(int i = 0; i < FtpClient.FilesOnServer.length; i++) { for(int j = 0;

因此,我有一个
JList
JLabel
对象,这些对象附带了一个图像和一些文本

一切看起来都很好,除了图标没有显示图像

public static void createIcons(){
    char[] extension = new char[4];
    Image img;
    try {
        for(int i = 0; i < FtpClient.FilesOnServer.length; i++) {
            for(int j = 0; j < 4; j++) {
                extension[3 - j] = FtpClient.FilesOnServer[i].charAt(FtpClient.FilesOnServer[i].length() - (j+1));
                if(j == 3) {
                    System.out.println(extension);
                    String fileType = new String(extension);
                    JLabel iconLabel = new JLabel();
                    switch (fileType){
                    case ".pdf":
                        iconLabel.setIcon(new ImageIcon(FtpWindow.class.getResource("icons\\text.png")));
                        break;
                    case ".png":
                        iconLabel.setIcon(new ImageIcon(FtpWindow.class.getResource("icons\\picture.png")));
                        break;
                    case ".jpg":
                        iconLabel.setIcon(new ImageIcon(FtpWindow.class.getResource("icons\\picture.png")));
                        break;
                    case ".txt":
                        iconLabel.setIcon(new ImageIcon(FtpWindow.class.getResource("icons\\text.png")));
                        break;
                    default:
                        iconLabel.setIcon(new ImageIcon(FtpWindow.class.getResource("icons\\default.png")));
                        break;
                    }
                    iconLabel.setText(FtpClient.FilesOnServer[i]);
                    iconLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
                    iconLabel.setVerticalAlignment(SwingConstants.BOTTOM);
                    model.addElement(iconLabel);
                }
            }
        }
        T_FileLayout = new JList(model);
    } catch(Exception ex){
        ex.printStackTrace();
    }
}

public static void BuildFtpWindow(){
    P_FilesOnServer = new JPanel();
    B_UPLOAD.setBounds(10, 9, 110, 25);
    B_UPLOAD.setText("UPLOAD");
    B_DOWNLOAD.setBounds(10, 39, 110, 25);
    B_DOWNLOAD.setText("DOWNLOAD");
    P_FilesOnServer.setBounds(130, 9, 320, 288);
    P_FilesOnServer.setBackground(new Color(255, 255, 255));
    createIcons();
    P_FilesOnServer.add(T_FileLayout);
    ServerBrowser.add(P_FilesOnServer);
    ServerBrowser.add(B_DOWNLOAD);
    ServerBrowser.add(B_UPLOAD);
}
所以我有一个JLabel对象的JList

您不应该在JList中使用JLabel对象。Swing组件使用渲染器以提高效率

相反,您应该创建一个具有两个属性的自定义对象:文本、图标。然后在渲染代码中,您只需使用自定义对象的值设置渲染器的文本和图标(默认情况下是JLabel)

我看过一篇关于重写DefaultListCellRenderer的帖子,但我不知道应该为Object Value赋予它什么

对象值将是上面提到的自定义对象

因此,基本代码如下所示:

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

    MyCustomObject customObject = (MyCustomObject)value;

    setText( customObject.getText() );
    setIcon( customObject.getIcon() );

    return this;
}

感谢您的编辑。我不知道您可以这样做。如果您有足够的声誉(至少10 IIRC),您可以在您的问题中嵌入图像。其他人可以在中编辑它,这就是我所做的。@TT这个问题仍然可见吗?仍然在寻找一些帮助
.getResource(“icons\\default.png”)
应该更像
.getResource(“/icons/default.png”)
//好吧,让这个类有两个东西,一个图像和一些文本,然后有两个评估器方法。像我处理标签一样创建这些对象。将这些对象保存到向量中。将向量传递到列表。然后重写该函数。一旦我重写了该函数(我已经将类扩展到DefaultListCellRenderer),我就不必调用GetListCellRenderComponent方法correct?@chowBapoclypse,
我也不必调用GetListCellRenderComponent方法correct?
-correct,JList在需要渲染对象时为您调用此方法。感谢您的解释!!JList也有自己的操作侦听器?所以我可以在单击时从对象中获取文本。@choubapoclypse,不,JList没有ActionListener。单击JList时,将选择一个项目。然后,您可以获取所选项目,将其转换为自定义对象并获取文本。有关更多信息和工作示例,请阅读上Swing教程的部分。也许ListSelectionListener就是你要找的。事实上,将来当你发布代码时,你应该以。这只是演示问题所需的最少代码量,而且代码应该是可编译的,以便我们可以根据需要对其进行测试。这有助于我们理解问题,你(可能)会得到一个更具体的答案,而不是像这里所做的一般答案。由于您的代码不是那种格式,我认为没有必要发布更改后的代码,因为我们不知道所有代码如何协同工作的上下文。
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

    MyCustomObject customObject = (MyCustomObject)value;

    setText( customObject.getText() );
    setIcon( customObject.getIcon() );

    return this;
}