user5647258
user5647258

Reputation:

Java actionPerformed not working

I'm trying to make a small Blackjack game with a GUI for school. Here's the code:

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class Gui extends JFrame implements ActionListener {

private JButton bBet;
private JLabel lblPointsPL;
private Blackjack bj;

public Gui() {

    Blackjack bj = new Blackjack();

    bBet = new JButton("Bet!");
    bBet.setBounds(10, 60, 200, 35);
    bBet.setForeground(Color.black);
    bBet.setBackground(Color.yellow);
    this.add(bBet);

    lblPointsPL = new JLabel("PointsPL");
    lblPointsPL.setBounds(300, 50, 200, 35);
    this.add(lblPointsPL);

    lblPointsPL.setText("test1");

}

public void actionPerformed(ActionEvent event) {

    Blackjack bj = new Blackjack();

    if (event.getSource() == bBet) {
        lblPointsPL.setText("test2");
    }

}

}

If I press the bBet Button, it should change the text to "test2" but that doesn't work. The first change to "test1" is working.

Upvotes: 0

Views: 642

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

You've forget to add action listener to your button.

bBet = new JButton("Bet!");
bBet.setBounds(10, 60, 200, 35);
bBet.setForeground(Color.black);
bBet.setBackground(Color.yellow);
this.add(bBet);
bBet.addActionListener(this); // missing statement.

Upvotes: 3

Related Questions