diceservice.java

来自「HeadFirstCode系列图书里的源代码」· Java 代码 · 共 41 行

JAVA
41
字号
import javax.swing.*;import java.awt.event.*;import java.io.*;public class DiceService implements Service {    JLabel label;    JComboBox numOfDice;    public JPanel getGuiPanel() {       JPanel panel = new JPanel();       JButton button = new JButton("Roll 'em!");       String[] choices = {"1", "2", "3", "4", "5"};       numOfDice = new JComboBox(choices);       label = new JLabel("dice values here");       button.addActionListener(new RollEmListener());       panel.add(numOfDice);       panel.add(button);       panel.add(label);       return panel;    }   public class RollEmListener implements ActionListener {      public void actionPerformed(ActionEvent ev) {         // roll the dice         String diceOutput = "";         String selection = (String)  numOfDice.getSelectedItem();         int numOfDiceToRoll = Integer.parseInt(selection);         for (int i = 0; i < numOfDiceToRoll; i++) {            int r = (int) ((Math.random() * 6) + 1);            diceOutput += (" " + r);         }        label.setText(diceOutput);               }    }       }                   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?