Insert a submenu into a JMenu

1

When I click on the woman or man icon in JFrame , it is called a JPanel where I enter the data as weight and height. But when I click save, I can not call another JPanel to show the result of the calculation, since I'm already in one.

Is there a way to put a menu item inside the woman or man icon, as if it were a sub-shortcut?

JFrame main class:

public class Home extends javax.swing.JFrame {

    public Home() {
    initComponents();
    }

    private void jImcHomemActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        ImcHomem imcHomem = new ImcHomem();
        this.setContentPane(imcHomem);
        this.pack();
    } 
// ...
}

Class where the user informs the data (weight and height) JPanel

public class ImcHomem extends javax.swing.JPanel {
    double pesoH;
    double alturaH;

    private void jBtnSalvarHActionPerformed(java.awt.event.ActionEvent evt){                                            
    // variável pesoH recebe o valor passado para Double do botão jTextPesoH
    pesoH = Double.parseDouble(jTextPesoH.getText());
    // variável alturaH recebe o valor passado para Double do botão jTextAlturaH
    alturaH = Double.parseDouble(jTextAlturaH.getText());
    // inseri os dados no botão Salvar só para ter certeza de que o código está correto
    jBtnSalvarH.setText("Peso: "+pesoH+" kg "+" e altura: "+alturaH+" cm");
    ResultHomem resuH = new ResultHomem();
    }                                           

    private void jTextPesoHActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        pesoH = Double.parseDouble(jTextPesoH.getText());
    }                                          

    private void jTextAlturaHActionPerformed(java.awt.event.ActionEvent evt{                                             
        // TODO add your handling code here:
        alturaH = Double.parseDouble(jTextAlturaH.getText());
    }
// ...
}

Class that would call to show the result

public class ResultHomem extends javax.swing.JPanel {

    /**
    * Creates new form ResultHomem
    */
    public ResultHomem() {
        initComponents();
    }

    private void jTextResultHActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:

    }
    // ...
}

    
asked by anonymous 07.06.2017 / 22:57

1 answer

1

You can create a JMenu normally and add it to the main menu, there is no secret;)

menu = new JMenu("Calcular Imc");

subMenu01 = new JMenu("homem");
mSubItem01 =  new JMenuItem("calcular");
subMenu01.add(mSubItem01 );

subMenu02 = new JMenu("mulher");
msubItem02 = new JMenuItem("calcular");
subMenu02.add(msubItem02);

menu.add(subMenu01);
menu.add(subMenu02);
menubar.add(menu);

Running:

Reference:

Adding a submenu

    
07.06.2017 / 23:41