gpt4 book ai didi

java - KeyListener 在 dispose() 后不起作用

转载 作者:行者123 更新时间:2023-11-29 08:57:08 24 4
gpt4 key购买 nike

在我的程序中,我有在普通窗口和全屏状态之间切换的方法:

public void setFullScreen(boolean enableFullScreen) {
Frame.fullScreen = enableFullScreen;
dispose();
if ( enableFullScreen ) {
getContentPane().setPreferredSize(resFull);
setSize(resFull);
setBounds(Frame.graphicsDev.getConfigurations()[0].getBounds().x, Frame.graphicsDev.getConfigurations()[0].getBounds().y, resFull.width, resFull.height);
//code for resizing components
setUndecorated(true);
setResizable(false);
} else {
getContentPane().setSize(resNormal);
getContentPane().setPreferredSize(resNormal);
//code for resizing components
setUndecorated(false);
setResizable(true);
}
pack();
setVisible(true);
}

resFullresNormal 是维度变量。

当我在打开的正常窗口中单击 F11 时,它会更改全屏状态 (setFullScreen(!Frame.fullScreen)),但更改后通常不会收到任何信号- 我必须切换到其他应用程序,然后返回到此窗口 - 然后就可以了。

最佳答案

您的问题可能是由于 JButton 在按下时窃取了焦点,导致您的 KeyListener 正在监听未聚焦的组件。同样,一种解决方案是使用键绑定(bind)。

我的 sscce作为证明在您尝试实现的转换之后 KeyListeners 和 Key Bindings 本身都可以正常工作:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class TestUndecoratedKeyListener {
protected static final int PREF_W = 400;
protected static final int PREF_H = PREF_W;

private static void createAndShowGui() {
final JFrame frame = new JFrame("TestUndecoratedKeyListener");

final JPanel mainPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
};

JToggleButton toggleButton = new JToggleButton("Full Screen");
toggleButton.setFocusable(false);
toggleButton.setMnemonic(KeyEvent.VK_F);
toggleButton.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
frame.dispose();
frame.setUndecorated(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
} else {
frame.dispose();
frame.setUndecorated(false);
frame.setExtendedState(JFrame.NORMAL);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
});
mainPanel.add(toggleButton);
final JLabel keyListenerLabel = new JLabel();
final JLabel keyBindingLabel = new JLabel();

mainPanel.add(new JLabel("Press Right or Left Arrow Key To Test"));
mainPanel.add(keyListenerLabel);
mainPanel.add(keyBindingLabel);

keyListenerLabel.setBorder(BorderFactory.createLineBorder(Color.black));
keyBindingLabel.setBorder(BorderFactory.createLineBorder(Color.black));

mainPanel.addKeyListener(new KeyAdapter() {

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keyListenerLabel.setText("KeyListener: left pressed");
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyListenerLabel.setText("KeyListener: right pressed");
}
}
});

int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = mainPanel.getInputMap(condition);
ActionMap actionMap = mainPanel.getActionMap();

KeyStroke leftArrowStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
KeyStroke rightArrowStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
inputMap.put(leftArrowStroke, leftArrowStroke.toString());
inputMap.put(rightArrowStroke, rightArrowStroke.toString());
actionMap.put(leftArrowStroke.toString(), new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
keyBindingLabel.setText("Key Bindings: left Arrow Pressed");
}
});
actionMap.put(rightArrowStroke.toString(), new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
keyBindingLabel.setText("Key Bindings: right Arrow Pressed");
}
});


mainPanel.setFocusable(true);
mainPanel.requestFocusInWindow();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - KeyListener 在 dispose() 后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com