简体   繁体   中英

How do I add buttons to this code? I do not know how or where to put it

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);    
    }
 }

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 .

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 .

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