I've done some reading on swing and JButtons but obviously not enough. I'm confused I have a program that uses JButtons and is essential that when the button is pressed it executes the code within the action performed method once (its a counter that increases once each time the button is pressed). But when the button is pressed once it continues to loop until the button is pressed again, then it starts with a loop all over again. Is this normal? and how do I get it to stop? :confused:
08-15-2006, 11:43 PM
anubis
hm? post your code so that we may see what the problem is.
08-17-2006, 09:34 PM
Dr Spud
well at this point I'm not sure that my button is running a loop anymore. but there definately is a problem and I'm not sure what I did. so here's the code I hope someone can take the time to look through and tell my what I did wrong. Basically what is happening is I have a bet max button that is supposed to set the bet to 3. which is working. It also should subtract 3 from the credits each time the spin button is pressed. It works the first time spin is pressed. but the second time its pressed the credits go from 97 to -296. Im not sure how to fix this becasuse I cant see what I did wrong.
public class SpudSlotTest extends JComponent
{
Image background = new ImageIcon("background1.png").getImage();
Image[] images = new Image[10];
int x = (int)(Math.random()*10);
int y = (int)(Math.random()*10);
int z = (int)(Math.random()*10);
int frame = x;
int frame2= y;
int frame3= z;
int bet = 0;
int credits = 100;
int win = 0;
int spinCounter = 0;
ImageIcon change = new ImageIcon("change.png");
JButton button = new JButton(change);
ImageIcon cashout = new ImageIcon("cashout.png");
JButton button2 = new JButton(cashout);
ImageIcon betone = new ImageIcon("betone.png");
JButton button3 = new JButton(betone);
ImageIcon betmax = new ImageIcon("betmax.png");
JButton button4 = new JButton(betmax);
ImageIcon spin = new ImageIcon("spin.png");
JButton button5 = new JButton(spin);
public static void main(String[] args)
{
SpudSlotTest slot = new SpudSlotTest();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 800);
frame.getContentPane().add(slot);
frame.setVisible(true);
}
/* public void PayoutTest()
public class MyStartListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
credits = credits - bet;
new Thread()
{
public void run()
{
images[0] = new ImageIcon(
"spud1.png").getImage();
images[1] = new ImageIcon(
"blank2.png").getImage();
images[2] = new ImageIcon(
"cherries.png").getImage();
images[3] = new ImageIcon(
"blank3.png").getImage();
images[4] = new ImageIcon(
"triplebone.png").getImage();
images[5] = new ImageIcon(
"blank4.png").getImage();
images[6] = new ImageIcon(
"doublebone.png").getImage();
images[7] = new ImageIcon(
"blank5.png").getImage();
images[8] = new ImageIcon(
"bone.png").getImage();
images[9] = new ImageIcon(
"blank6.png").getImage();
int delay = 10;
try
{
int t = (int)(Math.random() * 10);
while (t<150)
{
int a = (int)(Math.random()*10);
int s = (int)(Math.random()*10);
int d = (int)(Math.random()*10);