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获得的代码,但我不知道从哪里选择图像需要,因为每次我创建一个带有图像的源文件夹时,它仍然不会提取它。或者,如果你能告诉我哪部分代码正在挑选图片?我需要为我的工作使用相同的代码。
答案 0 :(得分:0)
在demo
src
└───demo
│ Demo.java
└───images
Bird.png
Cat.png
Dog.png
Rabbit.png
Pig.png
所在的同一文件夹中创建名为(intOption = 0)
的文件夹。并将您的图像放在那里。根据您的代码,图像名称为“Bird.png”,“Cat.png”,“Dog.png”,“Rabbit.png”,“Pig.png”。
所以你的项目结构是这样的:
intOption
答案 1 :(得分:0)
ImageIcon icon = createImageIcon("images/" + name + ".png");
此行提供images/name.png
的图片路径,name
的值可能属于{"Bird", "Cat", "Dog", "Rabbit", "Pig"}
picture.setIcon(icon);
以上一行是将icon
设置为JLabel
picture
。
在项目的根目录中创建名为images
的文件夹,并将所有.png
图像放在images
文件夹中。
答案 2 :(得分:0)
我终于找到了问题所在,但我无法理解为什么Oracle会让代码无法正常工作。
您需要转到项目所在的文件夹。你会看见: 箱子 图片 SRC
images文件夹中可能包含所有预期的图像文件,但程序无法以某种方式访问它们。将gif文件复制到包含bin,images和src的文件夹中。
您现在将看到: 箱子 图片 SRC Bird.gif Cat.gif 等等。
现在返回并更改Oracle的代码:
// ImageIcon icon = createImageIcon(" images /" + name +" .gif"); //这种方式不起作用。
到
ImageIcon icon = createImageIcon(name + ".gif");