Results 1 to 6 of 6
  1. #1
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default For loop in actionPreformed

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.DecimalFormat;
    import java.util.Random;

    public class RollDice extends JFrame
    implements ActionListener
    {

    JTextField displayDie,displayPlayer1, displayPlayer2, displayPlayer3;

    public RollDice()
    {
    super("Rolling");
    JLabel labelDie = new JLabel("You rolled a: ", SwingConstants.RIGHT);
    displayDie = new JTextField(5);
    displayDie.setEditable(false);
    JLabel labelPlayer1 = new JLabel("Player 1: ", SwingConstants.RIGHT);
    displayPlayer1 = new JTextField(5);
    displayPlayer1.setEditable(false);
    /*JLabel labelPlayer2 = new JLabel("Player 2: ", SwingConstants.RIGHT);
    displayPlayer2 = new JTextField(5);
    displayPlayer2.setEditable(false);
    JLabel labelPlayer3 = new JLabel("Player 3: ", SwingConstants.RIGHT);
    displayPlayer3 = new JTextField(5);
    displayPlayer3.setEditable(false);*/
    JButton go = new JButton("Roll");
    go.addActionListener(this);

    Container c = getContentPane();
    c.setBackground(Color.white);
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(2, 1, 5, 5));
    p.add(labelDie);
    p.add(displayDie);
    p.add(labelPlayer1);
    p.add(displayPlayer1);
    c.add(p, BorderLayout.NORTH);
    c.add(go, BorderLayout.SOUTH);
    }


    public void actionPerformed(ActionEvent e)
    {
    Random generator = new Random();
    int pos1=0;
    DecimalFormat df = new DecimalFormat("0");
    int die1 = generator.nextInt(6) + 1;
    for (int i=die1;pos1<=36;i=die1)
    {
    pos1=pos1+die1;
    displayDie.setText(df.format(die1));
    displayPlayer1.setText(df.format(pos1));

    if (pos1>=36)
    {
    displayPlayer1.setText("You win!!");
    }
    if (pos1==19)
    {

    pos1=9;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==2)
    {

    pos1=13;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==29)
    {

    pos1=18;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==10)
    {

    pos1=22;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==12)
    {

    pos1=3;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==34)
    {

    pos1=1;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==17)
    {

    pos1=33;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==5)
    {

    pos1=20;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==25)
    {

    pos1=14;
    displayPlayer1.setText(df.format(pos1));
    }
    if (pos1==15)
    {

    pos1=26;
    displayPlayer1.setText(df.format(pos1));
    }

    die1=generator.nextInt(6)+1;



    }





    }

    public static void main(String[] args)
    {
    RollDice w = new RollDice();
    w.setBounds(300, 300, 300, 90);
    w.setDefaultCloseOperation(EXIT_ON_CLOSE);
    w.setVisible(true);
    }
    }

    The code works, but not the way i want it to. I want the for loop to wait for the Jbutton to be pressed again to continue. As of now it just says "you win!!" every time b/c when the button is pressed the loop goes all the way through.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: For loop in actionPreformed

    Don't use a for loop. Instead use an int variable that increments each time the button is pushed (inside of actionPerformed), and then also inside of actionPerformed, decide what you want to have happen based on the state of this int variable.

  3. #3
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: For loop in actionPreformed

    public void actionPerformed(ActionEvent e)
    {
    Random generator = new Random();
    int pos1=0;
    if (JButton.isSelected(true))
    {
    DecimalFormat df = new DecimalFormat("0");
    int die1 = generator.nextInt(6) + 1;
    pos1=pos1+die1;
    displayDie.setText(df.format(die1));
    displayPlayer1.setText(df.format(pos1));

    }

    }

    I think that might work, but I don't really know how to say "if JButton is pressed" - is the isSelected method the right one to use?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: For loop in actionPreformed

    Quote Originally Posted by berb12 View Post
    but I don't really know how to say "if JButton is pressed" - is the isSelected method the right one to use?
    No. You know when the button is pressed -- the actionPerformed method gets called.

  5. #5
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: For loop in actionPreformed

    Got it working, thanks a lot!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: For loop in actionPreformed

    Quote Originally Posted by berb12 View Post
    Got it working, thanks a lot!
    Good deal! You're quite welcome!

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •