2014-06-07 8 views
6

एक सहायता प्रणाली पर काम करना, मैं चाहता हूं कि प्रत्येक घटक कुछ मदद प्रदान करे जब माउस खत्म हो जाए और "?" कुंजी दबाया जाता है। टूलटिप्स की तरह सॉर्ट करें, अधिक व्यापक सहायता के अलावा - अनिवार्य रूप से एक छोटा सा वेब ब्राउज़र पॉप, छवियों या अन्य चीज़ों को पॉप अप और प्रदर्शित करना है।, माउस कहां जाने के लिए मैं कुंजीपटल इनपुट की व्यवस्था कैसे कर सकता हूं?

मुझे जो मिल रहा है वह यह है कि माउस कहां है, इनपुट हमेशा एक ही KeyListener पर जाता है। क्या एक समय में केवल एक सक्रिय होना चाहिए?

इसके लायक होने के लिए, यह अब कामकाजी संस्करण है - सुझावों के लिए धन्यवाद!

 
    /** 
    * Main class JavaHelp wants to support a help function so that when 
    * the user types F1 above a component, it creates a popup explaining 
    * the component. 
    * The full version is intended to be a big brother to tooltips, invoking 
    * an HTML display with clickable links, embedded images, and the like. 
    */ 


    import javax.swing.*; 
    import javax.swing.border.Border; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.KeyListener; 

    class Respond2Key extends AbstractAction 
    { 
    Component jrp; 

    // Contract consructor 
    public Respond2Key(String text) 
    { 
     super(text); 
    } 

    // Constructor that makes sure it gets done right 
    public Respond2Key(String text, Component jrpIn) 
    { 
     super(text); 
     System.out.println("creating Respond2Key with component " + jrpIn 
             .toString 
             ()); 
     jrp = jrpIn; 
    } 

    public void setJrp(Component j) { 
     jrp = j; 
    } 


    // Functionality: what is the response to a key 
    public void actionPerformed(ActionEvent e) 
    { 
     // use MouseInfo to get position, convert to pane coords, lookup component 
     Point sloc = MouseInfo.getPointerInfo().getLocation(); 

     SwingUtilities.convertPointFromScreen(sloc, (Component) jrp); 

     Component c = jrp.getComponentAt(sloc); 
     System.out.printf("Mouse at %5.2f,%5.2f Component under mouse is %s\n", 
       sloc.getX(), sloc.getY(), c.toString()); 
    } 
    } 


    //---------------------------------------------------------------- 
    // The main class 
    //---------------------------------------------------------------- 
    public class JavaHelp extends JFrame 
    { 
    // The object constructor 
    public JavaHelp() 
    { 
     // Start construction 
     super("Help System"); 
     this.setSize(640, 480); 
     Container contents = getContentPane(); 
     contents.setLayout(new FlowLayout()); 


     JButton b1 = butt( "button1", 64, 48); 
     JButton b2 = butt( "button2", 96, 48); 
     JButton b3 = butt( "button3", 128, 48); 
     JPanel p1 = pane("hello", 100, 100); 
     JPanel p2 = pane("world", 200, 100); 

     contents.add(b1); 
     contents.add(p1); 
     contents.add(b2); 
     contents.add(p2); 
     contents.add(b3); 

     JRootPane jrp = this.getRootPane(); 
     jrp.getInputMap(jrp.WHEN_IN_FOCUSED_WINDOW) 
     .put(KeyStroke.getKeyStroke("F1"), "helpAction"); 
     jrp.getActionMap().put("helpAction", 
        new Respond2Key("frame",(Component)contents) 
        ); 
     this.setVisible(true); 
     this.requestFocus(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    // Inner classes for instantiating and listening to button, and panel. 
    class ButtonListener implements ActionListener 
    { 
     private String label = null; 

     public void setLabel(String s) {label = s;} 

     public void actionPerformed(ActionEvent e) 
     { 
     System.out.printf("Dealing with event labeled %s source %s\n\n", 
        label, 
        e.getSource().toString()); 
     } 

    } 

    // def butt(from, name, w, h) = new Jbutton (...) 
    protected JButton butt(String s, int w, int h) 
    { 
     JButton b = new JButton(s); 
     b.setSize(w, h); 
     ButtonListener oj = new ButtonListener(); 
     oj.setLabel(s); 
     b.addActionListener(oj); 
     return (b); 
    } 

    // def pane = new Jpanel(...) 
    protected JPanel pane(String name, int w, int h) 
    { 
     JPanel p = new JPanel(); 
     p.setMinimumSize(new Dimension(w, h)); 
     p.add(new Label(name)); 
     p.setBackground(Color.black); 
     p.setForeground(Color.red); 
     return (p); 
    } 

    //-------------------------------- 
    public static void main(String[] args) 
    { 
     JavaHelp jh = new JavaHelp(); 
    } 



    } 





उत्तर

3

इनपुट हमेशा एक ही KeyListener पर जाता है।

एक KeyEvent हमेशा ध्यान देने के साथ घटक के लिए भेजा जाता है, माउस स्थान कैसे महत्वपूर्ण घटना उत्पन्न होता है के साथ कोई संबंध नहीं है।

एक KeyListener का उपयोग करने के बजाय, आपको Key Bindings का उपयोग करना चाहिए। जब आप कुंजी बाइंडिंग का उपयोग करते हैं तो आप JFrame के रूट फलक पर बाध्यकारी जोड़कर एक कीस्ट्रोक उत्पन्न होने पर एक क्रिया का आह्वान कर सकते हैं। अधिक जानकारी के लिए Key Bindings पर स्विंग ट्यूटोरियल से अनुभाग पढ़ें।

अब उस क्रिया में जिसे आप "?" सुनने के लिए बनाते हैं KeyStroke तब आप कर सकते हैं:

  1. वर्तमान माउस स्थान प्राप्त करने के लिए MouseInfo कक्षा का उपयोग करें।
  2. SwingUtilities.convertPointFromScreen(...) का उपयोग माउस बिंदु कन्वर्ट करने के लिए जड़ फलक
  3. तो के सापेक्ष होने के लिए आप Conatiner.getComponentAt(...) उपयोग कर सकते हैं आप आपकी सहायता जानकारी प्रदर्शित कर सकते हैं
  4. पर वास्तविक घटक माउस है पाने के लिए एक बार आप घटक पता ।
+0

परफेक्ट - एक आकर्षण की तरह काम करता है। – ConorR

+0

आप लोग सबसे अच्छे हैं - आप दोनों के लिए धन्यवाद! – ConorR

3

मैं वहाँ एक बेहतर तरीका है यकीन है, लेकिन एक त्वरित और गंदी समाधान:

private final class HoverFocusListener extends MouseInputAdapter { 
    public void mouseEntered(MouseEvent e) { 
    e.getComponent().requestFocusInWindow(); 
    } 
} 

या, यदि आवश्यक हो तो:

public void mouseEntered(MouseEvent e) { 
    e.getSource().setFocusable(true); 
    for (Component c : refToParent.getComponents()) c.setFocusable(false); 
    e.getComponent().requestFocusInWindow(); 
} 

तो बस .addMouseListener(new HoverFocusListener()) सभी प्रभावित घटकों के लिए।

+0

क्या यह परिवर्तन हर बार जब मैं होवर करता हूं? – ConorR

संबंधित मुद्दे