簡體   English   中英

如何將圖像放在.jar文件中?

[英]How to put images in .jar file?

首先,我想說是的,我搜尋了,我非常努力地搜尋,我找不到任何對我來說足夠好的答案,我想我知道該怎么做,但是我不知道該怎么做! 好吧,我在程序中放入了很多圖像,效果很好,當我在計算機上運行時,圖像出現了,沒有問題,但是我無法使圖像出現在其他PC上! 每次我將.jar放入另一台計算機中時,圖像都會消失...好吧,我想我必須使用images文件夾創建一個包,對嗎? 但是我不知道該怎么做,或者之后該怎么辦! 如果有人可以給我一個完整的例子,那就太棒了……這是我的代碼,請讓我知道是否可以改善問題,我只是新來的!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IMC extends JFrame {
    private static final long serialVersionUID = 1L;

    private JButton ok;
    private JButton sair;
    private JTextField txt1;
    private JTextField txt2;
    private Dimension dim,dim2,dim3;
    private JLabel l1,l2,l3,l4;
    private Font letra;
    private float tamanho;
    private float peso;
    private float imc;

        public IMC (){
            dim = new Dimension(650,500);
            dim2 = new Dimension (340,25);
            dim3 = new Dimension (75,35);
            setTitle("Calcule seu IMC!");
            setSize(dim);
            setLocation(190,100);
            setResizable(false);
            setBackground(new Color(250,250,200));
            Color lightGray = Color.gray.brighter();
            getContentPane().setBackground(lightGray);
            setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            JMenuBar mBar = new JMenuBar();

            JMenu opcoes = new JMenu("Opções");
            JMenuItem sobreIMC = new JMenuItem("Sobre");
            JMenuItem sobreMim = new JMenuItem("Sobre mim");

            opcoes.add(sobreIMC);
            opcoes.add(sobreMim);

            JMenu fechar = new JMenu("Fechar");
            JMenuItem fecharIMC = new JMenuItem("Fechar");

            fechar.add(fecharIMC);

            mBar.add(opcoes);
            mBar.add(fechar);
            setJMenuBar(mBar);

            tamanho = 0;
            peso = 0;

            letra = new Font("Arial",Font.PLAIN,14);
            setFont(letra);

            l1 = new JLabel();
            l1.setText("Complete os campos abaixo:");
            l1.setSize(250,70);
            l1.setLocation(100,0);
            l1.setFont(new Font("Arial", Font.BOLD,15));
            this.add(l1);

            l2 = new JLabel();
            l2.setText("Insira sua altura:");
            l2.setBounds(47,85,150,150);
            l2.setFont(new Font("Arial", Font.BOLD,13));
            this.add(l2);

            l3 = new JLabel();
            l3.setText("Insira seu peso:");
            l3.setBounds(47,165,150,150);
            l3.setFont(new Font("Arial", Font.BOLD,13));
            this.add(l3);

            l4 = new JLabel();
            l4.setText("Gabriel Ozzy Santos");
            l4.setBounds(230,270,150,100);
            l4.setFont(new Font ("Verdana", Font.ITALIC,15));
            this.add(l4);

            ImageIcon im = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java3.1.jpg");
            JLabel l5 = new JLabel(im);
            l5.setBounds(255,350,120,120);
            this.add(l5);

            ok = new JButton();
            ok.setText("OK");
            ok.setLocation(180,390);
            ok.setSize(dim3);
            ok.setBackground(new Color(157,182,210));
            ok.setForeground(Color.white);
            this.add(ok);

            sair = new JButton();
            sair.setText("Sair");
            sair.setLocation(370,390);
            sair.setSize(dim3);
            sair.setBackground(new Color (157,180,210));
            sair.setForeground(Color.white);
            this.add(sair);

            txt1 = new JTextField();
            txt1.setSize(dim2);
            txt1.setLocation(160,150);
            this.add(txt1);

            txt2 = new JTextField();
            txt2.setSize(dim2);
            txt2.setLocation(160,230);
            this.add(txt2);


        ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                String t1 = txt1.getText();
                if(!"".equals(t1))
                tamanho = Float.parseFloat(t1);

                String t2 = txt2.getText();
                if(!"".equals(t2))
                peso = Float.parseFloat(t2);
                imc = (peso/(tamanho*tamanho));

                if (ok != null){
                    ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null,"Seu IMC é:"+imc,"Resultado do IMC",0,info);
                }
                if (imc <18.5){
                    ImageIcon info2 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null,"Você parece o esqueleto do HE-MAN!","Seu magrelo !",0, info2);
                }
                if (imc > 18.5 && imc < 24.9){
                    ImageIcon info3 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null, "Ta no peso certinho :)", "Continue assim!",0,info3);
                }
                if (imc > 24.9 && imc < 29.9){
                    ImageIcon info4 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null,"Você está ficando gordinho! :(", "Emagrece!",0,info4);
                }
                if (imc > 29.9 && imc < 34.9){
                    ImageIcon info5 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null,"Gordo!","Baleia!",0,info5);
                }
                if (imc > 34.9){
                    ImageIcon info6 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
                    JOptionPane.showMessageDialog(null,"Obeso mórbido!", "Orca Assassina!",0,info6);
                }
                tamanho = 0;
                peso = 0;
                txt1.setText("");
                txt2.setText("");
            }
        });
        sair.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                System.exit(0);
            }
        });
        sobreIMC.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                Icon about = new ImageIcon ("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
                JOptionPane.showMessageDialog(null,"Programado em Java 7 - Plataforma Eclipse","Sobre o software",0,about);
            }
        });
        sobreMim.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                Icon about2 = new ImageIcon ("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
                JOptionPane.showMessageDialog(null,"Programador: Gabriel Ozzy Santos","Sobre o desenvolvedor",0,about2);
            }
        });
        fecharIMC.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                System.exit(0);
            }
        });
        }
        public static void main (String [] args){
            new IMC().setVisible(true);
        }
}

您應該使用ImageIcon(URL)並從類加載器中獲取資源URL,例如

ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));

(順便說一下,我會避免在文件名中使用非ASCII字符。這可能很好用,但這可能又會出錯。)

確保圖像在您的jar文件中(例如,上例中的“ / images”下)。

還要注意,您在5位代碼中的每一個中都加載了相同的圖像圖標。 我強烈建議加載一次並重新使用它。

  1. 不要使用絕對路徑,而要使用相對路徑。 其他計算機將不會具有您在此處指定的相同路徑。
  2. ImageIcon(URL)加載圖像
  3. 確保在編譯.jar時將圖像添加到您的類路徑中

這是一個小的工作示例。 在這種情況下,映像位於src / resources中。

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ImageViewer {

    private final static String TITLE_IMAGE = "/resource/myimage.png";
    private final JFrame frame;
    private BufferedImage titleImage;
    private JLabel titleLabel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ImageViewer();
            }
        });
    }

    public ImageViewer() {
        frame = new JFrame();
        init();
    }

    private void init() {
        frame.setVisible(true);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        try {
            titleImage = ImageIO.read(getClass().getResource(TITLE_IMAGE));
            titleLabel = new JLabel(new ImageIcon(titleImage));
            frame.add(titleLabel);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

不要使用下面的代碼的擺動部分。 那太爛了。 只需使用加載圖像的方式,它就可以在您的jar中工作。

public class Test extends JPanel{
    static BufferedImage image = null;
    public static void main(String[] args) throws IOException {

        image = ImageIO.read(Test.class.getClassLoader().getResourceAsStream("images/g.png"));
        Test clz = new Test();
        JFrame frame = new JFrame();
        frame.add(clz);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
    g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM