Ho problemi con il mio KeyBoardFocusManger
per funzionare con lo schermo intero Window
. Non importa cosa, non si ottiene input da tastiera. Ho utilizzato uno System.exit(0)
e un println()
per cercare qualsiasi chiamata al metodo premuto a chiave/rilasciato/digitato, ma non vengono emessi errori. Ho provato KeyListeners
; ma dopo aver letto this, ho cambiato in uno KeyboardFocusManager
, e la stessa cosa succede ancora. Sto davvero diventando disperato; da quello che posso giudicare, lo Window
non ottiene il fuoco della tastiera?La finestra a schermo intero non riceve l'input della tastiera utilizzando KeyListener o KeyBoardFocusManager
Qui è la mia principale:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// Determine if full-screen mode is supported directly
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if (gs.isFullScreenSupported()) {
Frame frame = new Frame(gs.getDefaultConfiguration());
SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(frame);
// Enter full-screen mode
gs.setFullScreenWindow(spaceInvaderUI);
} else {
JOptionPane.showMessageDialog(null, "Does not support full screen!", "Error 0x01", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
});
}
e qui è l'interfaccia utente che contiene il KeyBoardFocusManger
, e viene aggiunto in addListeners()
metodo:
class SpaceInvaderUI extends Window {
private JPanel drawingPanel;
private Image background;
private JButton btnExit;
public SpaceInvaderUI(Frame frame) {
super(frame);
try {
background = ImageIO.read(getClass().getResourceAsStream("background.png"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
System.exit(2);
}
createWindow();
}
private void createComponents() throws HeadlessException {
drawingPanel = new DrawingPanel(background, this);
btnExit = new JButton("Exit");
}
private void createWindow() {
createComponents();
addListeners();
addComponentsToWindow();
}
private void addComponentsToWindow() {
add(drawingPanel, BorderLayout.CENTER);
add(btnExit, BorderLayout.SOUTH);
}
private void addListeners() {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
private class MyDispatcher implements KeyEventDispatcher {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
System.out.println("pressed");
System.exit(0);
} else if (e.getID() == KeyEvent.KEY_RELEASED) {
System.out.println("released");
System.exit(0);
} else if (e.getID() == KeyEvent.KEY_TYPED) {
System.out.println("Typed");
System.exit(0);
}
return false;
}
}
}
Il pulsante di uscita è solo perché mi sono stancato di uccidendo la mia app tramite taskmanager. Infine ecco il mio pannello sul quale il gioco avrà luogo e il mio background è dipinto su:
public class DrawingPanel extends JPanel {
private final Image background;
private final SpaceInvaderUI invaderUI;
DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
this.background = background;
this.invaderUI = invaderUI;
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(background.getScaledInstance((int) invaderUI.getWidth(), (int) invaderUI.getHeight(), Image.SCALE_SMOOTH), 0, 0, this);
}
}
Grazie in anticipo.
EDIT: ora ho provato ad utilizzare una combinazione di tasti sulla mia drawingPanel
, ma ancora non succede nulla quando si preme f2:
class SpaceInvaderUI extends Window {
private JPanel drawingPanel;
private Image background;
private JButton btnExit;
public SpaceInvaderUI(Frame frame) {
super(frame);
try {
background = ImageIO.read(getClass().getResourceAsStream("background.png"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
System.exit(2);
}
createWindow();
}
private void createComponents() throws HeadlessException {
drawingPanel = new DrawingPanel(background, this);
btnExit = new JButton("Exit");
}
private void createWindow() {
createComponents();
addListeners();
addComponentsToWindow();
}
private void addComponentsToWindow() {
add(drawingPanel, BorderLayout.CENTER);
add(btnExit, BorderLayout.SOUTH);
}
private void addListeners() {
Action exit = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
drawingPanel.getInputMap().put(KeyStroke.getKeyStroke("F2"),
exit);
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
}
Non è una cornice ma una finestra, e potrei usare un pulsante per chiuderla ma voglio che il gioco usi il tasto "Esc" per uscire e richiamare il menu ecc. –
Quindi suppongo che KeyBinding sia esattamente ciò che può funzionare per tu. –