首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不知道我的密码有什么问题。Java

不知道我的密码有什么问题。Java
EN

Stack Overflow用户
提问于 2011-12-28 07:18:35
回答 2查看 470关注 0票数 0

我正试着制作一个带有图像的组合框。我知道这个错误:

注意: C:\Users\Kyle\Desktop\TUSEG\Program\ProductDemo.java使用未经检查或不安全的操作。注意:有关详细信息,请使用-Xlint:unchecked重新编译。

不管怎么说,当它试图拉出一张照片时,我每次都会得到这个:

C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360控制器(PC)

找不到文件.jpg

找不到文件: C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Wireless激光鼠标5000.jpg

这条路绝对是正确的。我不知道我的问题是什么。如果有人能帮我看看这个吗?

代码语言:javascript
复制
    package components;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    public class ProductDemo extends JPanel
                      implements ActionListener {
JLabel picture;

public ProductDemo() {
    super(new BorderLayout());

    String pMS[] = new String[23];
    pMS[0] = ("LifeChat LX-3000");
    pMS[1] = ("LifeChat ZX-6000");
    pMS[2] = ("Wireless Notebook Presenter 8000");
    pMS[3] = ("Arc Mouse");
    pMS[4] = ("Bluetooth Notebook Mouse 5000");
    pMS[5] = ("Explorer Mouse");
    pMS[6] = ("Explorer Mini Mouse");
    pMS[7] = ("Sidewinder X8 Mouse");
    pMS[8] = ("Wireless Laser Mouse 5000");
    pMS[9] = ("Wireless Mobile Mouse 3000");
    pMS[10] = ("Wireless Mobile Mouse 6000");
    pMS[11] = ("Arc Keyboard");
    pMS[12] = ("Bluetooth Mobile Keyboard 6000");
    pMS[13] = ("Sidewinder X4 Keyboard");
    pMS[14] = ("Sidewinder X6 Keyboard");
    pMS[15] = ("Ergonomic Desktop 7000");
    pMS[16] = ("Wireless Desktop 3000");
    pMS[17] = ("Wireless Laser Desktop 6000 v2.0");
    pMS[18] = ("Wireless Media Desktop 1000");
    pMS[19] = ("Windows Server 2008 Enterprise");
    pMS[20] = ("Notebook Cooling Base");
    pMS[21] = ("Xbox 360 Controller (PC)");
    pMS[22] = ("Xbox 360 Controller");
    Arrays.sort(pMS);

    //Indices start at 0, so 4 specifies the last index of the product.
    JComboBox msList = new JComboBox(pMS);
    msList.setSelectedIndex(22);
    msList.addActionListener(this);

    //Set up the picture.
    picture = new JLabel();
    picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
    picture.setHorizontalAlignment(JLabel.CENTER);
    updateLabel(pMS[msList.getSelectedIndex()]);
    picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

    //height + width
    picture.setPreferredSize(new Dimension(100, 100));

    //Lays out the demo.
    add(msList, BorderLayout.PAGE_START);
    add(picture, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String pMS = (String)cb.getSelectedItem();
    updateLabel(pMS);
}

protected void updateLabel(String name) {
    ImageIcon icon = createImageIcon("C:\\Users\\Kyle\\Desktop\\TUSEG\\Program\\images\\microsoft\\" + name + ".jpg");
    picture.setIcon(icon);
    if (icon != null) {
        picture.setText(null);
    }
    else {
        picture.setText("Image not found");
    }
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = ProductDemo.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ProductDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new ProductDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-28 07:30:49

您似乎混淆了基于File的路径,比如..。

代码语言:javascript
复制
C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg

..with用于getResource(String)的相对引用,如:

代码语言:javascript
复制
"images/microsoft/Xbox 360 Controller (PC).jpg"

getResource()方法需要使用正斜杠的字符串,即相对于应用程序的运行时类路径(因此,images目录等通常会添加到Jar)。要确保它从任何包的类中运行,请在字符串前面加上/

代码语言:javascript
复制
"/images/microsoft/Xbox 360 Controller (PC).jpg"

getResource()方法将返回一个URL,所以一定要使用与URL兼容的构造函数。

票数 3
EN

Stack Overflow用户

发布于 2011-12-28 07:31:36

决定是否要从文件系统或应用程序的类路径加载图像。

如果从文件系统中,使用文件IO加载图标,或者使用以文件名为参数的构造函数:

代码语言:javascript
复制
ImageIcon icon = new ImageIcon("c:\\....jpg");

如果从类路径开始,那么路径是一个/分离的路径,从类路径的根开始,并且图像应该与类存储在同一个目录/jar中(或者存储在类路径中的另一个目录/jar中):

代码语言:javascript
复制
ImageIcon icon = new ImageIcon(ProductDemo.class.getResource("/path/to/image.jpg"));

http://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.htmlhttp://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8653015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档