-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_menuBar.java
More file actions
37 lines (30 loc) · 1.06 KB
/
simple_menuBar.java
File metadata and controls
37 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuBarExample {
public static void main(String[] args) {
// Create a new JFrame
JFrame frame = new JFrame("Menu Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Create a menu bar
JMenuBar menuBar = new JMenuBar();
// Create a menu
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
// Add action listener to exit item
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Add items to the menu
fileMenu.add(exitItem);
menuBar.add(fileMenu);
// Set the menu bar for the frame
frame.setJMenuBar(menuBar);
// Set the visibility to true
frame.setVisible(true);
}
}