Search code examples
javaswingjmenujmenuitemjmenubar

How to call another class to my main menu class using the JMenuItem?


I don't know how to call another class (called Calc) that is in the same package as my main class (Lista), using the JMenuItem. If i need to be more specific, i dont know how to call my class Calc to my Lista class using a JMenuItem that its on my Lista class.

The code below is my Lista class, sorry for the english guys

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;

    import javax.swing.*;
    import java.awt.event.*;

    public class Lista extends JFrame{
      public Lista(){
        super("Menu");

        // Menu Bar
        JMenuBar barra = new JMenuBar();
        setJMenuBar(barra);

        // Menu
        JMenu opcoes = new JMenu("Options");

        // Menu Item
        JMenuItem item = new JMenuItem("Item 1");

        // actionlistener
        item.addActionListener(
          new ActionListener(){
            public void actionPerformed(ActionEvent e){
              //I think that is in here where i must write the code
            }
          }
        );

        opcoes.add(item);

        // Adds
        barra.add(opcoes);

        setSize(300, 150);
        setVisible(true);    
      }

      public static void main(String args[]){
        Lista app = new Lista();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }

The other class, Calc, its just a simple calculator that i made with this code: public class Calc extends JFrame {

public Calc(){
    super("Calculadora");
    Container tela = getContentPane();
    setLayout(null);

    JLabel rotulo1 = new JLabel("1 numero: ");
    JLabel rotulo2 = new JLabel("2 numero: ");
    JLabel showup = new JLabel("");

    JTextField texto1 = new JTextField(5);
    JTextField texto2 = new JTextField(5);

    JButton somar = new JButton ("+");
    JButton subtrair = new JButton("-");
    JButton dividir = new JButton("/");
    JButton multiplicar = new JButton("x");
    JButton exibir = new JButton("=");

    rotulo1.setBounds(30,20,100,20); rotulo2.setBounds(30,60,100,20);
    texto1.setBounds(100,20,200,20); texto2.setBounds(100,60,200,20);
    showup.setBounds(125,100,200,20);
    somar.setBounds(230,100,45,45);//coluna, linha, largura, comprimento
    subtrair.setBounds(280,100,45,45);
    dividir.setBounds(230,155,45,45);
    multiplicar.setBounds(280,155,45,45);
    exibir.setBounds(255,205,45,45);


    setVisible(true);
    setLocationRelativeTo(null);

    tela.add(rotulo1); tela.add(rotulo2);
    tela.add(texto1); tela.add(texto2); tela.add(showup);
    tela.add(exibir); tela.add(somar); tela.add(subtrair); tela.add(dividir);tela.add(multiplicar);

    setSize(350,300);

    somar.addActionListener( new ActionListener(){
        public void actionPerformed(ActionEvent e){
            double numero1, numero2, soma;
            soma=0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            soma = numero1+numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText()+""+"+"+""+texto2.getText()+""+"="+soma);
            texto1.setText(null); texto2.setText(null); texto1.requestFocus(); //funcao limpar e focar
        }
    }
    );

    subtrair.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            double numero1, numero2, subtrair;
            subtrair=0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            subtrair = numero1 - numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText()+""+"-"+""+texto2.getText()+""+"="+subtrair);
            texto1.setText(null); texto2.setText(null); texto1.requestFocus();
        }
    });

    multiplicar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            double numero1, numero2, multiplicar;
            multiplicar=0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            multiplicar = numero1*numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText()+""+"x"+""+texto2.getText()+""+"="+multiplicar);
            texto1.setText(null); texto2.setText(null); texto1.requestFocus();
        }
    });

    dividir.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            double numero1, numero2, dividir;
            dividir=0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            dividir=numero1/numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText()+""+"/"+""+texto2.getText()+""+"="+dividir);
            texto1.setText(null); texto2.setText(null); texto1.requestFocus();
        }
    });


}

public static void main (String [] args){
    Calc app = new Calc();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}

The only thing that i want to do is: when i click in the JMenuItem in Lista code, my calculator program (Calc class) is called. I already tried to do: "Calc calc = new Calc(); calc.Visible(true);" or "item = calc;" but failled. I'm a beginner programmer guys, sorry, i think thats simple.


