2
I have several programs that run perfectly on my machine, the .jar
runs smoothly and I use them as well as I wish, no problem. However, when I run them on another machine, images do not appear. The program runs perfectly, everything as it should be, but the images just don’t appear.
Here’s one of the programs I’d like you to run, a calculator. I’ve changed the icon of JOpTionPane.showMessageDialog
, that is in the menu, and the icon does not appear.
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class Calculadora extends JFrame {
private static final long serialVersionUID = 1L;
private JButton n1;
private JButton n2;
private JButton n3;
private JButton n4;
private JButton n5;
private JButton n6;
private JButton n7;
private JButton n8;
private JButton n9;
private JButton n0;
private JButton botaoMais;
private JButton botaoMenos;
private JButton botaoVezes;
private JButton botaoDividi;
private JButton botaoIgual;
private JButton botaoC;
private JButton botaoCE;
private JButton botaoPonto;
private char operacao;
private int inteiro;
private int decimal;
private double memoria;
private boolean ponto;
private JLabel creditos;
private JTextField campo;
public Calculadora(){
setTitle("Calculadora do Ozzy");
setBounds(0,0,280,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
getContentPane().setBackground(new Color(190,194,233));
setLayout(null);
JMenuBar mBar = new JMenuBar();
JMenu opcoes = new JMenu("Opções");
JMenuItem sobreCalc = new JMenuItem("Sobre");
JMenuItem sobreMim = new JMenuItem("Sobre mim");
opcoes.add(sobreCalc);
opcoes.add(sobreMim);
JMenu sair = new JMenu("Sair");
JMenuItem exit = new JMenuItem ("Sair");
sair.add(exit);
mBar.add(opcoes);
mBar.add(sair);
setJMenuBar(mBar);
inteiro = 0;
decimal = 0;
memoria = 0;
n1 = new JButton ();
n1.setText("1");
n1.setBounds(25,115,45,45);
n1.setBackground(new Color(134,142,215));
n1.setForeground(Color.white);
this.add(n1);
n2 = new JButton ();
n2.setText("2");
n2.setBounds(85,115,45,45);
n2.setBackground(new Color(134,142,215));
n2.setForeground(Color.white);
this.add(n2);
n3 = new JButton();
n3.setText("3");
n3.setBounds(140,115,45,45);
n3.setBackground(new Color(134,142,215));
n3.setForeground(Color.white);
this.add(n3);
n4 = new JButton();
n4.setText ("4");
n4.setBounds (25,170,45,45);
n4.setBackground(new Color(134,142,215));
n4.setForeground(Color.white);
this.add(n4);
n5 = new JButton ();
n5.setText("5");
n5.setBounds(85,170,45,45);
n5.setBackground(new Color(134,142,215));
n5.setForeground(Color.white);
this.add (n5);
n6 = new JButton();
n6.setText("6");
n6.setBounds(140,170,45,45);
n6.setBackground(new Color(134,142,215));
n6.setForeground(Color.white);
this.add(n6);
n7 = new JButton ();
n7.setText("7");
n7.setBounds(25,225,45,45);
n7.setBackground(new Color(134,142,215));
n7.setForeground(Color.white);
this.add(n7);
n8 = new JButton();
n8.setText("8");
n8.setBounds (85,225,45,45);
n8.setBackground(new Color(134,142,215));
n8.setForeground(Color.white);
this.add (n8);
n9 = new JButton ();
n9.setText("9");
n9.setBounds (140,225,45,45);
n9.setBackground(new Color(134,142,215));
n9.setForeground(Color.white);
this.add (n9);
n0 = new JButton();
n0.setText ("0");
n0.setBounds (140,280,45,45);
n0.setBackground(new Color(134,142,215));
n0.setForeground(Color.white);
this.add (n0);
botaoMais = new JButton();
botaoMais.setText("+");
botaoMais.setBounds(195,115,45,45);
botaoMais.setBackground(new Color(133,197,235));
botaoMais.setForeground(Color.white);
this.add(botaoMais);
botaoMenos = new JButton ();
botaoMenos.setText ("-");
botaoMenos.setBounds(195,170,45,45);
botaoMenos.setBackground(new Color(133,197,235));
botaoMenos.setForeground(Color.white);
this.add (botaoMenos);
botaoVezes = new JButton();
botaoVezes.setText ("x");
botaoVezes.setBounds(195,225,45,45);
botaoVezes.setBackground(new Color(133,197,235));
botaoVezes.setForeground(Color.white);
this.add(botaoVezes);
botaoDividi = new JButton();
botaoDividi.setText("÷");
botaoDividi.setBounds(195,280,45,45);
botaoDividi.setBackground(new Color(133,197,235));
botaoDividi.setForeground(Color.white);
this.add(botaoDividi);
botaoIgual = new JButton();
botaoIgual.setText("=");
botaoIgual.setBounds(25,280,45,45);
botaoIgual.setBackground(new Color(133,197,235));
botaoIgual.setForeground(Color.white);
this.add(botaoIgual);
botaoPonto = new JButton ();
botaoPonto.setText(".");
botaoPonto.setBounds(85,280,45,45);
botaoPonto.setBackground(new Color(133,197,235));
botaoPonto.setForeground(Color.white);
this.add (botaoPonto);
botaoC = new JButton ();
botaoC.setText("C");
botaoC.setBounds(25,65,105,40);
botaoC.setBackground(new Color (157,182,210));
botaoC.setForeground(Color.white);
this.add(botaoC);
botaoCE = new JButton ();
botaoCE.setText("CE");
botaoCE.setBounds(140,65,100,40);
botaoCE.setBackground(new Color(157,182,210));
botaoCE.setForeground(Color.white);
this.add(botaoCE);
campo = new JTextField();
campo.setBounds(25,25,216,30);
this.add(campo);
creditos = new JLabel();
creditos.setBounds(78,287,200,100);
creditos.setText("Gabriel Ozzy Santos");
this.add(creditos);
n1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if(ponto){
decimal +=1;
} else{
inteiro *= 10;
inteiro +=1;
}
campo.setText(campo.getText()+ "1");
}
});
n2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (ponto){
decimal +=2;
}else{
inteiro *=10;
inteiro+= 2;
}
campo.setText (campo.getText()+"2");
}
});
n3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if (ponto){
decimal+=3;
}else {
inteiro *= 10;
inteiro += 3;
}
campo.setText(campo.getText()+"3");
}
});
n4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (ponto){
decimal+=4;
}else {
inteiro *= 10;
inteiro +=4;
}
campo.setText (campo.getText ()+"4");
}
});
n5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evt){
if (ponto){
decimal +=5;
}else{
inteiro*= 10;
inteiro +=5;
}
campo.setText (campo.getText ()+ "5");
}
});
n6.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent evt){
if (ponto){
decimal +=6;
}else {
inteiro*= 10;
inteiro +=6;
}
campo.setText (campo.getText ()+ "6");
}
});
n7.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evt){
if(ponto){
decimal +=7;
}else {
inteiro*= 10;
inteiro +=7;
}
campo.setText (campo.getText()+"7");
}
});
n8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if(ponto){
decimal +=8;
}else{
inteiro *=10;
inteiro +=8;
}
campo.setText(campo.getText()+"8");
}
});
n9.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (ponto){
decimal +=9;
}else{
inteiro *=10;
inteiro +=9;
}
campo.setText(campo.getText()+"9");
}
});
n0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if(ponto){
decimal +=0;
}else{
inteiro*=10;
inteiro +=0;
}
campo.setText(campo.getText()+"0");
}
});
botaoMais.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent evt){
double numero =Double.parseDouble(String.format("%s.%s",inteiro,decimal));
String t1 = campo.getText();
if(!"".equals(t1))
numero = Double.parseDouble(t1);
operacao = '+';
if (memoria > 0){
memoria += numero;
}else{
memoria = numero;
}
inteiro = 0;
numero = 0;
decimal = 0;
ponto = false;
campo.setText("");
}
});
botaoMenos.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent evt){
double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));
String t1 = campo.getText();
if(!"".equals(t1))
numero = Double.parseDouble(t1);
operacao = '-';
if (memoria > 0){
memoria -= numero;
}else {
memoria = numero;
}
numero = 0;
inteiro = 0;
decimal = 0;
ponto = false;
campo.setText("");
}
});
botaoVezes.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent evt){
double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));
String t1 = campo.getText();
if(!"".equals(t1))
numero = Double.parseDouble(t1);
operacao = '*';
if (memoria > 0){
memoria *= numero;
}else {
memoria = numero;
}
numero = 0;
inteiro =0;
decimal =0;
ponto = false;
campo.setText("");
}
});
botaoDividi.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evt){
double numero = Double.parseDouble(String.format("%s.%s",inteiro,decimal));
String t1 = campo.getText();
if(!"".equals(t1))
numero = Double.parseDouble(t1);
operacao = '/';
if(memoria > 0){
memoria /=numero;
}else {
memoria = numero;
}
numero=0;
inteiro =0;
decimal = 0;
ponto = false;
campo.setText("");
}
});
botaoC.addActionListener (new ActionListener(){
public void actionPerformed (ActionEvent evt){
inteiro = 0;
decimal = 0;
memoria = 0;
ponto = false;
campo.setText("");
}
});
botaoCE.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evt){
inteiro = 0;
decimal = 0;
ponto = false;
campo.setText("");
}
});
botaoPonto.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evt){
if (!ponto){
campo.setText(inteiro +".");
ponto = (true);
}
}
});
botaoIgual.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
double numero = Double.parseDouble(String.format("%s.%s", inteiro, decimal));
String t1 = campo.getText();
if(!"".equals(t1))
numero = Double.parseDouble(t1);
switch (operacao){
case '+':{
memoria += numero;
break;
}
case '-':{
memoria -=numero;
break;
}
case '*':{
memoria *=numero;
break;
}
case '/':{
memoria /=numero;
break;
}
}
numero = 0;
campo.setText(""+ memoria);
}
});
sobreCalc.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);
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.exit(0);
}
});
}
public static void main (String [] args){
Calculadora cCalculadora = new Calculadora();
cCalculadora.setVisible(true);
}
}
Hello, thank you for the answer ! Could you explain to me more precisely what Mediatracker is for ? thank you !
– Gabriel Ozzy
Mediatracker is a utility class for tracking the status of media objects. I used an attribute of the class that returns an integer value, the meaning of which is whether a media object was successfully loaded and compared with the return of the getImageLoadStatus() method. This method asked me to look at the attributes related to the media loading of the Mediatracker class, so after looking I made the comparison. I recommend reading the class documentation at http://docs.oracle.com/javase/7/docs/api/java/awt/MediaTracker.html.
– adelmo00
Thank you, it worked perfectly here, after two whole days crazy behind it ! Hahaha, thank you !
– Gabriel Ozzy
Nothing, I’m glad it worked.
– adelmo00