在 swing 中,您应该在主面板的操作映射中添加一个 KeyStroke:例如,以下代码让您在每次按下 [F10] 键时刷新包含 JPanel 的 JFrame:
public class MainWindow extends JFrame{
JPanel central;
public MainWindow(){
central = new JPanel();
// I assume you define your other 5 panels here
// and add them to the central JPanel.
getContentPane().add(central, BorderLayout.CENTER);
registerRefreshAction();
}
private void registerRefreshAction(){
javax.swing.Action refresh = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
JFrame frame = (JFrame) getTopLevelAncestor();
frame.setVisible(false);
frame.getContentPane().repaint();
frame.setVisible(true);
}
};
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
central.getActionMap().put("Refresh", refresh);
central.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "Refresh");
}
}
如前所示,您应该在构造函数的某个位置调用 registerRefreshAction。您提到的其他组件包含在 5 个面板中,不需要显示。它在 Linux 中运行。