2016-06-01 117 views
0

我是一名正在學習Java編程並使用Netbeans創建應用程序的學生。該程序已經完成並在IDE中加載(包含圖像)。我必須將其構建到JAR中供演講者進行演示並已完成,但JAR中不存在圖像。我無法在.JAR中加載圖像

首先,我已經檢查了所有可用的答案,允許圖像出現在JAR中,但是我無法在程序中正確顯示它,因爲它甚至無法在IDE中加載並顯示錯誤。其中大部分指出我必須輸入(getClass().getClassLoader().getResource("image.jpg"))。我嘗試輸入它,但它顯示錯誤,主要是因爲我放置ImageIcon的代碼是不同的。

下面是提交方案我的JFrame的全碼:

import java.awt.Color; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import javax.swing.*; 
import javax.swing.ImageIcon; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 

public class A2{ 

    public void GUI() throws IOException { 

     JFrame frame = new JFrame(); 
     frame.setVisible(true); 
     frame.setTitle("Minus"); 
     frame.setSize(700,500); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 

      //Set the layout 
      JPanel panel; 
     panel = new JPanel(); 
     panel.setLayout(null); 
     frame.setContentPane(panel); 

     // Create all components 
     JLabel ctgy = new JLabel("Minus"); 
      JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?"); 
     JButton ans_a = new JButton("9"); 
      JButton ans_b = new JButton("3"); 
      JButton ans_c = new JButton("7"); 
     JButton back = new JButton("Back"); 
      JLabel min2 = new JLabel(); 

     //Add objects to layout 
     panel.add(ctgy); 
      panel.add(minus2); 
     panel.add(min2); 
      panel.add(ans_a); 
      panel.add(ans_b); 
      panel.add(ans_c); 
     panel.add(back); 

     // Set position of objects in content pane 
      min2.setLocation(100,100); 
      minus2.setLocation(20,50); 
     ctgy.setLocation(10,3); 
      ans_a.setLocation(500,100); 
      ans_b.setLocation(500,150); 
      ans_c.setLocation(500,200); 
     back.setLocation(500, 400); 

     //Set size of object 
      min2.setSize(300,300); 
     ctgy.setSize(200,50); 
      minus2.setSize(350,50); 
      ans_a.setSize(100,30); 
      ans_b.setSize(100,30); 
      ans_c.setSize(100,30); 
     back.setSize(100, 30); 

     //Set the fonts and colors 
     Font font1 = new Font("Cooper Black", Font.BOLD, 26); 
     Font font2 = new Font("Calisto MT", Font.BOLD, 20); 
     ctgy.setFont(font1); 
     ctgy.setBackground(Color.white); 
      minus2.setFont(font2); 
      minus2.setBackground(Color.red); 
     panel.setBackground (Color.RED); 
      ans_a.setBackground(Color.white); 
     ans_b.setBackground(Color.white); 
      ans_c.setBackground(Color.white); 

      min2.setIcon(new ImageIcon("src/images/6-3.png")); 
      ans_b.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       JOptionPane.showMessageDialog(null, "Correct!"); 
       try { 
        A3.main(null); 
       } catch (IOException ex) { 
        Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex); 
       } 
        } 
       }); 

      ans_a.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent arg0) { 
        JOptionPane.showMessageDialog(null, "Incorrect! Please try again."); 
       } 
      });   

      ans_c.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent arg0) { 
        JOptionPane.showMessageDialog(null, "Incorrect! Please try again."); 
       } 
      }); 
     frame.repaint(); 



     back.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent f){ 
           Categories.main(null); 
         } 
       }); 



    }  

public static void main (String [] args) throws IOException { 
     A2 gd = new A2(); 
     gd.GUI(); 
    } 


} 

這是我州JLabel以把我的形象在我命名爲MIN2我的代碼的一部分:

JLabel ctgy = new JLabel("Minus"); 
      JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?"); 
      JButton ans_a = new JButton("9"); 
      JButton ans_b = new JButton("3"); 
      JButton ans_c = new JButton("7"); 
      JButton back = new JButton("Back"); 
      JLabel min2 = new JLabel(); 

這是添加面板爲的JLabel:

panel.add(min2); 

的大小和位置:

min2.setLocation(100,100); 
min2.setSize(300,300); 

最後圖像本身和地點:

min2.setIcon(new ImageIcon("src/images/6-3.png")); 

這一部分會顯示錯誤,因爲我設置爲打開一個新的類,當用戶點擊將JButton這樣,因爲你不認爲會發生有A3類:

public void actionPerformed(ActionEvent arg0) { 
        JOptionPane.showMessageDialog(null, "Correct!"); 
        try { 
         A3.main(null); 
        } catch (IOException ex) { 
         Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex); 
        } 

我檢查用WinRAR JAR文件並確認圖像的文件夾和圖像都在裏面。我想發佈截圖,但Imgur不適合我。

所有圖像的路徑文件位於src/images之內。

請提出我需要做些什麼改變。如果問得太多,謝謝你,對不起。

+0

您的JAR不應該包含src文件夾...並且您應該使用'getClass()。getResourceAsStream(String path)'加載資源。看到這個問題https://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource –

+0

雖然更詳細的資源是如何加載在這裏。 http://stackoverflow.com/a/676273/2308683 –

+0

*「我必須將它構建到JAR中,以便向演講者呈現並完成它,但**圖像在JAR中不存在。」*你確定?從命令行輸出的jar -tvf the.jar是什麼? –

回答

0
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/img.png"))); 

這是你應該如何加載圖像file.your給定的文件路徑是錯誤的嘗試這種方法。