How to call a class through another class using Jmenuitem?

Asked

Viewed 544 times

1

I have two classes in the same package, one called Calc, which is a simple calculator, whose code is below:

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);}}

And I have another class called List, whose code is available below:

 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();}}

I want the JMenuItem called item, when clicking on it, open a new window with the contents of the Calc class, that is, I want to call the Calc class when interacting with the JMenuItem opening a new window with the calculator.

  • The first class is not complete, add it complete so it is possible to simulate the problem.

1 answer

0

Simply instantiate the class in actionPerformed.

item.addActionListener(
  new ActionListener(){
    public void actionPerformed(ActionEvent e){
       new Calc();
    }
  }
);

Browser other questions tagged

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