我正在构建我的第一个Java应用程序并且似乎无法使其正常运行。它是一台带有GUI和独立课程的老虎机,它可以很好地运行。
这是addSlots方法:
lblSlot1 = new JLabel("?");
lblSlot1.setBounds(10, 50, 100, 20);
panel.add(lblSlot1);
lblSlot2 = new JLabel("?");
lblSlot2.setBounds(110, 50, 100, 20);
panel.add(lblSlot2);
lblSlot3 = new JLabel("?");
lblSlot3.setBounds(210, 50, 100, 20);
panel.add(lblSlot3);
lblResult = new JLabel("Result goes here");
lblResult.setBounds(10, 100, 100, 20);
panel.add(lblResult);
}
public void addButtons(){
btnSpin = new JButton("Spin");
btnSpin.setBounds(50, 200, 100, 20);
btnSpin.addActionListener(new SpinHandler());
panel.add(btnSpin);
btnExit = new JButton("Exit");
btnExit.setBounds(250, 200, 100, 20);
btnExit.addActionListener(new ExitHandler());
panel.add(btnExit);
动作监听器:
class SpinHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Spin");
SlotMachine sm = new SlotMachine();
lblSlot1.setText(String.valueOf(sm.getSlot1()));
lblSlot2.setText(String.valueOf(sm.getSlot2()));
lblSlot3.setText(String.valueOf(sm.getSlot3()));
lblResult.setText(String.valueOf(sm.getResult()));
}
它只生成0,0,0而不是随机数。
这是slotmachine类:
public int getSlot1(){
return slot1;
}
public int getSlot2(){
return slot2;
}
public int getSlot3(){
return slot3;
}
public void spin(){
Random rand = new Random();
slot1 = rand.nextInt(10);
slot2 = rand.nextInt(10);
slot3 = rand.nextInt(10);
答案 0 :(得分:0)
您似乎忘了在 getSlot #()方法中调用 spin ()方法