Open word file through the . jar

Asked

Viewed 1,087 times

2

I have a calculator in . jar working perfectly ! My questions are: In the menu I have the "Source code" button and I would like to open a Word document with the source code of the program. I managed to accomplish this task but only on my computer! I saved the document . docx within another package on . jar, but I need to know how to "call it" by clicking the button to work on any machine ! This is my main question, but in addition, I wish someone could help me make that, when pressing the buttons of +,-,* and / the keyboard, they were associated with the Actionlistener Boots More, Boots and etc present in the code! I believe it’s necessary to use the Keylistener, but I don’t know how.. If anyone can help me, I’d be very grateful, especially with regard to opening the document. Here comes 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);
}
}

inserir a descrição da imagem aqui

  • It would be good for you to create two questions

  • On the second part of your question, I believe it would be a duplicate of Push button by hotkey in Java

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 inside your . jar you have the file in the address src/Imagens/codigoCalc.docx

PS: It is the custom of most programmers to leave inside the folder src only the files . java, the files that are program resources usually go in folders apart. I usually put in a folder called assets, consider the possibility of separating your files in a similar way, however, what you are doing is not wrong, it can only generate confusion if your code starts to get too large.

  • however if you put your files in a folder that is not the src you will have to add it to your build path, and last time I remember it didn’t go right mt, rs

  • Thanks for the tip, I believe it was already half way, but still, when I execute . jar it does not find the file, only finds when I rotate by Eclipse ! Why will be ?

  • So that was my first attempt! However, it error, and when I step the mouse over, the error is the following: "The constructor File(URL) is Undefined" and it gives no suggestions, so I could not understand what to do !

  • @Gabrielozzy and if you do so: File docx = new File(Calculador.class.getResouce("Imagens/codigoCalc.docx").getPath()); ?

  • Not yet... Eclipse warns that the file does not exist, but I have checked, is in the folder, this with the name and the extension very correct, I do not understand, the images were no problem, however the document... I posted the mistake in an issue !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.