[英]Can someone tell me where is it picking images from?
package demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*`enter code here`
* ComboBoxDemo.java uses these additional files:`enter code here`
* images/Bird.gif
* images/Cat.gif
* images/Dog.gif
* images/Rabbit.gif
* images/Pig.gif
*/
public class Demo extends JPanel
implements ActionListener {
JLabel picture;
@SuppressWarnings({ "rawtypes", "unchecked" })
public Demo() {
super(new BorderLayout());
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select the item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(4);
petList.addActionListener(this);
//Set up the picture.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
updateLabel(petStrings[petList.getSelectedIndex()]);
picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//The preferred size is hard-coded to be the width of the
//widest image and the height of the tallest image + the border.
//A real program would compute this.
picture.setPreferredSize(new Dimension(177, 122+10));
//Lay out the demo.
add(petList, 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 petName = (String)cb.getSelectedItem();
updateLabel(petName);
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("images/" + name + ".png");
picture.setIcon(icon);
picture.setToolTipText("A drawing of a " + name.toLowerCase());
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 = Demo.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("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Demo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
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();
}
});
}
}
这是我从oracle那里获得的代码,但是我不知道从何处拾取图像,因为每次我创建带有图像的源文件夹时,它仍然不会拾取它。 或者,如果您能告诉我代码的哪一部分正在拾取图像? 我的工作需要使用相同的代码。
在Demo.java
所在的文件夹中创建一个名为images
的文件夹。 并将您的图像放置在那里。 根据您的代码,图像名称为“ Bird.png”,“ Cat.png”,“ Dog.png”,“ Rabbit.png”,“ Pig.png”。
所以您的项目结构是这样的:
demo
src
└───demo
│ Demo.java
└───images
Bird.png
Cat.png
Dog.png
Rabbit.png
Pig.png
ImageIcon icon = createImageIcon("images/" + name + ".png");
该行提供的图像路径为images/name.png
, name
值可以在{"Bird", "Cat", "Dog", "Rabbit", "Pig"}
picture.setIcon(icon);
上一行将icon
设置为JLabel
picture
。
在项目的根目录中创建一个名为images
的文件夹,并将所有.png
图像放入images
文件夹中。
请参阅: http : //www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading
我终于找到了问题所在,但我不明白为什么他们用Oracle编写代码的方式行不通。
您需要转到项目所在的文件夹。 您将看到:bin images src
images文件夹中可能包含所有预期的图像文件,但是程序无法以某种方式获取它们。 将gif文件复制到保存bin,图像和src的文件夹中。
现在,您将看到:bin images src Bird.gif Cat.gif等。
现在返回并更改Oracle的代码:
// ImageIcon图标= createImageIcon(“ images /” +名称+“ .gif”); //这种方式行不通。
至
ImageIcon icon = createImageIcon(name + ".gif");
如果要调试程序在何处提取文件,可以使用以下步骤创建File
对象并请求其绝对路径。
System.out.println(new File("images/Bird.png").getAbsolutePath()); // C:\... or /...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.