It's easy to use JMenuBar in your JFrame. These are the simple steps to use JMenubar:
1. Copy the setMenuBar() method below
2. Paste the method in your class that extends JFrame
3. Configure the method on your own
4. Add a line "setMenuBar();" at the bottom of the constructor of your JFrame class
The following is an example of using JMenuBar. Just copy the setMenuBar() method and configure it on your own.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class MyJFrame extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyJFrame frame = new MyJFrame(); frame.setTitle("JmenuBar example"); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MyJFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); setMenuBar(); //set JMenubar } private void setMenuBar(){ JMenuBar menuBar = new JMenuBar(); menuBar.setBackground(Color.lightGray); //////////////////// File JMenu fileMenu = new JMenu("File"); JMenuItem openMenuItem = new JMenuItem("Open File"); openMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }); fileMenu.add(openMenuItem); JMenuItem saverf5xAndMenuItem = new JMenuItem("Save"); fileMenu.add(saverf5xAndMenuItem); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); ////////////////////Edit JMenu editMenu = new JMenu("Edit"); JMenuItem copyMenuItem = new JMenuItem("ergedgvdfergv"); copyMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); editMenu.add(copyMenuItem); JMenuItem copyOriginalrf5xMenuItem = new JMenuItem("SFdfsrfs"); copyOriginalrf5xMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); editMenu.add(copyOriginalrf5xMenuItem); JMenuItem clearMenuItem = new JMenuItem("sferfgedrg"); clearMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); editMenu.add(clearMenuItem); JMenuItem clearrf5xMenuItem = new JMenuItem("egdfgvd"); clearrf5xMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); editMenu.add(clearrf5xMenuItem); JMenuItem clearBothMenuItem = new JMenuItem("rthfghrth"); clearBothMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); editMenu.add(clearBothMenuItem); menuBar.add(editMenu); ////////////////////Help JMenu helpMenu = new JMenu("Help"); JMenuItem aboutrf5xMenuItem = new JMenuItem("About"); aboutrf5xMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }); helpMenu.add(aboutrf5xMenuItem); menuBar.add(helpMenu); this.setJMenuBar(menuBar); } }
No comments:
Post a Comment