0

How do I add buttons to this code? I have written a code with menu bars but I do not know where to add button codes (JButton). My goal is to write a program that has menu bars and button, not within the toolbar, but within the applet that opens up. The menu bars are just for design as the options have no actionlisteners. (Sorry I am new here)

public class MenuBar extends JFrame {

    public MenuBar() {

        setTitle("Car Selection");
        setSize(300, 300);

        JMenuBar menuBar = new JMenuBar();
        JMenu exit;

        setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu aboutMenu = new JMenu("About");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(aboutMenu);
        menuBar.add(helpMenu);

        JMenuItem newAction = new JMenuItem("New");
        JMenuItem openAction = new JMenuItem("Open");
        JMenuItem exitAction = new JMenuItem("Exit");
        JMenuItem saveAction = new JMenuItem("Save");
        JMenuItem saveasAction = new JMenuItem("Save As");
        JMenuItem refreshAction = new JMenuItem("Refresh");

        JMenuItem undoAction = new JMenuItem("Undo");
        JMenuItem redoAction = new JMenuItem("Redo");
        JMenuItem cutAction = new JMenuItem("Cut");
        JMenuItem copyAction = new JMenuItem("Copy");
        JMenuItem pasteAction = new JMenuItem("Paste");
        JMenuItem optionAction = new JMenuItem("Options");

        JMenuItem registerAction = new JMenuItem("Register");
        JMenuItem versionAction = new JMenuItem("Version");
        JMenuItem aboutAction = new JMenuItem("About");
        JMenuItem policyAction = new JMenuItem("Policy");
        JMenuItem updatesAction = new JMenuItem("Updates");

        JMenuItem visitwebAction = new JMenuItem("Visit Web");
        JMenuItem tutorialsAction = new JMenuItem("Tutorials");
        JMenuItem feedbackAction = new JMenuItem("Feedback");
        JMenuItem dailynewsAction = new JMenuItem("Daily News");
        JMenuItem contactusAction = new JMenuItem("Contact Us");

        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(saveAction);
        fileMenu.add(saveasAction);
        fileMenu.add(refreshAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);

        editMenu.add(undoAction);
        editMenu.add(redoAction);
        editMenu.add(cutAction);
        editMenu.add(copyAction);
        editMenu.add(pasteAction);
        editMenu.addSeparator();
        editMenu.add(optionAction);

        aboutMenu.add(registerAction);
        aboutMenu.add(versionAction);
        aboutMenu.add(aboutAction);
        aboutMenu.add(policyAction);        
        aboutMenu.addSeparator();
        aboutMenu.add(updatesAction);

        helpMenu.add(visitwebAction);
        helpMenu.add(tutorialsAction);
        helpMenu.add(feedbackAction);
        helpMenu.add(dailynewsAction);
        helpMenu.addSeparator();
        helpMenu.add(contactusAction);

    }

    public static void main(String[] args) {
        MenuBar me = new MenuBar();
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.setVisible(true);    
    }
 }
Nobel
  • 13
  • 1
  • 5
  • See [this answer](http://stackoverflow.com/a/21447111/2587435) for how to correctly use `Action` for menus and tool bars. Also see [How to Use Actions](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) – Paul Samsotha Apr 11 '14 at 03:28

2 Answers2

4

I think you are talking about using a JToolBar.

Read the section from the Swing tutorial on How to Use Toolbars for more information and examples.

Also, you will want to write your code using Actions, so that the Action can be shared by the menu item and the toolbar. The tutorial also has a section on How to Use Actions.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Try this:

JButton jb = new JButton()
JPanel p = new JPanel(new BorderLayout());
p.add(jb)
getContentPane().add(p);

And really, you can put it at the beginning just before your declaration of JMenuBar menuBar.

JavaDocs Tutorials or JavaDocs for JButton

Edit: In response to your comment:

Instead of using a BorderLayout, use an AbsoluteLayout, or something else. Try these pages:

Info and usage.

Ethan Brouwer
  • 975
  • 9
  • 32
  • As for a bit more elaboration. I would like to resize the button so it doesn't cover the entire panel. Also I am hoping I can use actionlisteners on the button to open up another panel. @camickr Interesting tutorial but unfortunately it wasn't what I was looking for – Nobel Apr 11 '14 at 04:48
  • @user3521937 Updated answer about layouts. They're just different ways to display your data on the swing window. Have you ever thought about using eclipse (with window builder). Try this page: http://www.vogella.com/tutorials/EclipseWindowBuilder/article.html but instead of creating an swt window, create a swing window. Look at this for more reference: http://www.java-forums.org/blogs/eclipse/795-using-windowbuilder-design-gui-eclipse.html – Ethan Brouwer Apr 11 '14 at 04:52