So i'm trying to write this app that's basically a game of who can press the keys faster( 1 for player 1 and 0 for player 2). The app has a simple GUI with a simple scoreboard that should be updated when the players hit one of the keys. Here's a screenshot of the GUI : http://screencloud.net/v/a6vT Nevermind the start button, it's simply to reset the scores, i'll implement it later.
Here are the code files :
1.GameFrame.java
package MediatorGame;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class GameFrame extends JFrame {
private GamePanel currentPanel;
public GameFrame() {
currentPanel = new GamePanel();
setupFrame();
}
private void setupFrame(){
this.setContentPane(currentPanel);
currentPanel.setFocusable(true);
currentPanel.requestFocusInWindow();
this.setSize(450, 300);
}
}
2.GamePanel.java
package MediatorGame;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
@SuppressWarnings("serial")
public class GamePanel extends JPanel{
private JButton startButton;
private SpringLayout currentLayout;
private JLabel p1Label;
private JLabel p2Label;
private int p1Score = 0;
private int p2Score = 0;
private JLabel p1ScoreLabel;
private JLabel p2ScoreLabel;
private InputMap iMap;
private ActionMap aMap;
public GamePanel() {
startButton = new JButton("Start");
currentLayout = new SpringLayout();
p1Label = new JLabel("Player 1");
p2Label = new JLabel("Player 2");
p1ScoreLabel = new JLabel(String.valueOf(p1Score));
p2ScoreLabel = new JLabel(String.valueOf(p2Score));
iMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
aMap = getActionMap();
setupPanel();
}
private void setupPanel(){
setBackground(new Color(255, 255, 204));
setLayout(currentLayout);
currentLayout.putConstraint(SpringLayout.WEST, startButton, 206, SpringLayout.WEST, this);
currentLayout.putConstraint(SpringLayout.SOUTH, startButton, -40, SpringLayout.SOUTH, this);
add(startButton);
currentLayout.putConstraint(SpringLayout.NORTH, p2Label, 50, SpringLayout.NORTH, this);
currentLayout.putConstraint(SpringLayout.EAST, p2Label, -62, SpringLayout.EAST, this);
add(p2Label);
currentLayout.putConstraint(SpringLayout.WEST, p1Label, 75, SpringLayout.WEST, this);
currentLayout.putConstraint(SpringLayout.NORTH, p1Label, 0, SpringLayout.NORTH, p2Label);
add(p1Label);
currentLayout.putConstraint(SpringLayout.WEST, p1ScoreLabel, 0, SpringLayout.WEST, p1Label);
currentLayout.putConstraint(SpringLayout.NORTH, p1ScoreLabel, 0, SpringLayout.NORTH, p2ScoreLabel);
add(p1ScoreLabel);
currentLayout.putConstraint(SpringLayout.NORTH, p2ScoreLabel, 6, SpringLayout.SOUTH, p2Label);
currentLayout.putConstraint(SpringLayout.WEST, p2ScoreLabel, 0, SpringLayout.WEST, p2Label);
add(p2ScoreLabel);
iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "incP1Score");
iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, 0), "incP2Score");
aMap.put("incP1Score", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
p1Score += 1;
}
});
aMap.put("incP2Score", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
p2Score += 1;
}
});
}
}
3.GUIRunner.java
package MediatorGame;
import javax.swing.UIManager;
public class GUIRunner {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e){ }
java.awt.EventQueue.invokeLater(new Runnable(){
public void run() {
GameFrame myApp = new GameFrame();
myApp.setVisible(true);
}
});
}
}
I've checked a lot of questions. Most of them laid the blame on having the JPanel
focusable, and asking for focus, wrong keyEvent
codes (like '1' instead of "1" or VK_1) or having the default getInputMap()
method instead of getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
. I seem to have dealt with all of those and yet the label still don't update when I'm pressing the keys .
My question : what's wrong ? am I supposed to have the labels focused instead of the whole panel ? wrong keycode ? I've tried a lot of supposed solutions and just can't get a break!
As far as I can see, the reason the labels do not update is just because you don't update them:
@Override
public void actionPerformed(ActionEvent e) {
p1Score += 1;
p1ScoreLabel.setText(String.valueOf(p1Score));
}
And:
@Override
public void actionPerformed(ActionEvent e) {
p2Score += 1;
p1ScoreLabel.setText(String.valueOf(p2Score));
}