Right click and start pop-up menu

xiaoxiao2021-03-05  26

Fuhong (original)

06/05/03

Everyone is very familiar with the use of popup menu.

We will explain how to use the right-click and pop-up menu in Java by the following example: The main program is a Java Application.

Call UserightButton, it is a JPanel, there is a JLabel on this JPanel to display the pop-up menu.

The result of the instruction execution. We ask when to right-click Jlabel or JPanel, pop up a menu, menu

There are three options in "Say Hello Again", "Say Hello Again", "Say Byebye". Select any of these instructions,

The corresponding String is displayed in the JLabel.

There are two class classes in this example. One is UserightButton (Java application), the other is MyPopupMenu

(Pop-up window). UserightButton's source program is as follows:

import javax.swing *;. import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt *;. public class UseRightButton extends JFrame implements MouseListener {JPANEL PANEL; PUBLIC UserightButton () {settitle ("Use Right Button and Popup Menu); Setsize (400, 300); Panel = New JPanel (); Display = New Jlabel (" "); Display.Setopaque true); display.setBackground (Color.yellow); panel.add (display); panel.addMouseListener (this); display.addMouseListener (this);. getContentPane () setLayout (new BorderLayout ()); getContentPane () add. (panel, BorderLayout.CENTER);} public static void main (String [] args) {UseRightButton rb = new UseRightButton (); rb.setVisible (true);} public void mousePressed (MouseEvent e) {if (e.getSource ( ) == Panel && E.getButton () == mouseevent.button3) {MyPopUpMenu popup = new mypopupnu (this); Popup.show ((Component) Panel, E.GETX (), ELETY ());} else if (E.Getsource () == Display && E.getButton () == mouseevent.button3) {MyPopUpMenu popup = new mypopupmenu (this); popup.show ((Component) Display, E.GETX (), E.GETY ());}} public void mouseentered (mouseEvent E) {} PUBLIC Void mouseexited (MouseEvent E) {} public void mouseclicked (mouseEvent e) {} public void mouseeleased (mouseEvent e) {}} We see from above:

As with the left mouse button, we use to control mouse events. What we have to do is to limit the mouseevent response to the right click, do not respond to the left mock button. This can be implemented by the following method to implement E.getButton () == mouseevent.button3 where the getButton () method returns an integer, mouseevent.button1, mouseevent.button2 or mouseevent.button3. MouseEvent.Button1 represents the left button, MouseEvent.Button3 represents right click. If your mouse has three keys, mouseevent.button2 represents the middle of the middle. Jlabel (Display) only occupies a small part of JPanel (we show its size and location with yellow background). And we ask when right-click the JPanel, including Jlabel, you have to pop up the menu, so we add JPanel and Jlabel to MouseListener. The source program of the pop-up menu Class is as follows:

import javax.swing *;. import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class MyPopupMenu extends JPopupMenu implements ActionListener {JMenuItem sayHello, sayHelloAgain, sayByeBye; UseRightButton useRightButton; public MyPopupMenu (UseRightButton urb) { useRightButton = urb; sayHello = new JMenuItem ( "Say Hello"); sayHelloAgain = new JMenuItem ( "Say Hello again"); sayByeBye = new JMenuItem ( "Say Bye Bye"); sayHello.addActionListener (this); sayHelloAgain.addActionListener ( this); sayByeBye.addActionListener (this); add (sayHello); this.addSeparator (); add (sayHelloAgain); add (sayByeBye);} public void actionPerformed (ActionEvent e) {if (e.getSource () == sayHello ) {System.out.println (); userightbutton.display.settext ("Hello!");} Else if (E.GetSource () == Sayelloagain) {s YSTEM.OUT.PRINTLN ("Hello! Hello!"); UserightButton.display.Settext ("Hello! Hello!");} else if (E.GetSource () == Saybyebye) {system.out.println ("bye Bye! "); UserightButton.display.Settext (" BYE BYE! ");}}} This program is simple and is a standard JPopupMenu. The only thing I need to point out is

In the constructor, we introduce the Base class of the pop-up menu, UserightButton. This is because

We want to return to the result of the instruction execution in the pop-up window to the original GUI interface. Obviously we

You cannot use UserightButton = New UserightButton () because both Class cannot reference each other.

For a complex application, we should use the Model-View-Controller architecture, ie

With pop-up menu When Controller, use Jlabel (Display) as view, then write a Model class

Receive string from the pop-up menu and automatically update the display in the JLabel. From the above example we see that the right mouse button is used to start the pop-up menu is very easy.

转载请注明原文地址:https://www.9cbs.com/read-35931.html

New Post(0)