Insert a submenu into a Jmenu

Asked

Viewed 487 times

1

When I click on the woman or man icon on JFrame, is called a JPanel where I enter the data such as weight and height. But by clicking save, I cannot call another JPanel to show the result of the calculation, as I am already in a.

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

Main class Jframe:

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 user reports 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:

    }
    // ...
}

JFrame (Tela inicial) - Local onde seleciono o ícone para fazer o calculo do IMC

  • Ester, I had already answered, have you tried the solution below? That’s basically the answer;

1 answer

1

You can create a JMenu normally and add it to the main menu, has 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);

Working:

inserir a descrição da imagem aqui


Reference:

Adding to submenu

Browser other questions tagged

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