⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diceservice.java

📁 HeadFirstCode系列图书里的源代码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -