JButton help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: JButton help

  1. #1
    Join Date
    Jul 2006
    Posts
    5

    JButton help

    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?

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    hm? post your code so that we may see what the problem is.

  3. #3
    Join Date
    Jul 2006
    Posts
    5
    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.

    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    import javax.swing.JOptionPane;
    import java.awt.event.*;

    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 void paintComponent(Graphics g)
    {
    button.setSize(112, 95);
    button.setLocation(363, 538);
    button.addActionListener(new CashierListener());
    add(button);

    button2.setSize(112, 95);
    button2.setLocation(521, 538);
    button2.addActionListener(new CashoutListener());
    add(button2);

    button3.setSize(112, 95);
    button3.setLocation(667, 538);
    button3.addActionListener(new BetOneListener());
    add(button3);

    button4.setSize(112, 95);
    button4.setLocation(813, 538);
    button4.addActionListener(new BetMaxListener());
    add(button4);

    button5.setSize(171, 150);
    button5.setLocation(958, 487);
    button5.addActionListener(new MyStartListener());
    add(button5);

    Image image = images[frame];
    Image image2 = images[frame2];
    Image image3 = images[frame3];

    g.drawImage(background, 0, 0, this);
    g.drawImage(image, 199, 150, this);
    g.drawImage(image2, 475, 150, this);
    g.drawImage(image3, 750, 150, this);
    g.setColor(Color.red);
    g.setFont(new Font("Garamond", Font.BOLD, 50));
    g.drawString("" + bet, 770, 428);
    g.drawString("" + credits, 225, 428);
    g.drawString("" + win, 871, 428);
    }



    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);

    frame = (frame+a)%images.length;
    frame2 = (frame2+s)%images.length;
    frame3 = (frame3+d)%images.length;

    Thread.sleep(delay);
    repaint();
    ++t;
    }
    ++spinCounter;
    //JOptionPane.showMessageDialog(null, "" + spinCounter);
    wait();
    }
    catch (Exception e){}
    //PayoutTest();
    }
    }.start();

    }
    }

    public class BetOneListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.setFont(new Font("Garamond", Font.BOLD, 50));
    if (bet<3)
    {
    g.drawString("" + bet, 770, 428);
    ++bet;
    g.setColor(Color.red);
    g.drawString("" + bet, 770, 428);
    }
    else
    {
    g.drawString("" + bet, 770, 428);
    g.setColor(Color.red);
    g.drawString("3", 770, 428);
    }
    }
    }

    public class BetMaxListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.setFont(new Font("Garamond", Font.BOLD, 50));
    g.drawString("" + bet, 770, 428);
    g.setColor(Color.red);
    bet = 3;
    g.drawString("" + bet, 770, 428);
    }
    }

    public class CashoutListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }

    public class CashierListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }
    }

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2006, 11:09 PM
  2. Replies: 7
    Last Post: 03-07-2006, 05:34 PM
  3. plz help me n00b here
    By Apocalyp5e in forum Java
    Replies: 11
    Last Post: 12-22-2005, 09:03 PM
  4. Re: applet- help- the code
    By shafi in forum Java
    Replies: 1
    Last Post: 07-26-2001, 12:32 PM
  5. How to remove focus from jbutton?
    By Ryno in forum Java
    Replies: 0
    Last Post: 11-28-2000, 04:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center