2013-04-05 305 views
1

我不斷收到一個錯誤,createImageIcon()不被識別爲可用函數。不知道爲什麼會出現這種情況,因爲我覺得我已經加載了我需要的庫。Java - createImageIcon()在代碼中無法識別?

ImageIcon icon = createImageIcon("images/middle.gif","this is a caption"); 

我試過使用不同的參數等,但似乎無法弄清楚。

import javax.swing.*; 
import javax.swing.ImageIcon; 
import java.awt.*; 
import java.awt.image.BufferStrategy; 

public class main extends JFrame { 

    public main() { 

    } 

    public static void main(String[] args) { 
     JMenuBar menuBar = new JMenuBar(); 
     JMenu Menu_File = new JMenu("TEST MENU"); 
     JMenu Menu_File_New = new JMenu("New"); 
     ImageIcon icon = createImageIcon("images/middle.gif","this is a caption"); 
     Menu_File.add(Menu_File_New); 
     JMenuItem Menu_File_New_Project = new JMenuItem("Project..."); 
     JMenuItem Menu_File_New_Burrito = new JMenuItem("Burrito..."); 
     JMenuItem Menu_File_New_Cookie = new JMenuItem("Cookie..."); 
     JMenuItem Menu_File_New_Other = new JMenuItem("Other..."); 
     Menu_File_New.add(Menu_File_New_Project); 
     Menu_File_New.add(Menu_File_New_Burrito); 
     Menu_File_New.add(Menu_File_New_Cookie); 
     Menu_File_New.add(Menu_File_New_Other); 
     JMenuItem Menu_File_Open = new JMenuItem("Open..."); 
     JMenuItem Menu_File_Save = new JMenuItem("Save"); 
     JMenuItem Menu_File_SaveAs = new JMenuItem("Save As..."); 
     JMenuItem Menu_File_Reload= new JMenuItem("Reload"); 
     Menu_File.add(Menu_File_Open); 
     Menu_File.add(Menu_File_Save); 
     Menu_File.add(Menu_File_SaveAs); 
     Menu_File.add(Menu_File_Reload); 
     Menu_File.addSeparator(); 
     JMenuItem Menu_File_Screenshot = new JMenuItem("Take Screenshot"); 
     Menu_File.add(Menu_File_Screenshot); 
     Menu_File.addSeparator(); 
     JMenuItem Menu_File_Exit = new JMenuItem("Exit"); 
     Menu_File.add(Menu_File_Exit); 
     menuBar.add(Menu_File); 
     JMenu Menu_Edit = new JMenu("Edit"); 
     JMenu Menu_Windows = new JMenu("Windows"); 
     JMenu Menu_Mode = new JMenu("Mode"); 
     JMenu Menu_Help = new JMenu("Help"); 
     menuBar.add(Menu_Edit); 
     menuBar.add(Menu_Windows); 
     menuBar.add(Menu_Mode); 
     menuBar.add(Menu_Help); 

     main frame = new main(); 
     frame.setJMenuBar(menuBar); // Create menu and associate with frame 
     frame.setTitle("3DAirspace"); 
     frame.setUndecorated(true); 
     //frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

回答

6

如果它實際上是:

ImageIcon icon = new ImageIcon("images/middle.gif","this is a caption"); 

讓你調用構造一個新的ImageIcon對象?

+0

這工作,謝謝! :) – IDontWorkAtNASA 2013-04-05 18:41:10

4

您打電話的方法不在您的班級中。

在Java教程How to use icons中,有一個相同名稱的示例方法,您可以將其複製到您的類中。

/** Returns an ImageIcon, or null if the path was invalid. */ 
protected ImageIcon createImageIcon(String path, 
             String description) { 
    java.net.URL imgURL = getClass().getResource(path); 
    if (imgURL != null) { 
     return new ImageIcon(imgURL, description); 
    } else { 
     System.err.println("Couldn't find file: " + path); 
     return null; 
    } 
} 
-1

這項工作?

ImageIcon icon = new createImageIcon("images/middle.gif","this is a caption"); 
+0

「createImageIcon無法解析爲類型」 – IDontWorkAtNASA 2013-04-05 18:32:29

1

此代碼的工作:

JButton button1 = new JButton("Button 1",new ImageIcon("youricon.gif"));