简体   繁体   English

如何使2个JComboBoxes模拟骰子?

[英]How to make 2 JComboBoxes simulate dice?

For school I have to make a game, so I wanted to make a dice rolling game. 为了上学,我必须做一个游戏,所以我想做一个掷骰子的游戏。 My intention is to have 2 JComboBox es, one with how many dices a person wants and one which has the number of sides. 我的意图是拥有2个JComboBox es,一个具有一个人想要多少个骰子,一个具有边数。 I get how to make 1 JComboBox make a difference to the outcome but I don't know how to make both of them chance the outcome. 我知道如何使1个JComboBox对结果有所影响,但我不知道如何使它们两者都获得结果。

This is my code for now, I still need to actually make the button do something but I thought I would solve this first 这是我现在的代码,我仍然需要实际使按钮执行某些操作,但是我认为我会先解决这个问题

public class Dobbel1 extends JFrame {
    // Number of sides
    private int[] zijden = { 4, 6, 8, 10, 12, 20 };

    public Dobbel1() {
        super("Yahtzee!");
        getContentPane().setLayout(new FlowLayout());

        JComboBox<String> sides = new JComboBox<String>();
        for (int i=0;i<zijden.length;i++) {
            sides.addItem("" + zijden[i] + " Sides");
        }
        getContentPane().add(sides);

        JComboBox<String> number = new JComboBox<String>();
        number.addItem("1 Dice");
        number.addItem("2 Dices");
        number.addItem("3 Dices");
        number.addItem("4 Dices");
        number.addItem("5 Dices");
        number.addItem("6 Dices");
        number.addItem("7 Dices");
        number.addItem("8 Dices");
        getContentPane().add(number);

        number.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                JComboBox number = (JComboBox) event.getSource();
                Object selected = number.getSelectedItem();
                if selected.toString().equals("1 Dice");

            }
        });

        JButton button1 = new JButton("Throw!");
        add(button1);

        button1.addActionListener(new Throw());

        }

    public static void main(String[]args){
        Dobbel1 dobbel = new Dobbel1(); 
        dobbel .setSize(800,400);
        dobbel .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dobbel .setMinimumSize(new Dimension(300,100));
        dobbel .setLocation(300,150);
        dobbel .setVisible(true);

        Container contentPane = dobbel .getContentPane();

        Random r = new Random();
        int result = r.nextInt(6);
        result = result + 1;
    }
}

Voila! 瞧!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Dobbel1 extends JFrame {

    private int[] zijden = {4, 6, 8, 10, 12, 20};
    Random r = new Random();
    private int chosenDiceNumber = 1;
    private int chosenSidesNumber = 4;
    JLabel result = new JLabel();

    public Dobbel1() {
        super("Yahtzee!");
        getContentPane().setLayout(new FlowLayout());


        JComboBox<String> sides = new JComboBox<String>();
        for (int i = 0; i < zijden.length; i++) {
            sides.addItem("" + zijden[i] + " Sides");
        }

        sides.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                JComboBox number = (JComboBox) event.getSource();
                String selectedValue = number.getSelectedItem().toString();
                chosenSidesNumber = Integer.valueOf((selectedValue.split("\\s+"))[0]);
            }
        });

        getContentPane().add(sides);

        JComboBox<String> number = new JComboBox<String>();
        number.addItem("1 Dice");
        number.addItem("2 Dices");
        number.addItem("3 Dices");
        number.addItem("4 Dices");
        number.addItem("5 Dices");
        number.addItem("6 Dices");
        number.addItem("7 Dices");
        number.addItem("8 Dices");

        number.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                JComboBox number = (JComboBox) event.getSource();
                String selectedValue = number.getSelectedItem().toString();
                chosenDiceNumber = Integer.valueOf((selectedValue.split("\\s+"))[0]);
            }
        });

        getContentPane().add(number);

        JButton button1 = new JButton("Throw!");
        add(button1);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StringBuilder sb = new StringBuilder("Rolled: ");

                for (int i=0; i<chosenDiceNumber; i++) {
                    sb.append(getNextValue()).append(", ");
                }
                sb.delete(sb.length()-2, sb.length());
                result.setText(sb.toString());
            }
        });
        add(result);
    }

    public static void main(String[] args) {
        Dobbel1 dobbel = new Dobbel1();
        dobbel.setSize(800, 400);
        dobbel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dobbel.setMinimumSize(new Dimension(300, 100));
        dobbel.setLocation(300, 150);
        dobbel.setVisible(true);
    }

    private int getNextValue() {
        return r.nextInt(chosenSidesNumber) + 1;
    }
}

It's an example of a dirty code, but anyway, you've got the idea. 这是一个肮脏代码的示例,但是无论如何,您已经有了主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM