[英]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();
}
});
}
}
This is the code i have got from oracle but i don't know where it would need to pick images from because every time i create a source folder with images, it still wouldn't pick it up. 这是我从oracle那里获得的代码,但是我不知道从何处拾取图像,因为每次我创建带有图像的源文件夹时,它仍然不会拾取它。 Or if you could tell me which part of the code is picking the image up?
或者,如果您能告诉我代码的哪一部分正在拾取图像? I need to use same kinda code for my work.
我的工作需要使用相同的代码。
Create a folder named images
in the same folder where your Demo.java
is. 在
Demo.java
所在的文件夹中创建一个名为images
的文件夹。 And place your images there. 并将您的图像放置在那里。 The image names, according to your code, are "Bird.png", "Cat.png", "Dog.png", "Rabbit.png", "Pig.png".
根据您的代码,图像名称为“ Bird.png”,“ Cat.png”,“ Dog.png”,“ Rabbit.png”,“ Pig.png”。
So your project structure is something like this: 所以您的项目结构是这样的:
demo
src
└───demo
│ Demo.java
└───images
Bird.png
Cat.png
Dog.png
Rabbit.png
Pig.png
ImageIcon icon = createImageIcon("images/" + name + ".png");
This line is providing image path which is images/name.png
and value of name
could be among these {"Bird", "Cat", "Dog", "Rabbit", "Pig"}
该行提供的图像路径为
images/name.png
, name
值可以在{"Bird", "Cat", "Dog", "Rabbit", "Pig"}
picture.setIcon(icon);
The above line is setting the icon
to JLabel
picture
. 上一行将
icon
设置为JLabel
picture
。
Create a folder named images
in root directory of the project and put all .png
images in images
folder. 在项目的根目录中创建一个名为
images
的文件夹,并将所有.png
图像放入images
文件夹中。
See: http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading 请参阅: http : //www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading
I finally found out where the problem lies, but I cannot understand why they way Oracle made the code doesn't work. 我终于找到了问题所在,但我不明白为什么他们用Oracle编写代码的方式行不通。
Go back to the original code published at https://www.codetantra.com/java/jdk6.0/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java 返回在https://www.codetantra.com/java/jdk6.0/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java发布的原始代码
You need to go to the folder where your project is. 您需要转到项目所在的文件夹。 You will see: bin images src
您将看到:bin images src
The images folder may have all the expected image files in it, but the program does not get to them somehow. images文件夹中可能包含所有预期的图像文件,但是程序无法以某种方式获取它们。 Copy the gif files into the folder that holds bin, images, and src.
将gif文件复制到保存bin,图像和src的文件夹中。
You will now see: bin images src Bird.gif Cat.gif and so forth. 现在,您将看到:bin images src Bird.gif Cat.gif等。
Now go back and change Oracle's code: 现在返回并更改Oracle的代码:
// ImageIcon icon = createImageIcon("images/" + name + ".gif"); // ImageIcon图标= createImageIcon(“ images /” +名称+“ .gif”); //This way does not work.
//这种方式行不通。
to 至
ImageIcon icon = createImageIcon(name + ".gif");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.