How can I get a score to increase if the user clicked a Jbutton (All of them ), and decreased if he clicked a random place in the frame ? here's the code
package projet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Letstry {
static int score;
public static void main (String[] args){
JFrame frame = new JFrame("Scity4");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Score inc");
panel.add(button);
button.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
score = score +10;
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
}
});
}
}