首先,我想说是的,我搜索过,我搜索得很辛苦,不幸的是,我没有找到任何对我来说足够好的答案,我想我知道该怎么做,但我不会'我知道怎么做!好吧,我在我的程序中放了很多图像,效果很好,当我在计算机上运行时,图像出现并且没有问题,但我不能让图像出现在其他图像中电脑的!每当我把.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);
}
}
答案 0 :(得分:2)
您应该使用ImageIcon(URL)
并从类加载器获取资源URL,例如
ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
(顺便说一下,我避免在文件名中使用非ASCII字符。它可能很有用,但它只是可能出错的一件事。)
确保图像在您的jar文件中(例如,在&#34; / images&#34;在上例中)。
另请注意,您要在每个5位代码中加载相同的图像图标。我强烈建议您将加载并重复使用。
答案 1 :(得分:1)
ImageIcon(URL)
这是一个小工作示例。在这种情况下,图像位于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();
}
}
}
答案 2 :(得分:0)
不要使用下面代码的swing部分。那很糟糕。只需使用加载图像的方式,它应该在你的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);
}
}