Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
126 views
in JAVA by (178k points)
How do you create a menu bar in Java AWT?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

To create a menu bar in Java AWT, we can use the MenuBar and Menu classes. Here is an example code that creates a menu bar with a menu:

import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;

public class MyMenuBar {
   public static void main(String[] args) {
      Frame frame = new Frame("My Window");
      
      MenuBar menuBar = new MenuBar();
      Menu fileMenu = new Menu("File");
      MenuItem newItem = new MenuItem("New");
      MenuItem openItem = new MenuItem("Open");
      MenuItem exitItem = new MenuItem("Exit");
      
      fileMenu.add(newItem);
      fileMenu.add(openItem);
      fileMenu.addSeparator();
      fileMenu.add(exitItem);
      menuBar.add(fileMenu);
      
      frame.setMenuBar(menuBar);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
}
 

In this example code, we create a MenuBar object and a Menu object. We also create three MenuItem objects for the menu. We add the menu items to the menu using the add method and add a separator between the "Open" and "Exit" items using the addSeparator method. We add the menu to the menu bar using the add method and set the menu bar of the frame using the setMenuBar method.

When we run this code, a window with a menu bar containing the "File" menu will be displayed. When we click on the "File" menu, we will see the "New", "Open", and "Exit" items.

Related questions

0 votes
1 answer
0 votes
1 answer
asked May 1, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 1, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 1, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 1, 2023 in JAVA by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...