2016-02-25 47 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文件夾中。

相關問題