so I I'm trying to make a program where, when I click my Skate Or dice button, each label(representing a dice) changes to a random element from the corresponding list It belongs to. but so far when I click the button, the labels don't change to a random element, it gives me the same one each time I click it. I tested the output by printing it to the system when I click the button, and surly I get the same 4 answers eachtime. I would like some help figuring out why it chooses the same elements from each list repeatedly instead of different ones every time.
this is my main class
package skateordicegui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SKFrame extends SkateDice {
public JFrame jf;
public JPanel jp;
static public JButton sod = new JButton("SKATE OR DICE!!");
static public JLabel l1 = new JLabel("Stance");
static public JLabel l2 = new JLabel("Turn");
static public JLabel l3 = new JLabel("Flip");
static public JLabel l4 = new JLabel("Spin");
public SKFrame() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
jf = new JFrame();
jf.setSize((d.width / 2), (d.height / 2));
jf.setLocation((d.width / 2 - d.width / 4 ), (d.height / 2 - d.height / 4));
jf.setMinimumSize(jf.getSize());
jf.setResizable(true);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1.setBounds(110, 150, 100, 40);
l2.setBounds(230, 150, 100, 40);
l3.setBounds(350, 150, 100,40);
l4.setBounds(470, 150, 100,40);
sod.setLocation(140, 20);
sod.setSize(300, 100);
jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
jp.setSize(jf.getWidth(), jf.getHeight() / 2);
jp.add(l1);
jp.add(l2);
jp.add(l3);
jp.add(l4);
jp.add(sod);
jf.add(jp);
sod.addActionListener(action);
}
public ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == sod) {
System.out.println(SkateDice.getStance());
System.out.println(SkateDice.getTurn());
System.out.println(SkateDice.getFlip());
System.out.println(SkateDice.getSpin());
l1.setText(SkateDice.getStance());
l2.setText(SkateDice.getTurn());
l3.setText(SkateDice.getFlip());
l4.setText(SkateDice.getSpin());
} else
System.out.println("no swag");
}
};
public static void main(String[] args) {
SKFrame skf = new SKFrame();
}
}
and this is my SkateDice class
package skateordicegui;
import java.util.Random;
public class SkateDice {
static Random r = new Random();
// First time dealing with arrays
//this is the easy way (kinda like python)
static String[] d1 = {"Kickflip", "Kickflip", "Kickflip", "Heelflip", "Heelflip","Heelflip", "Shuv", "3 Shuv", "Shuv", "3 Shuv", "X", "Skate Or Dice"};
static String[] d2 = {"180", "180", "180", "360", "360", "360", "X", "Skate Or Dice"};
//OR
static String d3[] = {"FS", "FS", "BS", "BS", "FS", "BS", "X", "Skate Or Dice"};
static String d4[] = {"Regular", "Regular", "Regular", "Switch", "Switch", "Switch", "Nollie", "Nollie", "Nollie", "Fakie", "Fakie", "Fakie", "X", "Skate Or Dice"};
public static int r1 = r.nextInt(d1.length);
public static int r2 = r.nextInt(d2.length);
public static int r3 = r.nextInt(d3.length);
public static int r4 = r.nextInt(d4.length);
public static String flip = d1[r1];
public static String spin = d2[r2];
public static String turn = d3[r3];
public static String stance = d4[r4];
static public String getFlip() {
return flip;
}
static public String getSpin() {
return spin;
}
static public String getTurn() {
return turn;
}
static public String getStance() {
return stance;
}
}
Aucun commentaire:
Enregistrer un commentaire