Results 1 to 6 of 6
Thread: For loop in actionPreformed
- 11-09-2011, 02:29 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
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.
-
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.
- 11-09-2011, 03:00 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
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?
-
Re: For loop in actionPreformed
- 11-09-2011, 04:34 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
Re: For loop in actionPreformed
Got it working, thanks a lot!
-
Similar Threads
-
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks