简体   繁体   English

Java动作无法执行

[英]Java actionPerformed not working

I'm trying to make a small Blackjack game with a GUI for school. 我正在尝试使用学校的GUI制作一款小型的21点游戏。 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. 如果我按bBet按钮,它应该将文本更改为“ test2”,但这不起作用。 The first change to "test1" is working. 对“ test1”的第一个更改正在起作用。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM