在java awt或swing中,我如何安排键盘输入去鼠标的哪里?

Con*_*orR 6 java swing awt awt-eventqueue

在帮助系统上工作,我希望每个组件在鼠标悬停时提供一些帮助并且"?" 键是按下的.有点像工具提示,除了更广泛的帮助 - 基本上一个小的Web浏览器旨在弹出和显示文本,图像或更多.

我发现无论鼠标在哪里,输入总是转到同一个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();
    }



    }





cam*_*ckr 3

输入始终发送至同一个 KeyListener。

KeyEvent 始终被分派给具有焦点的组件,鼠标位置与按键事件的生成方式无关。

您应该使用Key Bindings. 当您使用按键绑定时,您可以通过将绑定添加到 JFrame 的根窗格来在每次生成 KeyStroke 时调用操作。请阅读 Swing 教程中有关键绑定的部分以获取更多信息。

现在在您创建的用于侦听“?”的操作中 KeyStroke 然后您可以:

  1. 使用该类MouseInfo获取当前鼠标位置。
  2. 使用SwingUtilities.convertPointFromScreen(...)将鼠标点转换为相对于根窗格的位置
  3. 然后你可以使用 来Conatiner.getComponentAt(...)获取鼠标悬停的实际组件
  4. 一旦您了解了该组件,您就可以显示帮助信息。