Popup menu is a menu that is displayed when you right-click in the window. The following program is an example of a program to make a popup menu in Java. Class that is used is the JPopupMenu.
Here is the display program:
Here's the source code program:
01 import java.awt .*;02 import java.awt.event .*;03 import javax.swing .*;0405 public class extends JFrame {PopupMenuTest06 private JRadioButtonMenuItem items [];ColorValues 07 private final Color [] = {Color.BLUE, Color.YELLOW, Color.red};08 private JPopupMenu popMenu;0910 public PopupMenuTest () {11 super ("Popup Menu");1213 ItemHandler ItemHandler handler = new ();14 colorNames String [] = {"Blue", "Yellow", "Red"};1516 ButtonGroup colorGroup = new ButtonGroup ();17 popMenu = new JPopupMenu ();18 items = new JRadioButtonMenuItem [colorValues.length];1920 for (int i = 0; i <items.length; i + +) {21 items [i] = new JRadioButtonMenuItem (colorNames [i]);22 popMenu.add (items [i]);23 colorGroup.add (items [i]);24 items [i]. AddActionListener (handler);25}2627 getContentPane (). SetBackground (Color.WHITE);2829 addMouseListener (30 new MouseAdapter () {31 public void mousePressed (MouseEvent e) {ShowPopupMenu 32 (e);33}3435 public void mouseReleased (MouseEvent e) {ShowPopupMenu 36 (e);37}3839 private void showPopupMenu (MouseEvent e) {40 if (e.isPopupTrigger ())41 popMenu.show (e.getComponent (), e.getX (), e.getY ());42}43} / / end of anonymous class44); / / end of addMouseListener4546 setSize (400.300);47 setLocationRelativeTo (null);48 setVisible (true);49}5051 public static void main (String args []) {52 JFrame.setDefaultLookAndFeelDecorated (true);53 PopupMenuTest PopupMenuTest test = new ();54 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);55}5657 private class implements ActionListener {ItemHandler58 public void actionPerformed (ActionEvent e) {59 / /60 for (int i = 0; i <items.length; i + +) {61 if (e.getSource () == items [i]) {62 getContentPane (). SetBackground (colorValues [i]);63 return;64}65}66}67}68}
May be useful. [OOT] Visit this site to get a free book PHP and MySQL.
Langganan:
Posting Komentar (Atom)
0 komentar: