2012-12-01 7 views
5

मेरे पास एक जावा प्रोग्राम है जो एक जेपीनेल में दायाँ क्लिक करते समय पॉपअप मेनू खोलता है। जब कोई भी पॉपअप मेनू आइटम क्लिक किया जाता है, तो मैं सही क्लिक के स्थान को प्रिंट करना चाहता हूं जो टर्मिनल में पॉपअपमेनू को ट्रिगर करता है। मैं यह कैसे करु? मैं पॉपअप एक्शन इवेंट्स के भीतर से दायाँ क्लिक कहां से स्थान प्राप्त कर सकता हूं?पॉपमेनू एक्शन इवेंट से दायाँ क्लिक स्थान प्राप्त करना

पॉपअप मेनू JComponent में कोड बदलने पर कोड कैसे बदलता है?

यहां कार्यक्रम है।

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

    public class MenuTest 
    { 
     public static void main(String[] args) 
     { 
      EventQueue.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        MenuFrame frame = new MenuFrame(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
       } 
      }); 
     } 
    } 

    class MenuFrame extends JFrame 
    { 
     public MenuFrame() 
     { 
      setTitle("MenuTest"); 
      setSize(300, 200); 

      Action cutAction = new TestAction("Cut"); 
      Action copyAction = new TestAction("Copy"); 
      Action pasteAction = new TestAction("Paste"); 

      JPopupMenu popup = new JPopupMenu(); 
      popup.add(cutAction); 
      popup.add(copyAction); 
      popup.add(pasteAction); 

      JPanel panel = new JPanel(); 
      panel.setComponentPopupMenu(popup); 
      add(panel); 

      panel.addMouseListener(new MouseAdapter() {}); 
     } 

     class TestAction extends AbstractAction 
     { 
      public TestAction(String name) 
      { 
       super(name); 
      } 

      public void actionPerformed(ActionEvent event) 
      { 
       System.out.println("Right click happened at ?"); // How do I get right click location?   
      } 
     } 
    } 

उत्तर

4

दबाया घटनाओं, के लिए एक माउस श्रोता जोड़े जरूरत

private Dimension clickLocation; 

    public TestAction(String name, Dimension clickLocation) { 
     super(name); 
     this.clickLocation = clickLocation; 
    } 

    public void actionPerformed(ActionEvent event) { 
     System.out.println("Right click happened at " + clickLocation); 
    } 
+0

धन्यवाद गेटेट। – user1868856

3

आप सही रास्ते पर थे। मैं व्यक्तिगत रूप से इसे MouseAdapter में मैन्युअल रूप से दिखाना पसंद करता हूं ताकि मैं अन्य माउसवेंट्स पर विधियां जोड़ सकूं। आयाम

panel.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent e) { 
      clickLocation.setSize(e.getX(), e.getY()); 
     } 
    }); 
    Action cutAction = new TestAction("Cut", clickLocation); 
    Action copyAction = new TestAction("Copy", clickLocation); 
    Action pasteAction = new TestAction("Paste", clickLocation); 

प्रिंट आउट:: इस के लिए आप शायद (क्लिक किया घटनाओं पॉपअप द्वारा कब्जा कर लिया हो) को दूर करने के panel.setComponentPopupMenu(popup);


panel.addMouseListener(new MouseAdapter() { 

     @Override 
     public void mouseClicked(MouseEvent arg0) { 
      if (arg0.getButton() == MouseEvent.BUTTON3) { //Button3 is rightclick 
       popup.show(panel, arg0.getX(), arg0.getY()); 
      } 
     } 
}); 
+0

धन्यवाद शिप्पी। – user1868856

+0

नोट: किसी को 'mouseClicked' में अंधाधुंध करने के बजाय 'isPopupTrigger' का उपयोग करना चाहिए - देखें [मैं जावा स्विंग में राइट क्लिक संदर्भ मेनू कैसे बना सकता हूं?] (Http://stackoverflow.com/a/767254/16673) – Suma

+0

आप 'arg0.getButton() == MouseEvent.BUTTON3' को 'SwingUtilities.isRightMouseButton (arg0)' के साथ बदल सकते हैं। तो आपको बटन नंबर याद रखने की ज़रूरत नहीं है। –

0

यहां वह कोड है जिसे मैं ढूंढ रहा था। आपकी मदद के लिए धन्यवाद Schippi और Garret।

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

    public class MenuTest 
    { 
     public static void main(String[] args) 
     { 
      EventQueue.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        MenuFrame frame = new MenuFrame(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
       } 
      }); 
     } 
    } 

    class MenuFrame extends JFrame 
    { 
     public MenuFrame() 
     { 
      setTitle("MenuTest"); 
      setSize(300, 200); 

      Action cutAction = new TestAction("Cut"); 
      Action copyAction = new TestAction("Copy"); 
      Action pasteAction = new TestAction("Paste"); 

      JPopupMenu popup = new JPopupMenu(); 
      popup.add(cutAction); 
      popup.add(copyAction); 
      popup.add(pasteAction); 

      JPanel panel = new JPanel(); 
      panel.setComponentPopupMenu(popup); 
      add(panel); 

      panel.addMouseListener(new MouseAdapter() { 
       @Override 
       public void mousePressed(MouseEvent e) { 
        clickLocation= e.getPoint(); 
       } 
      }); 
     } 

     class TestAction extends AbstractAction 
     { 
      public TestAction(String name) 
      { 
       super(name); 
      } 

      public void actionPerformed(ActionEvent event) 
      { 
       System.out.println("Right click happened at (" + clickLocation.getX()+"," + clickLocation.getY()+ ")");   
      } 
     } 

     private Point2D clickLocation; 
    } 
0

या यदि आप इसे ईवेंट से प्राप्त नहीं करना चाहते हैं।

Point mousepospoint=null; 

if((mousepospoint=componentname.getMousePosition()) != null){ 
//mouseposArray[0]=mousepospoint.x; 
//mouseposArray[1]=mousepospoint.y; 
mousepoints(mousepospoint.x,mousepospoint.y); 
}//enif 


int[] mouseposArray={0,0}; 
// requires a function to return it if mouseposArray[] is global 
protected int[] mousepoints(int xpo,int ypo){ 
mouseposArray=new int[2]; 
mouseposArray[0]=xpo; 
mouseposArray[1]=ypo; 
return mouseposArray; 
}//enmeth 
संबंधित मुद्दे