'How to add a score and display it on the screen
I'm basically making a bass guitar playing application where you click buttons to play the notes. I want to add a score counter for when the right sequence of buttons are clicked (to play a song), so I made a JButton array of the notes to play in order. My problem is making the score and displaying it on the screen.
This is my array:
private static JButton buttons[] = {string1, string2, string3, string4, string5, string6, string7, string8};
This is my score counter method:
public void actionPerformed(ActionEvent e) {
jb = (JButton)e.getSource();
for (i = 0; i < buttons.length; i++)
{
if (jb == (buttons[i])) {
totalOccurrences += 1;
score.setText("score: " + totalOccurrences + "/8");
}
else
{
totalOccurrences -= 1;
score.setText("score: " + totalOccurrences + "/8");
How can I fix this?
Solution 1:[1]
Add a text area to the UI, and ensure that the data output goes there and not to the console. Set an actionListener, which will tell the button what to do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
