2 jackscsnd jackscsnd 于 2015.05.28 19:03 提问

JAVA popupMenu弹出菜单了,怎样实现点击项产生事件

如图:图片说明我弹出的这个PopupMenu ,想在点击添加好友响应事件,但是他ActionListener,该怎么样去
响应事件,求解,刚那个朋友说的不对。求解。

3个回答

tongyi55555
tongyi55555   2015.05.28 22:19
已采纳

 import java.awt.BorderLayout;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

/**
 * 右键弹出菜单测试类
 */
public class PopupMenuTest extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1282599344832870566L;
    private JTextArea txtArea;
    private JMenuItem menuItem;
    private JPopupMenu popMenu;

    public PopupMenuTest() {
        setSize(200, 200);
        setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout());

        txtArea = new JTextArea();
        panel.add(txtArea, BorderLayout.CENTER);
        txtArea.setText("右键清除内容");

        popMenu = new JPopupMenu();

        menuItem = new JMenuItem("清除");
        menuItem.addActionListener(actionListener);

        popMenu.add(menuItem);

        txtArea.addMouseListener(mouseAdapter);
    }

    /**
     * 文本区鼠标监听事件,右键弹出菜单。
     */
    private MouseAdapter mouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            if (e.getButton() == MouseEvent.BUTTON3) {
                popMenu.show(txtArea, e.getX(), e.getY());
            }
        }
    };

    /**
     * 弹出菜单响应事件
     */
    private ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getSource() == menuItem) {
                txtArea.setText("");
            }
        }
    };

    public static void main(String[] args) {
        PopupMenuTest popTest = new PopupMenuTest();
        popTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        popTest.setVisible(true);
    }
}

qq_28559121
qq_28559121   2015.05.28 19:14

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ShowDIalog extends JFrame{
JButton button=new JButton("显示");
public ShowDIalog(){
setLayout(new FlowLayout());
add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Wath a fucking day!");
}
});
setVisible(true);
setSize(100,100);
}
public static void main(String[] args){
ShowDIalog s=new ShowDIalog();
}

jackscsnd
jackscsnd 你写的不对。
2 年多之前 回复
jackscsnd
jackscsnd 谢谢, 我试试啊
2 年多之前 回复
danielinbiti
danielinbiti   Ds   Rxr 2015.05.28 22:03
 import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PopupTest extends JFrame { 
   private JRadioButtonMenuItem items[]; 
   private final Color colorValues[] = 
      { Color.BLUE, Color.YELLOW, Color.RED }; 
   private JPopupMenu popupMenu; 

   // set up GUI 
   public PopupTest() 
   { 
      super( "Using JPopupMenus" ); 

      ItemHandler handler = new ItemHandler(); 
      String colors[] = { "Blue", "Yellow", "Red" }; 

      // set up popup menu and its items 
      ButtonGroup colorGroup = new ButtonGroup(); 
      popupMenu = new JPopupMenu(); 
      items = new JRadioButtonMenuItem[ 3 ]; 

      // construct each menu item and add to popup menu; also 
      // enable event handling for each menu item 
      for ( int count = 0; count < items.length; count++ ) { 
         items[ count ] = new JRadioButtonMenuItem( colors[ count ] ); 
         popupMenu.add( items[ count ] ); 
         colorGroup.add( items[ count ] ); 
         items[ count ].addActionListener( handler ); 
      } 

      getContentPane().setBackground( Color.WHITE ); 

      // declare a MouseListener for the window that displays 
      // a JPopupMenu when the popup trigger event occurs 
      addMouseListener( 

         new MouseAdapter() {  // anonymous inner class 

            // handle mouse press event 
            public void mousePressed( MouseEvent event ) 
            { 
               checkForTriggerEvent( event ); 
            } 

            // handle mouse release event 
            public void mouseReleased( MouseEvent event ) 
            { 
               checkForTriggerEvent( event ); 
            } 

            // determine whether event should trigger popup menu 
            private void checkForTriggerEvent( MouseEvent event ) 
            { 
               if ( event.isPopupTrigger() ) 
                  popupMenu.show( 
                     event.getComponent(), event.getX(), event.getY() );  
            } 

         } // end anonymous inner clas 

      ); // end call to addMouseListener 

      setSize( 300, 200 ); 
      setVisible( true ); 

   } // end constructor PopupTest 

   public static void main( String args[] ) 
   { 
      PopupTest application = new PopupTest(); 
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
   } 

   // private inner class to handle menu item events 
   private class ItemHandler implements ActionListener { 

      // process menu item selections 
      public void actionPerformed( ActionEvent event ) 
      { 
         // determine which menu item was selected 
         for ( int i = 0; i < items.length; i++ ) 
            if ( event.getSource() == items[ i ] ) { 
               getContentPane().setBackground( colorValues[ i ] ); 
               return; 
            } 
      } 

   } // end private inner class ItemHandler 

} // end class PopupTest 
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!