java 按钮不再被按,按下按钮后,Java Key Event将不会执行

这是一个焦点问题。使用键绑定而不是KeyListener,因此您不必担心此问题(以及其他好处 - 请查看Key Bindings tutorial以获取详细信息)。

这是我的SSCCE,它展示了我正在谈论的内容。请注意,KeyListener和键绑定都有效,直到您按下按钮,然后只有绑定工作:

import java.awt.event.*;

import javax.swing.*;

public class KeyBindingsEg {

private static void createAndShowGui() {

PanelContainer mainPanel = new PanelContainer();

JFrame frame = new JFrame("KeyBindingsEg");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(mainPanel);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

}

@SuppressWarnings("serial")

class PanelContainer extends JPanel {

public PanelContainer() {

this.setFocusable(true);

this.addKeyListener(new MyKeyListener());

JButton newGameButton = new JButton("New Game");

newGameButton.addActionListener(new MyActionListener());

add(newGameButton);

setKeyBindings();

}

private void setKeyBindings() {

Action hideAction = new BindingAction(BindingAction.HIDE);

Action showAction = new BindingAction(BindingAction.SHOW);

int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;

InputMap inputMap = getInputMap(condition);

ActionMap actionMap = getActionMap();

actionMap.put(BindingAction.HIDE, hideAction);

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0), BindingAction.HIDE);

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.SHIFT_DOWN_MASK),

BindingAction.HIDE);

int[] showKeys = { KeyEvent.VK_O, KeyEvent.VK_ESCAPE };

actionMap.put(BindingAction.SHOW, showAction);

for (int key : showKeys) {

inputMap.put(KeyStroke.getKeyStroke(key, 0), BindingAction.SHOW);

inputMap.put(KeyStroke.getKeyStroke(key, KeyEvent.SHIFT_DOWN_MASK),

BindingAction.SHOW);

}

}

private class MyActionListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent aevt) {

System.out.println("button pressed");

}

}

private class MyKeyListener extends KeyAdapter {

public void keyReleased(KeyEvent kevt) {

if (kevt.getKeyCode() == KeyEvent.VK_ESCAPE

|| kevt.getKeyChar() == 'O' || kevt.getKeyChar() == 'o') {

System.out.println("KeyListener: show");

} else if (kevt.getKeyChar() == 'h' || kevt.getKeyChar() == 'H') {

System.out.println("KeyListener: hide");

}

}

}

private class BindingAction extends AbstractAction {

public static final String HIDE = "Hide";

public static final String SHOW = "Show";

public BindingAction(String text) {

super(text);

putValue(ACTION_COMMAND_KEY, text);

}

@Override

public void actionPerformed(ActionEvent evt) {

String actionCommand = evt.getActionCommand();

if (actionCommand.equals(HIDE)) {

System.out.println("key bindings: hide");

} else if (actionCommand.equals(SHOW)) {

System.out.println("key bindings: show");

}

}

}

}

要将ActionButtonsPreference的三个按钮转换为一个按钮,你需要在xml布局文件定义一个按钮,并将其添加到ActionButtonsPreference。下面是完整的代码示例: 首先,你需要在xml布局文件定义一个按钮: ```xml <Button android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Button" /> ``` 然后,在你的PreferenceFragment或PreferenceActivity,你需要获取ActionButtonsPreference并将其转换为一个Button对象,并设置点击事件: ```java public class MyPreferenceFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { // Load the preferences from an XML resource setPreferencesFromResource(R.xml.preferences, rootKey); // Get the action buttons preference ActionButtonsPreference actionButtons = findPreference("action_buttons"); // Get the custom button view and set its click listener Button customButton = (Button) actionButtons.findViewById(R.id.custom_button); customButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Handle button click event } }); // Hide the other action buttons actionButtons.findViewById(android.R.id.button1).setVisibility(View.GONE); actionButtons.findViewById(android.R.id.button2).setVisibility(View.GONE); } } ``` 上面的代码,我们首先获取了ActionButtonsPreference对象,然后获取了我们在xml布局定义的按钮,最后设置了按钮的点击事件。我们还隐藏了其他两个按钮,以便只显示我们的自定义按钮。 最后,你需要在xml布局将ActionButtonsPreference转换为一个PreferenceCategory,并将其包含在你的Preferences.xml文件: ```xml <PreferenceCategory android:title="Action Buttons"> <PreferenceScreen> <Preference android:key="action_buttons" android:widgetLayout="@layout/custom_button_preference" /> </PreferenceScreen> </PreferenceCategory> ``` 这样,你就可以将ActionButtonsPreference的三个按钮转换为一个自定义按钮了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值