ActionListener只响应一次

时间:2014-05-30 23:52:59

标签: java swing timer actionlistener japplet

我用Java创建了一个基本的轮盘赌轮,我有一个带有ActionListener的JButton旋转轮子。按下按钮一次后,按预期工作。

问题是:一旦我第二次按下JButton,它就不再有效了。我会发布我的整个代码,对于任何想要确切了解我的意思的人。

奖励积分:奖励积分可以帮助我处理以下事项:

  • 按钮不会出现,直到您点击它们,或将窗口放在后台并将其重新启动。
  • 出于某种原因,旋转总是落在红色上。我有一个名为randomSpin()的方法,它生成一个21或22的int,并且该方法可以工作,但由于某些原因,当它在spin方法中使用时,它总是旋转21次。

WheelBuilder

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.lang.*;

public class WheelBuilder extends JApplet{

public int total = 1000, score, Tbet = 100, last; 
public JLabel winning;
public JButton spin, Abet, Sbet;
RouletteWheel wheel = new RouletteWheel();
Graphics page;
public Color color;

public void init()
{
    resize(540,600);
    spin = new JButton("SPIN!");
    spin.addActionListener(new spinListener());
    Container cp = getContentPane();
    cp.setVisible(true);
    cp.setBackground((Color.GREEN).darker().darker());
    Abet = new JButton("+BET+");
    Abet.addActionListener(new aListener());
    Sbet = new JButton("-BET-");
    Sbet.addActionListener(new sListener());

    cp.add(Sbet);
    cp.add(spin);
    cp.add(Abet);
    cp.setLayout(new FlowLayout(270, 5, 525));
}
public void paint(Graphics page)
{
    page.setColor((Color.GREEN).darker().darker());
    page.fillRect(0, 0, 1000, 1000);
    setBackground((Color.GREEN).darker().darker());
    wheel.paintWheel(page, wheel.getStatus());
    page.setColor(Color.BLACK);
    page.drawString("TOTAL: "+total, 400, 50);
    page.drawString("Current Bet: "+Tbet, 400, 25);
    page.drawString("Last Spin:", 50, 25);
    page.setColor(Color.WHITE);
    page.fillOval(260, 75, 20, 20);
    page.fillRect(50, 35, 60, 25);
    page.setColor(color);
    page.drawString(""+last, 70, 52);
}
public class spinListener implements ActionListener
{
    Timer tm = new Timer(100, this);
    int count = 0;
    int countEnd = randomSpin();

    public void actionPerformed(ActionEvent e)
    {
        tm.start();
        changeWheel();
        if (wheel.getStatus())
        {
            color = Color.RED;
            last = (int)(Math.random()*7)*2+1;
        }
        else 
        {
            color = Color.BLACK;
            last = (int)(Math.random()*7)*2+2;
        }
    }
    public void changeWheel()
    {
        int countEnd = randomSpin();
        if (count <= countEnd)
        {
            wheel.setStatus(!(wheel.getStatus()));
            repaint();
            count++;
        }

    }
    public int randomSpin()
    {
        return ((int)(Math.random()*2)+21);
    }

}
public class aListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if (Tbet <= total-50)
        {
            Tbet+=50;
        }
        last = 0;
        repaint();
    }
}
public class sListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if (Tbet > 50)
        {
            Tbet-=50;
        }
        last = 0;
        repaint();
    }
}
}

RouletteWheel

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

public class RouletteWheel extends JApplet {

public boolean status;

public void paintWheel(Graphics page, boolean status)
{
    if (status){
        setBackground(Color.green);
        page.setColor(Color.orange.darker().darker());
        page.fillOval(20, 20, 500, 500);
        page.setColor(Color.WHITE);
        page.drawOval(40, 40, 460, 460);
        page.setColor(Color.BLACK);
        int[] xback = {0+70,0+70,30+70,90+70,160+70,240+70,310+70,370+70,400+70,400+70,370+70,310+70,240+70,160+70,90+70,30+70};
        int[] yback = {240+70,160+70,90+70,30+70,0+70,0+70,30+70,90+70,160+70,240+70,310+70,370+70,400+70,400+70,370+70,310+70};
        page.fillPolygon(xback, yback, 16);

        int[] xvals = {0+70,0+70,200+70,30+70,90+70,200+70,160+70,240+70,200+70,310+70,370+70,200+70,400+70,400+70,200+70,370+70,310+70,200+70,240+70,160+70,200+70,90+70,30+70,200+70};
        int[] yvals = {240+70,160+70,200+70,90+70,30+70,200+70,0+70,0+70,200+70,30+70,90+70,200+70,160+70,240+70,200+70,310+70,370+70,200+70,400+70,400+70,200+70,370+70,310+70,200+70};
        page.setColor(Color.BLACK);
        page.setColor(Color.RED);
        page.fillPolygon(xvals, yvals, 24);

        page.setColor(Color.orange.darker().darker());
        page.fillOval(140, 140, 260, 260);
        page.setColor(Color.lightGray);
        page.fillOval(220, 220, 100, 100);
        page.setColor(Color.GRAY);
        page.fillOval(240, 240, 60, 60);
        page.setColor(Color.DARK_GRAY);
        page.fillOval(260, 260, 20, 20);

        page.setColor(Color.WHITE);
        page.drawOval(100, 100, 340, 340);
        page.drawOval(110, 110, 320, 320);

    }
    if (!status)
    {
        setBackground(Color.green);
        page.setColor(Color.orange.darker().darker());
        page.fillOval(20, 20, 500, 500);
        page.setColor(Color.WHITE);
        page.drawOval(40, 40, 460, 460);
        page.setColor(Color.RED);
        int[] xback = {0+70,0+70,30+70,90+70,160+70,240+70,310+70,370+70,400+70,400+70,370+70,310+70,240+70,160+70,90+70,30+70};
        int[] yback = {240+70,160+70,90+70,30+70,0+70,0+70,30+70,90+70,160+70,240+70,310+70,370+70,400+70,400+70,370+70,310+70};
        page.fillPolygon(xback, yback, 16);

        int[] xvals = {0+70,0+70,200+70,30+70,90+70,200+70,160+70,240+70,200+70,310+70,370+70,200+70,400+70,400+70,200+70,370+70,310+70,200+70,240+70,160+70,200+70,90+70,30+70,200+70};
        int[] yvals = {240+70,160+70,200+70,90+70,30+70,200+70,0+70,0+70,200+70,30+70,90+70,200+70,160+70,240+70,200+70,310+70,370+70,200+70,400+70,400+70,200+70,370+70,310+70,200+70};
        page.setColor(Color.BLACK);
        page.fillPolygon(xvals, yvals, 24);

        page.setColor(Color.orange.darker().darker());
        page.fillOval(140, 140, 260, 260);
        page.setColor(Color.lightGray);
        page.fillOval(220, 220, 100, 100);
        page.setColor(Color.GRAY);
        page.fillOval(240, 240, 60, 60);
        page.setColor(Color.DARK_GRAY);
        page.fillOval(260, 260, 20, 20);

        page.setColor(Color.WHITE);
        page.drawOval(100, 100, 340, 340);
        page.drawOval(110, 110, 320, 320);

    }
}
public boolean getStatus()
{
    return status;
}
public void setStatus(boolean s)
{
    status = s;
}
}

0 个答案:

没有答案