popup menu:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS_zQVq-E6MV3Pq9zuP6uW5KGuykaiPPPkqAeoS4Lv16c7Vqw8sq5GM0Ldwsm3NbOkxirng2Y40DLrO9J0hWFhylQ7zR345c1RUz3wgp2RXSWmseqyzAGLV8Hmz8luXVnQEovnCyxfxA/s1600/6.png)
Sample program:
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class PopupMenu extends JFrame
{
private JPopupMenu menu;
private Toolkit toolkit;
public PopupMenu(String title)
{
super(title);
this.initUI();
}
private void initUI()
{
toolkit = this.getToolkit();
menu = new JPopupMenu();
JMenuItem menuItemBeep = new JMenuItem("Beep");
menuItemBeep.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
toolkit.beep();
}
});
menu.add(menuItemBeep);
JMenuItem menuItemClose = new JMenuItem("Close");
menuItemClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
menu.add(menuItemClose);
this.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON3)
{
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(250, 200);
this.setLocationRelativeTo(null);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
PopupMenu pm = new PopupMenu("JPopupMenu");
pm.setVisible(true);
}
});
}
}
No comments:
Post a Comment