In your actionPerformed
method, you're not updating the view with the new score. I imagine it will be something like:
score.setText("Score:" + score);
Domanda
I am trying to add score when the button is pressed,the score is displayed in the JTextField, but when the button is pressed the score inst added, it says 0. I have an integer field which stores the score
private int score=0;
yesButton = new JButton("True");
panel.add(yesButton);
yesButton.addActionListener(new ActionListener() {
int index = 0;
@Override
public void actionPerformed(ActionEvent e) {
index++;
score++;
qScore.setText("",+score);
qText.setText(questions.get(index).getQuestions());
}
});
score
JPanel scorePanel = new JPanel();
scorePanel.setLayout(new GridLayout(1,0));
JLabel label = new JLabel("Score:");
JTextField qScore = new JTextField();
qScore.setEditable(false);
Any idea what am I doing wrong?
Soluzione
In your actionPerformed
method, you're not updating the view with the new score. I imagine it will be something like:
score.setText("Score:" + score);
Altri suggerimenti
Your variable qScore should be an instance member and constructed once.
Then when the actionPerformed method is fired, call setText
// in actionPerformed
score++;
qScore.setText(score);