Functional button in Java

Good I need help in java, I'm doing a graphic menu in netbeans with the part of making menus that already has built-in that is to drag the options and the menu is made, by placing a button and double-clicking the menu code appears and to edit it, I need help to make that button perform for example a mathematical operation of sum as you would have to put the code or open to do, I would thank you very much for your help.

enter the description of the image here NetBeans has the option to make menus by objects only by dragging the options and by double clicking the button the menu code already generated by the compiler comes out, what I want is that in that button in your code enter a function for example a sum operation or open a document, that by pressing the button Generate said Operation. Thank you.

enter the description of the image here

package empresa;

public class Menu extends javax.swing.JFrame {


    public Menu() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(jButton1)
                .addContainerGap(187, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addComponent(jButton1)
                .addContainerGap(177, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//AQUI VA LA ACCION A EJECUTAR EL BOTON
    }                                        


    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Menu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}
 1
Author: Jorgesys, 2016-06-05

2 answers

Hello at first I got confused as gdaimon since you were commenting You Didn't want to use JMenuBar and JMenu, I put an example to perform a sum operation.

Add two JTextField to enter the values you want to add and one to add the result, then we would have:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //AQUI VA LA ACCION A EJECUTAR EL BOTON
        double valor1, valor2, resultado;
        //obtienes los valores a sumar que están introducidos en los JTextField. 
        valor1 = Double.parseDouble(jTextField1.getText());
        valor2 = Double.parseDouble(jTextField2.getText());
        //Realizas la suma.
        resultado = valor1 + valor2;
        //El resultado es agregado al JTextField destinado para mostrarlo.
        jTextFieldResultado.setText(String.valueOf(resultado));

 }   
 2
Author: Jorgesys, 2016-06-05 21:18:06

Look What happens is that the IDE creates a lot of unnecessary code you can implement it in the following way and it is easier to understand, I think this will help you:

public class Prueba extends JFrame implements ActionListener {

JButton boton1;

public Prueba() {
 setLayout(null);
 boton1=new JButton("Click");
 boton1.setBounds(300,250,100,30);
 add(boton1);
 boton1.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
  if (e.getSource()==boton1) {
      System.out.println("Funciona...!");
        try {
            Runtime.getRuntime().exec("cmd /c start C:\\\"Users\\Administrador\\Desktop\"\\File.xls");
        } catch (IOException error) {
            error.printStackTrace();
        }
  }
 }

public static void main(String[] ar) {
  Prueba prueba=new Prueba();
  prueba.setBounds(0,0,450,350);
  prueba.setVisible(true);
 }
}
 0
Author: Gdaimon, 2016-06-05 06:36:39