Tuesday, January 25, 2011

How to Make Menu Bar in Java using Swing Libary

Menu bar is an essential part of application,because menu bar is give general features. okay let's make menu bar, first open your IDE->make a new project->make a new class (for instance:Class Name:MenuBarExercise.java)


Copy the code bellow:


import javax.swing.*;

public class MenuBarExercise {

private JFrame _frame = new JFrame("Exercise Menu Bar");
private JMenuBar _menubar = new JMenuBar();
private JMenu File = new JMenu("File");
private JMenu Data_Master = new JMenu("Master Data");
private JMenu Transaction = new JMenu("Transaction");
private JMenu About = new JMenu("About");
private JMenuItem Exit = new JMenuItem("Exit");
private JMenuItem Account = new JMenuItem("Account");
private JMenuItem Transfer = new JMenuItem("Transfer");
private JMenuItem Save = new JMenuItem("Save Money");
private JMenuItem Take = new JMenuItem("Take Money");

public MenuBarExercise() {
//Set property of JFrame component
_frame.setVisible(true);
_frame.setTitle("Exercise User Interface");
_frame.setExtendedState(_frame.MAXIMIZED_BOTH);
_frame.setDefaultCloseOperation(_frame.EXIT_ON_CLOSE);

//Add Menu Bar into JFrame
_frame.setJMenuBar(_menubar);

// Set Menu to Menu Bar
_menubar.add(File);
_menubar.add(Data_Master);
_menubar.add(Transaction);
_menubar.add(About);

// Set Menu Item to Menu (File Menu)
File.add(Exit);
Transaction.add(Transfer);
Transaction.add(Save);
Transaction.add(Take);
Data_Master.add(Account);
}

public static void main(String[] args) {
new MenuBarExercise();
}
}

if the code is executed correctly it will display like this:


Congratulation you have made MenuBar in your java application. I hope this tutorial can help...Thank you..^^

No comments:

Post a Comment