我制作了掷骰子的代码,代码尚未完全完成。我想检查它,但是当我运行程序时没有按钮/组合框出现。有人能告诉我我的代码有什么问题以及如何修复它?
这是我现在的代码:
public class StartGame implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame game = new JFrame();
game.setSize(800, 400);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setMinimumSize(new Dimension(300, 100));
game.setLocation(300, 150);
game.setVisible(true);
}
private int[] zijden = {4, 6, 8, 10, 12, 20};
Random r = new Random();
private int chosenDiceNumber = 1;
private int chosenSidesNumber = 4;
static JLabel result = new JLabel();
public void Dices() {
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);
JButton button2 = new JButton("Best Yahtzee move");
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());
private void add(JButton button2) {
// TODO Auto-generated method stub
}
});
private Object getNextValue() {
// TODO Auto-generated method stub
return null;
}
});
add(result);
}
private void add(JButton button1) {
// TODO Auto-generated method stub
}
private void add(JLabel result2) {
// TODO Auto-generated method stub
}
private AbstractButton getContentPane() {
// TODO Auto-generated method stub
return null;
}
private int getNextValue() {
return r.nextInt(chosenSidesNumber) + 1;
}
}
答案 0 :(得分:0)
添加一个框架并将其作为参数传递给您的方法。
public void Dices(JFrame game) {
game.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]);
}
});
game.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]);
}
});
game.add(number);
JButton button1 = new JButton("Throw!");
game.add(button1);
JButton button2 = new JButton("Best Yahtzee move");
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());
}
});
game.add(result);
}