我正在尝试制作一个弹出图像的组合框。我收到这个错误:
注意:C:\ Users \ Kyle \ Desktop \ TUSEG \ Program \ ProductDemo.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:取消选中以获取详细信息。
无论如何,当它试图拉起一张照片时,我每次都会这样:
找不到文件:C:\ Users \ Kyle \ Desktop \ TUSEG \ Program \ images \ microsoft \ Xbox 360 Controller(PC).jpg
找不到文件:C:\ Users \ Kyle \ Desktop \ TUSEG \ Program \ images \ microsoft \ Wireless Laser Mouse 5000.jpg
路径绝对是正确的。我不确定我的问题是什么。如果有人可以看看这个并帮助我?
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();
}
});
}
}
答案 0 :(得分:3)
您似乎混淆了基于File
的路径,如..
C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg
..带有用于getResource(String)
的相对参考,例如:
"images/microsoft/Xbox 360 Controller (PC).jpg"
getResource()
方法需要一个使用正斜杠的字符串,它相对于应用程序的运行时类路径(所以images
目录等通常会被添加到一个Jar中)。要确保它适用于任何包中的类,请在字符串前加/
。
"/images/microsoft/Xbox 360 Controller (PC).jpg"
getResource()
方法将返回URL
,因此请务必使用与URL兼容的构造函数。
答案 1 :(得分:1)
确定是要从文件系统还是从应用程序的类路径加载图像。
如果从文件系统,使用文件IO加载图标,或使用文件名作为参数的构造函数:
ImageIcon icon = new ImageIcon("c:\\....jpg");
如果来自类路径,那么路径是从类路径的根开始的/分隔路径,并且图像应该存储在与类相同的目录/ jar中(或者存储在其中的另一个目录/ jar中)类路径):
ImageIcon icon = new ImageIcon(ProductDemo.class.getResource("/path/to/image.jpg"));
请参阅http://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html和http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29