How do I display menus and panels in the same window of Java swing?

I have a JMenu. I want to change the contents of the window according to the buttons in the menu I try to display the panel as a pop-up window, but I want it to appear in the same window as the menu So far, this is my code:

public class GUImenu extends JFrame


{
      private JMenuBar menuBar;   
       private JMenu menu;          
       private JMenu subMenu;    
       private JMenuItem item1;
       private JMenuItem item2;
       private JMenuItem item3;
       private JMenuItem item4;
       private JMenuItem item5;
       private JMenuItem item6;

       public GUImenu()
       {
          super("Example Menu System");// Call the JFrame constructor.
          setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);   // Specify an action for the close button.
          buildMenuBar();

          // Pack and display the window.
          pack();
          setSize(1000,250); // set frame size
          setVisible(true);
       }

       private void buildMenuBar()
       {
          // Create the menu bar.
          menuBar = new JMenuBar();

          // Create the file and text menus.
          menu = new JMenu("Menu"); menuBar.add(menu);
          subMenu = new JMenu("Create Customer");
          item1 = new JMenuItem("Ordinary Customer"); subMenu.add(item1);
          item1.addActionListener(new showOrdinaryCust());
          item6 = new JMenuItem("Privileged Customer"); subMenu.add(item6);

          menu.add(subMenu);
          item2 = new JMenuItem("View Customers Who Didn't Pay"); menu.add(item2);
          item3 = new JMenuItem("Remove Client");menu.add(item3);
          item4 = new JMenuItem("Create Order"); menu.add(item4);
          item5 = new JMenuItem("Search..."); menu.add(item5);
          setJMenuBar(menuBar);

       }

       public static void main(String[] args)
       {
          new GUImenu();
       }
       private class showOrdinaryCust implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
              if(event.getSource()==item1)
                  GUIpanel.main(null);

          }
       }
 }

Solution

I'll try to fill the whole window with CardLayout CardLayout is used to switch its contents between different views Just set up multiple cards for each panel you want to display and switch menus between them

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>