2016-02-25 45 views
0
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(); 
      } 
     }); 
    } 
} 

这是我从甲骨文获得的代码,但我不知道从哪里选取图片,因为每次我用图片创建一个源文件夹时,它仍然不会捡起它。或者,如果你能告诉我代码的哪一部分正在挑选图像?我需要为我的工作使用相同的代码。有人可以告诉我它从哪里采摘图像?

Screenshot

+0

在你的'user.dir'中创建一个'images'文件夹,并把图像放在那里。你可以看到'user.dir'正在打印出'System.getProperty(“user.dir”)'。 – ifly6

回答

0

创建一个在你的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 
+0

我确实尝试过,但它不起作用。 – EverBloom

+0

你是什么意思的“不行”?你把你的java文件和images文件夹放在名为'demo'的文件夹内吗? –

+0

我希望我可以在这里张贴屏幕截图,但是我有主项目名称演示,然后我有两个文件夹名称作为src,其中包含应该以png格式显示的图像的demo.java和images文件夹。谢谢 – EverBloom

0
ImageIcon icon = createImageIcon("images/" + name + ".png"); 

此行提供图片的路径这是nameimages/name.png和值可能是这些{"Bird", "Cat", "Dog", "Rabbit", "Pig"}

picture.setIcon(icon); 

上述中行将icon设置为JLabelpicture

在该项目的根目录下创建一个名为images的文件夹,并将所有.png图像放入images文件夹中。

相关问题