how to send the class from one package to another class from another package

0

This is my window where the menu bar goes

package ventanas;

import java.awt.*;    
import java.awt.event.*;    
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import Menu.Archivos;
import javax.swing.*;

public class VentanaPrincipal extends JFrame {

    public static void MostrarVentanaPrincipal(){
        // Declaración de elementos.
        JFrame ventana;
        JPanel panel_01;
        JMenuBar barraMenu;

        //instancias
        ventana=new JFrame("Proyecto Dragon");
        panel_01=new JPanel(new BorderLayout());
        barraMenu= new JMenuBar();      

        //propiedades de la ventana principal
        ventana.setLayout(new BorderLayout());
        ventana.setBounds(0,0,800,600);
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //añadiendo el panel al jframe
        ventana.add(panel_01, BorderLayout.PAGE_START);

        //añadiendo la barra de menu al panel
        panel_01.add(barraMenu, BorderLayout.PAGE_START);

        //añadiendo el menu archivo a la barra de menu
        Archivos Arv = new Archivos(); 
        barraMenu.add(Arv);


        //mostrando la ventana principal
        ventana.setVisible(true);



}  
}

and this is my Files class:

package Menu;

import javax.swing.*;

public class Archivos extends JMenu{

private JMenu Archivo;

private JMenuItem nuevo,abrir,guardar,cerrar;

    public Archivos() {

        //instacias
        Archivo=new JMenu("Archivo");
        nuevo=new JMenuItem("Nuevo");
        abrir=new JMenuItem("Abrir");
        guardar=new JMenuItem("Guardar");
        cerrar=new JMenuItem("cerrar");

        //añadiendo los items del menu archivo
        Archivo.add(nuevo);
        Archivo.add(abrir);
        Archivo.add(guardar);
        Archivo.add(cerrar);


    }
}

at the moment I want to add file to the barmenu there is no error but when running it does not show anything in the menu bar

    
asked by Uriel Compean 22.06.2018 в 23:39
source

1 answer

0

Just like that, you would need a getter that returns the JMenu that you have called Archivo , it happens that Arv even if it extends from JMenu , it is NOT the object to which you have added the options of JMenuItem ; but let's go to the code ...

public JMenu getMenu() {
  return Archivo;
}

so in the main class you would need to correct for something like the following:

Archivos Arv = new Archivos();
barraMenu.add(Arv.getMenu());

Because basically Archivo is the attribute (component swing in this case) to which you have instantiated and added the JMenuItem . In that order of ideas up to class Archivos can without any problem DO NOT extend from JMenu

Now, another alternative way:

Archivos Arv = new Archivos();
barraMenu.add(Arv);

so that this code can add the menu the class Archivos should be modified like this:

public class Archivos extends JMenu{

//private JMenu Archivo;

private JMenuItem nuevo,abrir,guardar,cerrar;

    public Archivos() {
        //instacias
        //Archivo=new JMenu("Archivo");
        setText("Archivo");
        nuevo=new JMenuItem("Nuevo");
        abrir=new JMenuItem("Abrir");
        guardar=new JMenuItem("Guardar");
        cerrar=new JMenuItem("cerrar");

        //añadiendo los items del menu archivo
        /*Archivo.add(nuevo);
        Archivo.add(abrir);
        Archivo.add(guardar);
        Archivo.add(cerrar);*/
        add(nuevo);
        add(abrir);
        add(guardar);
        add(cerrar);
    }

    /*public JMenu getMenu() {
      return Archivo;
    }*/
}

In this case, there is nowhere a component JMenu (except comments), but the JMenuItem is added directly to the class that is type JMenu .

After all the above I got this capture:

    
answered by 23.06.2018 в 06:05