Focus fix

This commit is contained in:
iryupnik 2020-04-08 16:03:56 +02:00
parent 28da4d4995
commit 093a2d7ab7
2 changed files with 20 additions and 26 deletions

View File

@ -78,6 +78,6 @@ public class Game extends JFrame {
} }
public Game game() {return this; } public Game game() {return this;}
} }

View File

@ -26,43 +26,32 @@ public class WindowOfMenu extends JLayeredPane {
background.setBackground(Color.BLACK); background.setBackground(Color.BLACK);
startNewGame = new JButton("START NEW GAME"); startNewGame = new JButton("START NEW GAME");
startNewGame.setFocusable(false); //startNewGame.setFocusable(false);
startNewGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 -70,200,30); startNewGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 -70,200,30);
startNewGame.setActionCommand("startNewGame"); startNewGame.setActionCommand("startNewGame");
startNewGame.addActionListener(new ActionListener() { startNewGame.addActionListener(actionEvent -> {
@Override
public void actionPerformed(ActionEvent actionEvent) {
cleanUp(); cleanUp();
game.add(freeMove); game.add(freeMove);
freeMove.setFocusable(true); game.requestFocus();
freeMove.start(true); freeMove.start(true);
}
}); });
loadSavedGame = new JButton("LOAD SAVED GAME"); loadSavedGame = new JButton("LOAD SAVED GAME");
loadSavedGame.setFocusable(false); //loadSavedGame.setFocusable(false);
loadSavedGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2,200,30); loadSavedGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2,200,30);
loadSavedGame.setActionCommand("loadSavedGame"); loadSavedGame.setActionCommand("loadSavedGame");
loadSavedGame.addActionListener(new ActionListener() { loadSavedGame.addActionListener(actionEvent -> {
@Override
public void actionPerformed(ActionEvent actionEvent) {
cleanUp(); cleanUp();
game.add(freeMove); game.add(freeMove);
freeMove.setFocusable(true); game.requestFocus();
freeMove.start(false); freeMove.start(false);
}
}); });
exitGame = new JButton("EXIT GAME"); exitGame = new JButton("EXIT GAME");
exitGame.setFocusable(false); //exitGame.setFocusable(false);
exitGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 + 70,200,30); exitGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 + 70,200,30);
exitGame.setActionCommand("exitGame"); exitGame.setActionCommand("exitGame");
exitGame.addActionListener(new ActionListener() { exitGame.addActionListener(actionEvent -> game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING)));
@Override
public void actionPerformed(ActionEvent actionEvent) {
game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING));
}
});
add(startNewGame); add(startNewGame);
add(loadSavedGame); add(loadSavedGame);
@ -72,7 +61,12 @@ public class WindowOfMenu extends JLayeredPane {
private void cleanUp() { private void cleanUp() {
setVisible(false); setVisible(false);
startNewGame.removeActionListener(startNewGame.getActionListeners()[0]);
loadSavedGame.removeActionListener(loadSavedGame.getActionListeners()[0]);
exitGame.removeActionListener(exitGame.getActionListeners()[0]);
game.remove(this); game.remove(this);
menu().revalidate();
menu().repaint();
} }
public WindowOfMenu menu() { return this; } public WindowOfMenu menu() { return this; }