1

Good day! I am trying to learn JOptionPane. I search a code online and tried to analyze it through running the code and searching online but I still cannot understand some part of it. Can you please elaborate those code with comments, particularly the javax.swing, public JOPmenu(), createMenuBar(), setJMenuBar and String [] args.

javax.swing.*;** //what's the difference between this and javax.swing.JOptionPane
import java.awt.EventQueue;  
import java.awt.event.KeyEvent;

public class JOpMenu extends JFrame
{
    public JOpMenu()
    {
        **initUI();** // can I delete this class and just directly insert the code from initUI
    }

    public void initUI()
    {
        createMenuBar(); // whats the purpose of this

        setTitle("CHS Presentation");
        setSize(560, 350);
        setLocationRelativeTo(null);
    }

    private void createMenuBar()
    {
        var menuBar = new JMenuBar();

        var fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        var newMenuItem = new JMenuItem("New");
        newMenuItem.setMnemonic(KeyEvent.VK_N);
        var openMenuItem = new JMenuItem("Open");
        openMenuItem.setMnemonic(KeyEvent.VK_O);
        var saveMenuItem = new JMenuItem("Save");
        saveMenuItem.setMnemonic(KeyEvent.VK_S);
        var exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.setMnemonic(KeyEvent.VK_E);

        exitMenuItem.addActionListener((event) -> System.exit(0));

        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);

        var editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);

        var undoMenuItem = new JMenuItem("Undo");
        undoMenuItem.setMnemonic(KeyEvent.VK_U);
        var redoMenuItem = new JMenuItem("Redo");
        redoMenuItem.setMnemonic(KeyEvent.VK_R);
        var copyMenuItem = new JMenuItem("Copy");
        copyMenuItem.setMnemonic(KeyEvent.VK_C);
        var pasteMenuItem = new JMenuItem("Paste");
        pasteMenuItem.setMnemonic(KeyEvent.VK_P);
        var selectMenuItem = new JMenuItem("Select");
        selectMenuItem.setMnemonic(KeyEvent.VK_S);

        editMenu.add(undoMenuItem);
        editMenu.add(redoMenuItem);
        editMenu.addSeparator();
        editMenu.add(copyMenuItem);
        editMenu.add(pasteMenuItem);
        editMenu.add(selectMenuItem);

        var helpMenu = new JMenu("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);

        var aboutMenuItem = new JMenuItem("About");
        aboutMenuItem.setMnemonic(KeyEvent.VK_A);

        helpMenu.add(aboutMenuItem);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);

        setJMenuBar(menuBar); //why the menu disappear when I remove this
    } 

    public static void main(String[] args) // why does the string args declaration is in here
    {
        EventQueue.invokeLater(() -> {
            var ex = new JOpMenu();
            ex.setVisible(true);
        });
    } 
}

Thanks!

zultz
  • 224
  • 2
  • 9
  • *"//what's the difference between this and javax.swing.JOptionPane"* - [What's the difference between a package and an import?](https://stackoverflow.com/questions/23580284/whats-the-difference-between-a-package-and-an-import) – MadProgrammer Feb 06 '20 at 00:00
  • *"`// can I delete this class and just directly insert the code from initUI`"* - Sure, give it a go – MadProgrammer Feb 06 '20 at 00:01
  • *"`//why the menu disappear when I remove this`"* because you no have set the menu bar. Maybe [`JFrame@setJMenuBar`](https://docs.oracle.com/javase/10/docs/api/javax/swing/JFrame.html#setJMenuBar(javax.swing.JMenuBar)), [How to Use Menus](https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) and [How to Use Root Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) might be better places to start – MadProgrammer Feb 06 '20 at 00:03
  • *"`// why does the string args declaration is in here`"* - That's really a fundamental concept for Java, maybe start with [the Java Trails](https://docs.oracle.com/javase/tutorial/getStarted/application/index.html) – MadProgrammer Feb 06 '20 at 00:04
  • I thought string [] args should be on top under the class – zultz Feb 06 '20 at 01:58
  • No `main` can appear any where in the main class - the order is unimportant and unlike some languages, like C, won't effect the compilation process – MadProgrammer Feb 06 '20 at 02:41
  • Oh ok, one last question, why do I need to setVisible for the class? Can I remove it? – zultz Feb 07 '20 at 14:46
  • Sure, try it and see what happens – MadProgrammer Feb 07 '20 at 23:53
  • Ok, thanks for your help! – zultz Feb 08 '20 at 01:25

1 Answers1

0
  1. JOpMenu()

JOpMenu is the constructor of your class and hence it will be called implicitly when you create an instance/object of your class launching the initUI function. Hence you will call the initUI function automatically. It is also considered a good coding practice to launch your UI using a constructor, there are other ways in which you don't have to call initUI at all (that's a different topic), and yes you can directly write initUI() it will still work.

  1. createMenuBar()

This just a call to a user-defined function which is there in the code

  1. setMenuBar()

This is a JFrame component using which you can add a menu bar to your frame.
Here JFrame is extended to the class. But if your creating a JFrame explicitly then the syntax will be:

JFrame frame_name = new JFrame();
frame_name.setMenuBar(menuBar);
  1. String[] args

This is used when you are running your code on a command prompt and you have to take input. All the inputs given will be stored in the args array in string format and you can use them just as you use array elements. specifically in this program if you remove String[] args your code will still run fine even on command prompt as you are not taking any inputs from the user.

phwt
  • 1,356
  • 1
  • 22
  • 42