Pregunta

I am a beginner with Java and I need to make a program that rolls 1-8 dice. How can I add a textbox where I can fill in the number of dice to show (and roll) with Java Netbeans?

This is my code to roll 3 dice:

public class ToetsJan extends JFrame {
    public static void main(String args[]){
        JFrame frame = new ToetsJan();
        frame.setSize(600,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Dobbelsteen toets");
        frame.setContentPane(new TekenPaneel());
        frame.setVisible(true);
    }
}

class TekenPaneel extends JPanel {

    private Dobbelsteen steen1, steen2, steen3;
    private JButton werpKnop;

    //constructor
    public TekenPaneel() {
        setLayout(null);

        steen1=new Dobbelsteen(1);
        steen2=new Dobbelsteen(3);
        steen3=new Dobbelsteen(5);

        werpKnop = new JButton();
        werpKnop.setText("Gooi!");
        werpKnop.setBounds(50,150,70,25);
        werpKnop.addActionListener(new WerpKnopHandler());
        add (werpKnop);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        steen1.tekenStenen(g);
        steen1.tekenOgen(g);
        steen2.tekenStenen(g);
        steen2.tekenOgen(g);
        steen3.tekenStenen(g);
        steen3.tekenOgen(g);
    }


    class WerpKnopHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            steen1.gooi();
            repaint();
        }
    }

}


public class Dobbelsteen{
    private int x, y;


    public Dobbelsteen(int x){
        this.x = x;
        y = 50*x;
    }

    public void tekenStenen(Graphics g){
        // Teken de dobbelsteen
        g.setColor( Color.BLACK );
        g.fillRoundRect((y), 55, 60, 60, 25, 25 );
        g.setColor( Color.WHITE );
        g.drawRoundRect((y), 55, 60, 60, 25, 25 );
    }

    public void tekenOgen(Graphics g){    
           int getal = gooi();   
           if (getal == 1) {
               g.fillOval((y+25), 80, 8, 8);
            }
            else if (getal ==  2) {
                g.fillOval((y+5), 60, 8, 8);
                g.fillOval((y+45), 100, 8, 8);
            }
            else if (getal ==  3){
                g.fillOval((y+25), 80, 8, 8);
                g.fillOval((y+5), 60, 8, 8);
                g.fillOval((y+45), 100, 8, 8);
            }
            else if (getal ==  4){
                g.fillOval((y+5), 60, 8, 8);
                g.fillOval((y+45), 100, 8, 8);
                g.fillOval((y+5), 100, 8, 8);
                g.fillOval((y+45), 60, 8, 8);
            }
            else if (getal ==  5){
                g.fillOval((y+25), 80, 8, 8);
                g.fillOval((y+5), 60, 8, 8);
                g.fillOval((y+45), 100, 8, 8);
                g.fillOval((y+5), 100, 8, 8);
                g.fillOval((y+45), 60, 8, 8);
            }
            else if (getal ==  6){
                g.fillOval((y+5), 60, 8, 8);
                g.fillOval((y+45), 100, 8, 8);
                g.fillOval((y+5), 100, 8, 8);
                g.fillOval((y+45), 60, 8, 8);
                g.fillOval((y+5), 80, 8, 8);
                g.fillOval((y+45), 80, 8, 8);
            }    
    }

    public int gooi() {
        return (int) (6 * Math.random() + 1);
    }
}
¿Fue útil?

Solución

Your code layout was a train wreck. Please write neat and legible code with matching indentation. I advise you to use English names for classes, variables and methods. Otherwise it will be confusing when you read them alongside JDK or library names which are nearly always in English.

To show a text dialog you can use the following code:

public class TextDialog extends Dialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    private boolean dialogCompleted = false;
    private Button ok, can;
    private TextField input;

    public TextDialog() {

        super(new Frame(""), "Text dialog", true);
        setLayout(new FlowLayout());
        input = new TextField(15);                                
        add(new Label("Input :"));
        add(input);                
        addOKCancelPanel();
        createFrame();
        pack();
        setVisible(true);
    }

    private void addOKCancelPanel() {

        Panel p = new Panel();
        p.setLayout(new FlowLayout());
        createButtons(p);
        add(p);
    }

    private void createButtons(Panel p) {

        p.add(ok = new Button("OK"));
        ok.addActionListener(this);
        p.add(can = new Button("Cancel"));
        can.addActionListener(this);
    }

    private void createFrame() {

        Dimension d = getToolkit().getScreenSize();
        setLocation(d.width / 4, d.height / 3);
    }

    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == ok) {

            dialogCompleted = true;
            setVisible(false);
        } else if (ae.getSource() == can) {

            dialogCompleted = false;
            setVisible(false);
        }
    }

    public boolean isDialogCompleted() {
        return dialogCompleted;
    }

    public String getInput() {
        return input.getText();
    }     
}

To set up an arbitrary number of dice you need a datastructure that can hold any number of dice. Take a look at java.util.List

For example:

private List<Dobbelsteen> dice;

public TekenPaneel() {

    TextDialog dialog = new TextDialog();
    int numberOfDice;
    if (dialog.isDialogCompleted()) {
        numberOfDice = Integer.parseInt(dialog.getInput());
    }
    else {
        // use a default if the user cancels the input
        numberOfDice = 2;
    }
    dice = new ArrayList<Dobbelsteen>();
    int x = 1;
    for(int i = 1; i < numberOfDice; i++) {   
        dice.add(new Dobbelsteen(x));
        x = x + 2;
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top