📜  Java AWT PopupMenu(1)

📅  最后修改于: 2023-12-03 15:15:55.460000             🧑  作者: Mango

Java AWT PopupMenu

Java AWT (Abstract Window Toolkit) provides a PopupMenu class that is used to display a small window that pops up and displays a list of choices. This menu appears when the user clicks on a designated area on the screen. The PopupMenu class is a part of the java.awt package.

Creating a PopupMenu

To create a PopupMenu, we first need to create an instance of PopupMenu class by using the new keyword. We then can add MenuItems to it by using the add() method. Finally, we need to attach this menu to a component where we want to display it.

//Creating a PopupMenu object
PopupMenu popupMenu = new PopupMenu();

//Adding MenuItems to the PopupMenu
popupMenu.add(new MenuItem("Option 1"));
popupMenu.add(new MenuItem("Option 2"));
popupMenu.add(new MenuItem("Option 3"));

//Adding PopupMenu to a component
component.add(popupMenu);
Displaying a PopupMenu

To display a PopupMenu, we need to add a MouseListener to a component where we want to show this menu. We then use the show() method to display this menu on the screen.

//Adding MouseListener to a component
component.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON3) { //Right-click detected
         popupMenu.show(component, e.getX(), e.getY()); //Showing the PopupMenu
      }
   }
});

In this example, we have added a MouseListener to the component, which listens for a right-click event. If this event is detected, we show the PopupMenu by calling the show() method and passing in the component where we want to show the menu and the x and y coordinates where we want to display it.

Complete Example
import java.awt.*;
import java.awt.event.*;

public class PopupMenuExample {
   public static void main(String[] args) {
      //Creating the Frame
      Frame frame = new Frame("PopupMenu Example");

      //Creating a PopupMenu object
      PopupMenu popupMenu = new PopupMenu();

      //Adding MenuItems to the PopupMenu
      popupMenu.add(new MenuItem("Option 1"));
      popupMenu.add(new MenuItem("Option 2"));
      popupMenu.add(new MenuItem("Option 3"));

      //Adding PopupMenu to a component
      frame.add(popupMenu);

      //Adding MouseListener to a component
      frame.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) { //Right-click detected
               popupMenu.show(frame, e.getX(), e.getY()); //Showing the PopupMenu
            }
         }
      });

      //Setting the size, layout, and visibility of the Frame
      frame.setSize(300, 300);
      frame.setLayout(null);
      frame.setVisible(true);
   }
}

In this example, we have created a Frame and added a PopupMenu to it. We then added a MouseListener to the Frame, which listens for a right-click event. If this event is detected, we show the PopupMenu by calling the show() method and passing in the Frame where we want to show the menu and the x and y coordinates where we want to display it.

This is how we can create and display a PopupMenu in Java AWT.