对于学校我必须做一个游戏,所以我想做一个骰子滚动游戏。我的意图是有两个JComboBox
es,一个有一个人想要多少骰子,另一个有多边。我得到了如何让1个JComboBox对结果产生影响,但我不知道如何让它们都成为可能的结果。
这是我现在的代码,我仍然需要让按钮做一些事情,但我想我会先解决这个问题
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;
}
}
答案 0 :(得分:0)
瞧!
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;
}
}
这是一个脏代码的例子,但无论如何,你已经有了这个想法。