本文介紹了如何設(shè)置 Java 默認(rèn)按鈕以對 ENTER 鍵 _released_ 做出反應(yīng)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
在我的應(yīng)用程序中,我使用默認(rèn)按鈕.我希望它在 ENTER
鍵釋放時做出反應(yīng).不是當(dāng) ENTER
鍵被按下.
In my application I use a Default button. I want it to react when ENTER
Key is released. Not when ENTER
Key is pressed.
我從按鈕的 InputMap
中刪除了 KeyStroke
.但這對我不起作用.我該怎么做?
I removed the KeyStroke
from InputMap
of the button. But it didn't work for me. How should i do that?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class ButtonTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
buildFrame();
}
});
}
private static void buildFrame() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton button = new JButton(new AbstractAction("Button") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ButtonTest::actionPerformed: CALLED");
}
});
JButton button2 = new JButton("Button 2");
InputMap im = button.getInputMap();
im.put(KeyStroke.getKeyStroke("ENTER"), "none");
im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
f.setLayout(new GridBagLayout());
f.add(button);
f.add(button2);
f.getRootPane().setDefaultButton(button);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
這是示例代碼.
推薦答案
JRootPane rootPane = SwingUtilities.getRootPane(/* Your JButton */);
rootPane.setDefaultButton(/* Your JButton */);
這篇關(guān)于如何設(shè)置 Java 默認(rèn)按鈕以對 ENTER 鍵 _released_ 做出反應(yīng)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!