Solution

  • Felipe, this worked for me... It's based on Sanjeev Saha answer...

    enter 
    
    import java.awt.Container;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class Calc extends JDialog {
    
    private JLabel rotulo1;
    private JLabel rotulo2;
    private JLabel showup;
    private JTextField texto1;
    private JTextField texto2;
    private JButton somar;
    private JButton subtrair;
    private JButton dividir;
    private JButton multiplicar;
    private JButton exibir;
    
    public Calc(Frame owner) {
    super(owner, "Calculadora");
    Container tela = getContentPane();
    setLayout(null);
    
    rotulo1 = new JLabel("1 numero: ");
    rotulo2 = new JLabel("2 numero: ");
    showup = new JLabel("");
    
    texto1 = new JTextField(5);
    texto2 = new JTextField(5);
    
    somar = new JButton("+");
    subtrair = new JButton("-");
    dividir = new JButton("/");
    multiplicar = new JButton("x");
    exibir = new JButton("=");
    
    rotulo1.setBounds(30, 20, 100, 20);
    rotulo2.setBounds(30, 60, 100, 20);
    texto1.setBounds(100, 20, 200, 20);
    texto2.setBounds(100, 60, 200, 20);
    showup.setBounds(125, 100, 200, 20);
    somar.setBounds(230, 100, 45, 45);// coluna, linha, largura, comprimento
    subtrair.setBounds(280, 100, 45, 45);
    dividir.setBounds(230, 155, 45, 45);
    multiplicar.setBounds(280, 155, 45, 45);
    exibir.setBounds(255, 205, 45, 45);
    
    setVisible(true);
    setLocationRelativeTo(null);
    
    tela.add(rotulo1);
    tela.add(rotulo2);
    tela.add(texto1);
    tela.add(texto2);
    tela.add(showup);
    tela.add(exibir);
    tela.add(somar);
    tela.add(subtrair);
    tela.add(dividir);
    tela.add(multiplicar);
    
    setSize(350, 300);
    
    somar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double numero1, numero2, soma;
            soma = 0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            soma = numero1 + numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText() + "" + "+" + "" + texto2.getText() + "" + "=" + soma);
            texto1.setText(null);
            texto2.setText(null);
            texto1.requestFocus(); // funcao limpar e focar
        }
    });
    
    subtrair.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double numero1, numero2, subtrair;
            subtrair = 0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            subtrair = numero1 - numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText() + "" + "-" + "" + texto2.getText() + "" + "=" + subtrair);
            texto1.setText(null);
            texto2.setText(null);
            texto1.requestFocus();
        }
    });
    
    multiplicar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double numero1, numero2, multiplicar;
            multiplicar = 0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            multiplicar = numero1 * numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText() + "" + "x" + "" + texto2.getText() + "" + "=" + multiplicar);
            texto1.setText(null);
            texto2.setText(null);
            texto1.requestFocus();
        }
    });
    
    dividir.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double numero1, numero2, dividir;
            dividir = 0;
            numero1 = Double.parseDouble(texto1.getText());
            numero2 = Double.parseDouble(texto2.getText());
            dividir = numero1 / numero2;
            showup.setVisible(true);
            showup.setText(texto1.getText() + "" + "/" + "" + texto2.getText() + "" + "=" + dividir);
            texto1.setText(null);
            texto2.setText(null);
            texto1.requestFocus();
        }
    });
    
      }
    
    }
    

    and Lista class...

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Lista extends JFrame{
      public Lista(){
        super("Menu");
    
        // Menu Bar
        JMenuBar barra = new JMenuBar();
        setJMenuBar(barra);
    
        // Menu
        JMenu opcoes = new JMenu("Options");
    
        // Menu Item
        JMenuItem item = new JMenuItem("Item 1");
    
        // actionlistener
        item.addActionListener(
          new ActionListener(){
            public void actionPerformed(ActionEvent e){
              new Calc(Lista.this);
            }
          }
        );
    
        opcoes.add(item);
    
        // Adds
        barra.add(opcoes);
    
        setSize(300, 150);
        setVisible(true);    
      }
    
      public static void main(String args[]){
        Lista app = new Lista();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }