2016-06-01 111 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给定的文件路径是错误的尝试这种方法。