从JButton接收输入的问题

时间:2014-02-07 19:38:45

标签: java swing jbutton actionlistener

这是我的Black Jack程序,它仍然是WIP,但该程序将运行。问题是单击JButton命中不会.setText到新值,存储在JLabel总计中。有人请帮忙!

import java.awt.Color;
import javax.swing.*;
import java.text.*;
import java.awt.Font.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class A3_Poltoranos_Max implements ActionListener{

  int playerCard1 = (int)(Math.random() * 11) * 1 + 1;
  int playerCard2 = (int)(Math.random() * 11) * 1 + 1;
  int totalScore = playerCard1 + playerCard1;``

  JPanel background;
  JLabel total, totalTitle;
  JButton hit, stand;

  public JPanel createContentPane(){
    JPanel totalGUI = new JPanel();    
    totalGUI.setLayout(null);

    JPanel background = new JPanel();
    Color feltGreen = new Color(27, 154, 25);
    background.setLayout(null);
    background.setBackground(feltGreen);
    background.setLocation(0, 0);
    background.setSize(800,600);
    totalGUI.add(background);

    JLabel totalTitle = new JLabel("Your total:");

    totalTitle.setLocation(150, 50);
    totalTitle.setSize(100, 40);
    totalTitle.setHorizontalAlignment(0);
    background.add(totalTitle);

    JLabel total = new JLabel(""+totalScore);

    total.setLocation(150, 200);
    total.setSize(100, 40);
    total.setHorizontalAlignment(0);
    background.add(total);

    JButton hit = new JButton("Hit!");
    hit.setLocation(50, 450);
    hit.setSize(100, 30);
    hit.addActionListener(this);
    background.add(hit);

    JButton stand = new JButton("Stand!");
    stand.setLocation(250, 450);
    stand.setSize(100, 30);
    stand.addActionListener(this);
    background.add(stand);

    totalGUI.setOpaque(true); 
    return totalGUI;  
  }
  @Override
  public void actionPerformed(ActionEvent e) {
        if(e.getSource() == hit)
        {
            playerCard2 = (int)(Math.random() * 11) * 1 + 1;
            totalScore = totalScore + playerCard2;
            total.setText(""+totalScore);
        }
        else if(e.getSource() == stand)
        {            
            total.setText(""+totalScore);
        }
    }

  private static void createWindow() {
    JFrame frame = new JFrame("Black Jack V 0.1"); 
    A3_Poltoranos_Max demo = new A3_Poltoranos_Max();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    frame.setVisible(true); 
  }
  public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
    createWindow();

      }
    });
  }
}

2 个答案:

答案 0 :(得分:2)

您将按钮存储在本地var“hit”而不是对象var“hit”中 - 这就是测试的原因

if(e.getSource() == hit)

总是会失败(命中为空)。

不要声明新变量,代码应该有效:

hit = new JButton("Hit!");

而不是

JButton hit = new JButton("Hit!");

(当然也适用于其他按钮,标签和面板。)

答案 1 :(得分:1)

Stefan的回答是正确的。

您可以做的另一件事是将动作侦听器放在按钮本身上:

hit.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    playerCard2 = (int)(Math.random() * 11) * 1 + 1;
    totalScore = totalScore + playerCard2;
    total.setText(""+totalScore);
  }
});

如果您也为Stand按钮执行此操作,则可以从类中删除ActionListener接口。

这做了一些事情:

  1. 它更清楚地说明什么按钮正在进行什么活动
  2. 它消除了对类范围JButton定义的需求 - 现在,可以保留JButton hit = new JButton("Hit!");并删除类级变量
  3. 它更加面向对象,而不是在单个actionPerformed()方法中拥有一组if语句