简体   繁体   中英

how to call a method from another class Java Swing?

I have the following SwingMenu class. package base;

import javax.swing.*;

public class SwingMenu {
    public static void main(String[] args) {
        SwingMenu s = new SwingMenu();
    }

    public SwingMenu() {
        JFrame frame = new JFrame(
                "Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);
        frame.setJMenuBar(menubar);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

And I want to display the Menu by calling it from the this main class.

package base;

import javax.swing.*;

import java.awt.*;
import base.SwingMenu;

public class StickyNotes {

    private static void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");

        frame.setPreferredSize(new Dimension(5000, 5000));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        SwingMenu mainBar = new SwingMenu();
        //frame.setJMenuBar(mainBar);
        //frame.getContentPane().add(mainBar);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I just cannot figure it out all morning :) How do I get the Menu Bar to show up in Java Swing?

    // Add Main Menu
    SwingMenu mainBar = new SwingMenu();

You are creating 2 different JFrame s. After creating the JFrame :

JFrame frame = new JFrame("Java Sticky Notes");

Create the menu bar and assign it to the JFrame :

JMenuBar menubar = new JMenuBar();

// ...

frame.setJMenuBar(menubar);

No need for:

SwingMenu mainBar = new SwingMenu();

Try this out:

This basically makes SwingMenu a manu bar which will help encapsulating the build of the menu bar.

public class SwingMenu extends JMenuBar{
    public SwingMenu() {
        JMenu filemenu = new JMenu("File");
        JMenu editmenu = new JMenu("Edit");

        // Build your file menu and edit menu here...

        add(filemenu);
        add(editmenu);
    }
}

Now, in your createAndShowGUI() just create a form and add the newly created menu bar to it.

private static void createAndShowGUI() {
     // Create and set up the window.
     JFrame frame = new JFrame("Java Sticky Notes");
     // other stuff...
     // Add Main Menu
     SwingMenu mainBar = new SwingMenu();
     frame.setJMenuBar(mainBar);
     // Display the window.
     frame.pack();
     frame.setVisible(true);
}

Check this modified code example :

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private JMenuBar getMenuBar()
    {
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);

        return menubar;
    }

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        frame.setJMenuBar(getMenuBar());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

Or you can modify your code a bit like this, if you really wanted to keep the JMenuBar set up in a different Class, where you can simply make an Object of the SwingMenu Class and call the method getMenuBar() by making an Object of this Class :

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        SwingMenu swingMenu = new SwingMenu();
        frame.setJMenuBar(swingMenu.getMenuBar());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

class SwingMenu {

    public JMenuBar getMenuBar() 
    {   
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);

        return menubar;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM