- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在每个窗口/面板中,我都将 Enter 键映射为 defaultButton(这意味着即使按下的按钮不在焦点上,按下 Enter 也会触发按钮按下)。我需要做的是将 Escape 键映射到另一个按钮,无论焦点如何,它也会触发第二个按钮。输入键的代码是:
// We get the button
IButton iButton = (IButton)actionToolbar.getComponent(actionToolbar.getComponentCount() - 1);
// We get the window
Window window = SwingUtilities.getWindowAncestor(this);
// Only these two types of windows should have this behaviour
if (window instanceof JFrame) {
((JFrame)window).getRootPane().setDefaultButton(iButton);
}
else if (window instanceof JDialog) {
((JDialog)window).getRootPane().setDefaultButton(iButton);
}
现在我需要的是基本相同的代码,但将 Enter 更改为 Escape,或将监听器添加到...,我不确定是什么。
编辑:我必须在 Java 1.4 中执行此操作,我知道如果我立即说出来会很棒。
最佳答案
JXRootPane(SwingX 项目的)默认有它,你可以做类似的事情
private void installKeyboardActions() {
Action escAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent evt) {
JButton cancelButton = getCancelButton();
if (cancelButton != null) {
cancelButton.doClick(20);
}
}
/**
* Overridden to hack around #566-swing:
* JXRootPane eats escape keystrokes from datepicker popup.
* Disable action if there is no cancel button.<p>
*
* That's basically what RootPaneUI does - only not in
* the parameterless isEnabled, but in the one that passes
* in the sender (available in UIAction only). We can't test
* nor compare against core behaviour, UIAction has
* sun package scope. <p>
*
* Cont'd (Issue #1358-swingx: popup menus not closed)
* The extended hack is inspired by Rob Camick's
* <a href="http://tips4java.wordpress.com/2010/10/17/escape-key-and-dialog/"> Blog </a>
* and consists in checking if the the rootpane has a popup's actionMap "inserted".
* NOTE: this does not work if the popup or any of its children is focusOwner.
*/
@Override
public boolean isEnabled() {
Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (component instanceof JComponent) {
Action cancelPopup = ((JComponent)component).getActionMap().get("cancel");
if (cancelPopup != null) return false;
}
return (cancelButton != null) && (cancelButton.isEnabled());
}
};
getActionMap().put("esc-action", escAction);
InputMap im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
im.put(key, "esc-action");
}
/**
* Sets the <code>cancelButton</code> property,
* which determines the current default cancel button for this <code>JRootPane</code>.
* The cancel button is the button which will be activated
* when a UI-defined activation event (typically the <b>ESC</b> key)
* occurs in the root pane regardless of whether or not the button
* has keyboard focus (unless there is another component within
* the root pane which consumes the activation event,
* such as a <code>JTextPane</code>).
* For default activation to work, the button must be an enabled
* descendant of the root pane when activation occurs.
* To remove a cancel button from this root pane, set this
* property to <code>null</code>.
*
* @param cancelButton the <code>JButton</code> which is to be the cancel button
* @see #getCancelButton()
*
* @beaninfo
* description: The button activated by default for cancel actions in this root pane
*/
public void setCancelButton(JButton cancelButton) {
JButton old = this.cancelButton;
if (old != cancelButton) {
this.cancelButton = cancelButton;
if (old != null) {
old.repaint();
}
if (cancelButton != null) {
cancelButton.repaint();
}
}
firePropertyChange("cancelButton", old, cancelButton);
}
/**
* Returns the value of the <code>cancelButton</code> property.
* @return the <code>JButton</code> which is currently the default cancel button
* @see #setCancelButton
*/
public JButton getCancelButton() {
return cancelButton;
}
关于java - 如何将 “Default Button” 功能添加到 Swing 中的两个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7430687/
我遇到过这个 html: 上面的html和这个有什么区别: 最佳答案 来自MDN page on the tag : 对于 type 的属性标签,可能的值是: 提交:按钮将表单数据提交给服务器
Button button= (Button) findViewbyID(R.id.button); 和 Button button = new Button(this); 有什么区别? 最佳答案 有
我是一名优秀的程序员,十分优秀!