Adding Swing components from another class

2019-08-10 19:16发布

问题:

I am learning java, and I am trying to add a menu bar to my frame from another class (practicing dividing code into multiple classes to better organize the program).

Here is a sample of my code:

public class MainApp {

public static void main(String[] args) {
    // Create window
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(600, 400);

    // Create main panel
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    frame.add(content);

    //Create menu bar
    menubar menu = new menubar();
    content.add(menu.menuBar(), BorderLayout.NORTH);
            //Other stuff...

} // Ends main method
} // Ends MainApp class

And the menubar class:

public class menubar {
public static void menuBar(){
    JMenuBar menu = new JMenuBar();
    JMenu file = new JMenu("File");
        JMenuItem clear = new JMenuItem("New");
        JMenuItem exit = new JMenuItem("Exit");
    JMenu help = new JMenu("Help");
        JMenuItem about = new JMenuItem("About");
        JMenuItem instructions = new JMenuItem("Instructions");
} // Ends method menuBar
} // Ends class menubar

I use eclipse, and in the line:

content.add(menu.menuBar(), BorderLayout.NORTH);

the "add" is underlined, and as a result I am not able to compile the code.

I have been searching for a way to resolve this, and as far as I can tell this should work.

Any help is appreciated.

Thank you

Josh

回答1:

Note that your menuBar() method is a void type hence no value was returned, while the add() method of content (JPanel) you used requires two parameters which are (JComponent type [Note that JMenuBar is a subclass of JComponent], int [For Layouting Purposes])

content.add(menu.menuBar(), BorderLayout.NORTH);

Well a quick fix of your code is below:

public class menubar {
public static JMenuBar menuBar(){
    JMenuBar menu = new JMenuBar();
    JMenu file = new JMenu("File");
        JMenuItem clear = new JMenuItem("New");
        JMenuItem exit = new JMenuItem("Exit");
    JMenu help = new JMenu("Help");
        JMenuItem about = new JMenuItem("About");
        JMenuItem instructions = new JMenuItem("Instructions");
    return menu;
} // Ends method menuBar
} // Ends class menubar

My advice for you is to use the concept of inheritance (Extending menubar class to JMenuBar so that your class can act like JMenuBar) when dealing with GUI in Java rather than depending on the concept of composition. You can follow also the above post with regards to setting the JMenuBar:

frame.setJMenuBar(menu.menuBar());

If you are novice in Java Programming, you need to start practicing the Java Coding standards especially the proper naming of the Class and methods. The first letter of the Class's name should be capitalized while your names of the method should have at least a verb on it. :)



回答2:

Instead of:

content.add(menu.menuBar(), BorderLayout.NORTH);

I think you mean this:

content.add(menubar.menuBar(), BorderLayout.NORTH);

...but that still won't work because the return type of this method is void. It needs to be JMenuBar.



回答3:

The method menuBar has a void return type so is not applicable for the add method. so you would need

content.add(menubar.menuBar(), BorderLayout.NORTH);

while returning menu from the menuBar method

Aside: setJMenuBar is used to set a JMenuBar for a JFrame.

frame.setJMenuBar(menu.menuBar());


回答4:

I think you:

  • don't want to create new menubar() if menubar.menubar() is a static function
  • want public static JMenuBar menubar() { ... }, not void
  • want menubar.menubar() to return menu otherwise, that method was just an expensive no-op

content.add(menu.menuBar(), ...) is underlined because you can't use a void method as an argument to another method.

Also, you should always refer to a static field or method using the class, not an instance. And class names should start with a capital letter (Menu).



标签: java swing class