Open word file through .jar

2

I have a .jar calculator that works perfectly! My doubts are: In the menu I have the "Source Code" button and I would like, when clicking, to open a Word document with the source code of the program. I managed to accomplish this task but only on my computer! I've saved the .docx document inside another package in .jar, but I need to know how to "call it" when I click the button to run on any machine! That's my main question, but I'd like somebody else to help me so that by pressing the +, -, * and / buttons on the keyboard, they were associated with the ActionListener buttons, Buttons, and etc. present in the code! I believe I need to use KeyListener, but I do not know how ... If anyone can help me I will be very grateful, especially with regards to opening the document. Here's the code:

import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.*;
import java.io.File;
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");
    JMenuItem codFonte = new JMenuItem("Código-Fonte");

    opcoes.add(sobreCalc);
    opcoes.add(sobreMim);
    opcoes.add(codFonte);

    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){
            ImageIcon about = new ImageIcon(Calculadora.class.getResource("/Imagens/Java2.jpg"));
            JOptionPane.showMessageDialog(null,"Programado em Java 7 \n Plataforma Eclipse", "Sobre o software",0,about);
        }
    });
    sobreMim.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            ImageIcon about = new ImageIcon(Calculadora.class.getResource("/Imagens/Java2.jpg"));
            JOptionPane.showMessageDialog(null,"Programador:\n Gabriel Ozzy Santos","Sobre o desenvolvedor",0,about);
        }
    });
    codFonte.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            File docx = new File("C:/Users/Gabriel Ozzy/workspace/Calculadora/src/Imagens/codigoCalc.docx");  
            try {  
              Desktop.getDesktop().open(docx);  
            } catch(Exception ex) {  
              ex.printStackTrace();  
              JOptionPane.showMessageDialog(null, "Erro no Desktop: " + ex);  
            }
        }
    });
    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);
}
}

    
asked by anonymous 15.05.2014 / 19:40

1 answer

4

Use the relative path instead of the absolute path to open your .docx

File docx = new File("src/Imagens/codigoCalc.docx"); 

When exporting, make sure that within your .jar you have the file in the address src/Imagens/codigoCalc.docx

PS: It is customary for most programmers to leave within the src folder only the .java files, the files that are program resources usually go in folders the part. I usually put it in a folder called assets , consider separating your files in a similar way, however, what you're doing is not wrong, it can only cause confusion if your code starts to get very large.

    
15.05.2014 / 20:00