使用 Swing 在 Java 中创建热键 [英] create hot keys in java using swing

查看:35
本文介绍了使用 Swing 在 Java 中创建热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来在 java Swing 中创建热键.我为 Jtextfield1 (Name) 创建了助记符.它显示正确,但现在我需要知道是否在运行时我立即单击 tf2 然后光标将从 tf1 转到 tf2.

Hi I wrote the following code to create hot keys in java Swing. I create the Mnemonic for Jtextfield1 (Name). It showed properly, but now I need to know if at run time I immediately click tf2 then the cursor will come to tf2 from tf1.

我在 tf2 中输入了一些值.然后我需要输入tf1.在这种情况下,我按 ALT+N 键(因为 N 是 tf1 的助记符).光标聚焦 tf1 并在文本字段中输入名称.我该怎么做?

I enter some values in tf2. Then I need to enter tf1. In this situation I press ALT+N keys (because N is mnemonic of tf1). The cursor focused tf1 and entered the name in the textfield. how do I do this?

package hotkeys;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class hotkey extends JFrame {

    public static void main(String arg[]) {

        JLabel Name=new JLabel("Name");
        JTextField tf1=new JTextField(20);
        Name.setLabelFor( Name );
        Name.setDisplayedMnemonic( 'N' );

        JLabel Regno=new JLabel("Reg_NO");
        JTextField tf2=new JTextField(20);

        JButton b1=new JButton("Save");
        JButton b2=new JButton("eXit");

        JFrame f=new JFrame();
        JPanel p=new JPanel();

        p.add(Name);
        p.add(Regno);
        p.add(tf1);
        p.add(tf2);
        p.add(b1);
        p.add(b2);

        f.add(p);
        f.setVisible(true);
        f.pack();
    }    
}

推荐答案

you have to look for KeyBindingsKeyBindings 的输出应该是 javax.swing.Action 然后你可以将 Focus 包装到决策 JComponent,

you have to look for KeyBindings, output from KeyBindings should be javax.swing.Action and there you can wrap set Focus to the decisions JComponent,

这篇关于使用 Swing 在 Java 中创建热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