老虎机随机

时间:2015-01-05 21:38:50

标签: java random jtextfield slot

我正在创建一个老虎机项目(为了好玩)。我正在JTextField中显示水果。我已经设法在JTextFields中显示随机果实,但随机果实是相同的,例如,结果将是 - >橙色,橙色,橙色。或者它会是,Kiwi,Kiwi,Kiwi。等等。 这是我的代码。

    import java.awt.BorderLayout;...

    import java.util.Random;


    public class Game extends JFrame {

    String [] fruits = { "Cherry", "Lemon", "Orange", "Grape", "Kiwi" };

    String fruit = fruits[(int) (Math.random() * fruits.length)];

    private JPanel contentPane;
    private JTextField Slot_1;
    private JTextField Slot_2;
    private JTextField Slot_3;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Game frame = new Game();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    }

    /**
    * Create the frame.
    */
    public Game() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    Slot_1 = new JTextField();
    Slot_1.setText("----------------");
    Slot_1.setEditable(false);
    Slot_1.setBounds(41, 53, 95, 28);
    contentPane.add(Slot_1);
    Slot_1.setColumns(10);

    Slot_2 = new JTextField();
    Slot_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    Slot_2.setText("-----------------");
    Slot_2.setEditable(false);
    Slot_2.setColumns(10);
    Slot_2.setBounds(183, 53, 95, 28);
    contentPane.add(Slot_2);

    Slot_3 = new JTextField();
    Slot_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    Slot_3.setText("---------------");
    Slot_3.setEditable(false);
    Slot_3.setColumns(10);
    Slot_3.setBounds(317, 53, 95, 28);
    contentPane.add(Slot_3);

    JButton btn_Pull = new JButton("Pull");
    btn_Pull.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            Slot_1.setText(fruit);
            Slot_2.setText(fruit);
            Slot_3.setText(fruit);

        }
    });
    btn_Pull.setBounds(145, 108, 166, 29);
    contentPane.add(btn_Pull);

    JButton btnMenu = new JButton("Menu");
    btnMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });

    btnMenu.setBounds(383, 243, 61, 29);
    contentPane.add(btnMenu);




}
}

谢谢

4 个答案:

答案 0 :(得分:3)

String fruit = fruits[(int) (Math.random() * fruits.length)];

水果的价值在你班级开始时确定。

        Slot_1.setText(fruit);
        Slot_2.setText(fruit);
        Slot_3.setText(fruit);

水果的价值不会因为您将其分配给3个不同的文字字段而发生变化。

如果你想要随机值,那么你需要3次调用random()方法。也许是这样的:

Slot_1.setText( fruits[(int) (Math.random() * fruits.length)] );
...

或者创建一个返回随机字符串的方法:

private String getFruit()
{
    return fruits[(int) (Math.random() * fruits.length)];
}

并使用:

slot_1.setText( getFruit() );

此外:

  1. 变量名称不应以大写字母(Slot_1 ...)开头。你的其他变量是正确的(水果,水果),所以要保持一致。

  2. 不要使用空布局。 Swing旨在与布局管理器一起使用。

答案 1 :(得分:0)

Slot_1.setText(fruit);
Slot_2.setText(fruit);
Slot_3.setText(fruit);

你期望这些神奇地改变吗? fruit只是一个字符串变量,您只需在类顶部设置一次。您需要为每个插槽调用Math.random()功能。您可以通过使fruit函数获得随机数并每次生成新的水果来实现此目的,然后您将fruit替换为fruit()。或者你可以从这里的行顶部复制简单的代码行。

答案 2 :(得分:0)

为您的类添加一个名为generateFruit();

的函数
private String generateFruit()
{
    return this.fruits[(int) (Math.random() * this.fruits.length)];
}

然后调用它而不是set fruit变量。

Slot_1.setText(this.generateFruit());

答案 3 :(得分:0)

我还添加了一个更改fruit1,fruit2和fruit3的方法,并在动作侦听器中调用该方法来更改值。这会在每次推拉时改变值。

public class Game扩展JFrame {     String [] fruits = {" Cherry"," Lemon"," Orange"," Grape"," Kiwi" };

String fruit1 = fruits[(int) (Math.random() * fruits.length)];
String fruit2 = fruits[(int) (Math.random() * fruits.length)];
String fruit3 = fruits[(int) (Math.random() * fruits.length)];

private JPanel contentPane;
private JTextField Slot_1;
private JTextField Slot_2;
private JTextField Slot_3;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Game frame = new Game();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Game() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    Slot_1 = new JTextField();
    Slot_1.setText("----------------");
    Slot_1.setEditable(false);
    Slot_1.setBounds(41, 53, 95, 28);
    contentPane.add(Slot_1);
    Slot_1.setColumns(10);

    Slot_2 = new JTextField();
    Slot_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    Slot_2.setText("-----------------");
    Slot_2.setEditable(false);
    Slot_2.setColumns(10);
    Slot_2.setBounds(183, 53, 95, 28);
    contentPane.add(Slot_2);

    Slot_3 = new JTextField();
    Slot_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    Slot_3.setText("---------------");
    Slot_3.setEditable(false);
    Slot_3.setColumns(10);
    Slot_3.setBounds(317, 53, 95, 28);
    contentPane.add(Slot_3);

    JButton btn_Pull = new JButton("Pull");
    btn_Pull.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            Slot_1.setText(fruit1);
            Slot_2.setText(fruit2);
            Slot_3.setText(fruit3);
            newRandomPullValues();
        }
    });
    btn_Pull.setBounds(145, 108, 166, 29);
    contentPane.add(btn_Pull);

    JButton btnMenu = new JButton("Menu");
    btnMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });

    btnMenu.setBounds(383, 243, 61, 29);
    contentPane.add(btnMenu);




}

public void newRandomPullValues() {
    fruit1 = fruits[(int) (Math.random() * fruits.length)];
    fruit2 = fruits[(int) (Math.random() * fruits.length)];
    fruit3 = fruits[(int) (Math.random() * fruits.length)];
}

}