java 如何在 netbeans 中设置 KeyPress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17566249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:20:55  来源:igfitidea点击:

How to setup KeyPress in netbeans

javaswingkeylistenerjinternalframe

提问by Tyran Xavier Winchester

Can anyone help me to set a KeyPressaction on a currently opened jInternalFrame?

谁能帮我KeyPress在当前打开的文件上设置一个操作jInternalFrame

I have a jDesktopPaneinside a jframe, and I have multiple jInternalFrameinside the DesktopPane. I am using Netbeans to create this application.

我有一个jDesktopPane里面 a jframe,我有多个jInternalFrame里面DesktopPane. 我正在使用 Netbeans 创建此应用程序。

On the jDesktopPaneI have 3 buttons to open 3 jInternalFrame, and I created a Keypresson those buttons and it works fine using this code:

jDesktopPane我有 3 个按钮可以打开 3 jInternalFrame,我Keypress在这些按钮上创建了一个,使用以下代码可以正常工作:

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:
     if(evt.getKeyCode()==KeyEvent.VK_F3){
        frmLogistics.setVisible(true);
        frmLogistics.toFront();
    }
}                               

A jInternalFrameis open and inside there's a jtoolbarwith sets of buttons, one of it is a close button to close that opened jInternalFrame. I set up the code for its ActionPerformso when a user clicks that button the frame or window will be closed.

AjInternalFrame是打开的,里面有一jtoolbar组按钮,其中一个是关闭按钮,用于关闭打开的jInternalFrame。我为其设置了代码,ActionPerform因此当用户单击该按钮时,框架或窗口将关闭。

The problem now is how about a keyboard press? I want to trigger that close button inside the toolbar in a internalframe in order to close it

现在的问题是键盘按下怎么样?我想在内部框架的工具栏中触发关闭按钮以关闭它

I tried this code:

我试过这个代码:

  private void btnCloseLogisticsKeyPressed(java.awt.event.KeyEvent evt) {                                             
    // TODO add your handling code here:
    if(evt.getKeyCode()==KeyEvent.VK_F4){
         int type = JOptionPane.YES_NO_OPTION;
        int choice = JOptionPane.showConfirmDialog(this,"Do You Want to Log Out?","Exit Logistics System", type);
        if(choice == JOptionPane.YES_OPTION){
            frmLogistics.setVisible(false);
            frmLogIn.show();
            btnCashier.setEnabled(false);
            btnTrucking.setEnabled(false);
            btnAccounting.setEnabled(false);
        }
    }
}                                 

But nothing happens. I tried to put that code inside jtoolbar, jInternalFrameand still nothing happens. Maybe anyone of you could help me?

但什么也没有发生。我试图将该代码放入 中jtoolbarjInternalFrame但仍然没有任何反应。也许你们中的任何人都可以帮助我?

采纳答案by Andrew Thompson

For Swing, typically use key bindings over the AWT based, lower level, KeyListener. See How to Use Key Bindingsfor details on how to use them.

对于 Swing,通常在基于 AWT 的较低级别上使用键绑定KeyListener。有关如何使用它们的详细信息,请参阅如何使用键绑定

回答by user2872939

In the event keypress click right in the Jframe de netbeans mas naki..

在事件中,在 Jframe de netbeans mas naki 中单击右键。

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:

     if(evt.getKeyCode()==KeyEvent.VK_F4){
   dispose();

}