Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import javax.swing.*;
- class EnableDisableMenuExample extends JFrame {
- private JMenuBar menuBar;
- private JMenu fileMenu;
- public EnableDisableMenuExample() {
- setTitle("Enable and Disable Menu Example");
- setSize(300, 200);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- fileMenu = new JMenu("File");
- menuBar.add(fileMenu);
- JMenuItem openMenuItem = new JMenuItem("Open");
- fileMenu.add(openMenuItem);
- JMenuItem saveMenuItem = new JMenuItem("Save");
- fileMenu.add(saveMenuItem);
- JMenuItem closeMenuItem = new JMenuItem("Close");
- closeMenuItem.setEnabled(false);
- fileMenu.add(closeMenuItem);
- openMenuItem.addActionListener(e -> {
- // Perform some action
- closeMenuItem.setEnabled(true);
- });
- saveMenuItem.addActionListener(e -> {
- // Perform some action
- closeMenuItem.setEnabled(true);
- });
- closeMenuItem.addActionListener(e -> {
- // Perform some action
- closeMenuItem.setEnabled(false);
- });
- }
- }
- public class GUI{
- public static void main(String[] args) {
- EnableDisableMenuExample example = new EnableDisableMenuExample();
- example.setVisible(true);
- }
- }
Advertisement
Advertisement