更改JFrame的面板“禁用”插入的新面板的inputMap

时间:2012-04-16 14:51:19

标签: java swing event-handling jframe jpanel

问题描述:我有一个JFrame,在这个JFrame里面有一个带按钮的JPanel,当我按下按钮时,一个动作监听器用一个新的JPanel改变当前的JPanel,其中包含另外两个JPanel,这两个都有一个inputMap,当用户按下“向上”键时,在它们两个上做一些事情。问题是:当我用新的JPanel更改JPanel时,“up”键将不会执行任何操作。

以下是代码:是SSCCE,因此您只需复制并粘贴以查看其功能。

这个修改过的代码来自我有时在“解决”的另一个问题。 How to make two JPanels listen to the same event? (代码在我选择的答案中)。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class TwoPanelsTest extends JFrame {

private static MyPanel one = new MyPanel("One");
private static MyPanel two = new MyPanel("Two");
private static List<MyPanel> list = Arrays.asList(one, two);
private PanelsController panelsController;

public TwoPanelsTest() {
    super("TwoPanelsTest");
    panelsController= new PanelsController(this);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(400,400);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

private static class MyPanel extends JPanel {

    private String string = " will be updated though its action.";
    private Action action = new UpdateAction(this);
    private String name;
    private JLabel label;

    public MyPanel(String name) {
        this.name = name;
        this.label = new JLabel(name + string, JLabel.CENTER);
        this.setLayout(new GridLayout());
        this.setFocusable(true);
        this.add(label);
    }

    public Action getAction() {
        return action;
    }

    private void update() {
        label.setText(name + ": " + System.nanoTime());
    }

    private static class UpdateAction extends AbstractAction {

        private MyPanel panel;

        public UpdateAction(MyPanel panel) {
            this.panel = panel;
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            panel.update();
        }
    }
}//MyPanel

private static class ButtonPanel extends JPanel{
    private JButton button ;
    private PanelsController panelsController;

    public ButtonPanel(PanelsController panelsController){
        this.panelsController=panelsController;

        button = new JButton("Button");
        button.setActionCommand("buttonPressed");
        button.addActionListener(this.panelsController);
        this.setFocusable(true);
        add(button);
    }
}//ButtonPanel

private static class PanelsController implements ActionListener {

    private TwoPanelsTest twoPanelsTest;

    public PanelsController(TwoPanelsTest twoPanelsTest){
        this.twoPanelsTest=twoPanelsTest;
        this.twoPanelsTest.getContentPane().add(new ButtonPanel(this));
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("buttonPressed")){
            twoPanelsTest.getContentPane().removeAll();
            twoPanelsTest.getContentPane().invalidate();

            JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
            panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            panel.add(one);
            panel.add(two);
            panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
            panel.getActionMap().put("up", new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    for (MyPanel panel : list) {
                        panel.getAction().actionPerformed(e);
                    }
                }
            });
            twoPanelsTest.getContentPane().add(panel);
            twoPanelsTest.validate();
            twoPanelsTest.repaint();
        }
    }//ActionPerformed

}//PanelsController

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            TwoPanelsTest t = new TwoPanelsTest();
        }
    });
}
}

1 个答案:

答案 0 :(得分:4)

嗯,非常简单 - 如果你有两个没有任何组件的面板,并希望他们听热键使用:

panel.getInputMap ( JComponent.WHEN_IN_FOCUSED_WINDOW )
     .put ( KeyStroke.getKeyStroke ( KeyEvent.VK_UP, 0 ), "up" );

JComponent.WHEN_IN_FOCUSED_WINDOW而不是JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT。

否则你需要在面板内集中注意事项,以便能够捕捉关键事件。

顺便说一下,还有另一种方法可以在Java应用程序窗口中监听全局热键:

Toolkit.getDefaultToolkit ().addAWTEventListener ( new AWTEventListener ()
{
    public void eventDispatched ( AWTEvent event )
    {
        // All application key events will be passed here
    }
}, AWTEvent.KEY_EVENT_MASK );