Local variable money defined in an enclosing scope must be final or effectively final

Jakob12345

Grünschnabel
Hallo, ich kenne mich noch nicht so gut in Java aus und habe eine Frage. Ich will eine Art Glücksspiel machen bei dem man um 30 Money Spielen kann ich will bei jedem Klick auf Start 30 money abziehen aber es kommt der Fehler "Local variable money defined in an enclosing scope must be final or effectively final". Ich weiß der Code ist schlecht da ich mich noch nicht gut auskenne. Wie kann ich es machen das kein Fehler kommt. Danke
Hier der Code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.JLabel;

public class Glücksrad extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Glücksrad frame = new Glücksrad();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Glücksrad() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 812, 644);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
int money = 30;




JButton btnNewButton = new JButton("START");
btnNewButton.setBackground(Color.DARK_GRAY);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==btnNewButton) {
if (money >= 30) {

money = money - 30; // Hier ist der Fehler


String s1, s2, s3, s4;
Random random = new Random();
s1 = "selten";
s2 = "gewöhnlich";
s3 = "episch";
s4 = "legendär";
int a;

a = random.nextInt(1000)+1;
if (a >= 1 && a<=500) {
textField.setText(s1);
String S = "";



} else if (a >500 && a<700 ) {
textField.setText(s2);

} else if (a >700 && a<= 900) {
textField.setText(s3);


} else if (a >900) {
textField.setText(s4);




}

} else {
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setForeground(Color.RED);
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_2.setBounds(292, 118, 298, 25);
contentPane.add(lblNewLabel_2);
lblNewLabel_2.setText("Nicht genügend Geld!");

Timer time1 = new Timer(1500, new ActionListener() {


public void actionPerformed(ActionEvent e) {
lblNewLabel_2.setText("");


}
});
time1.start();


}




}
}
});
btnNewButton.setFont(new Font("Stencil", Font.BOLD, 34));
btnNewButton.setBounds(31, 129, 166, 141);
contentPane.add(btnNewButton);

textField = new JTextField();
textField.setFont(new Font("Arial Black", Font.ITALIC, 26));
textField.setBackground(Color.LIGHT_GRAY);
textField.setEditable(false);
textField.setBounds(292, 159, 298, 90);
contentPane.add(textField);
textField.setColumns(10);

JLabel lblNewLabel = new JLabel("Money :");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 17));
lblNewLabel.setBounds(584, 24, 87, 37);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("");
String printmoney = Integer.toString(money);



lblNewLabel_1.setText(printmoney);
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_1.setBounds(660, 24, 104, 37);
contentPane.add(lblNewLabel_1);





}
}
 
Moin,
nutze doch bitte die CODE-Tags (über den Button mit den 3 Punkten im Editor)!!
So bekommt man ja Augenkrebs o_O
VG Klaus
 
Java:
    /**
    * Create the frame.
    */
    public Glücksrad() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 812, 644);
        contentPane = new JPanel();
        contentPane.setBackground(Color.LIGHT_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        int money = 30;

        JButton btnNewButton = new JButton("START");
        btnNewButton.setBackground(Color.DARK_GRAY);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(arg0.getSource()==btnNewButton) {
                    if (money >= 30) {
                        money = money - 30; // Hier ist der Fehler

money ist eine Lokale Variable innerhalb des Konstruktors und bleibt nach dem Verlassen des Konstruktor nicht bestehen. Im Gegensatz dazu soll im Nachhinein auf money in der actionPerformed Methode zugegriffen werden. Was du tun musst ist also money zu einer Instanzvariablen zu machen.
 
Zurück