-1

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));
            } 
        }); 
    } 
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
student23
  • 68
  • 1
  • 2
  • 12
  • 1
    please include some code and comments of what you already tried and add a [MCVE](http://stackoverflow.com/help/mcve) so others can try out your code. – oli_obk Jun 02 '15 at 13:57
  • Sorry I didnt know I'm trying now but the score won't increase :/ – student23 Jun 02 '15 at 14:21
  • 1st of all I edited your code so it's readable, you should edit it next time with the `{}` button and make it readable as I said. Next **don't** use `null layout` **NEVER** (i.e. `setBounds()`) instead choose the [Layout Manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) which fits your needs. You're making your frame visible before everything is painted and you never paint button. I'm writing my answer so please wait. – Frakcool Jun 02 '15 at 15:43

1 Answers1

0

Alright this code works for increasing +10 each time user clicks +10 button.

To make it work with -10 or whatever number you want to decrease if they click on the frame, you should check JFrame mouse click using JComponent and MouseListener.

As I said, next time make your code readable don't use this marks (``) to show the code, instead use 4 spaces or clic the { } button above.

Don't use null layout instead use a Layout Manager. For more information about this, read this question and the answer of @BillK.

I hope this helps, and welcome to StackOverflow.

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
public class Letstry { 
    static int score;
    static JFrame frame;
    static JLabel label;
    static JButton button;
    public static void main (String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    } 
    public static void createAndShowGUI() {
        frame = new JFrame("Scity4"); 
        frame.getContentPane().setLayout(new FlowLayout());
        label = new JLabel("Score: ");
        button = new JButton("+10");
        button.addActionListener (new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                score = score + 10;
                label.setText("Score: " + score);
                frame.pack();
            } 
        });
        frame.add(label);
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89