FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Head First Java/DiceService.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Head First Java
/
DiceService.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (32 loc) · 954 Bytes
Breadcrumbs
JavaExercises
/
Head First Java
/
DiceService.java
Copy path
File metadata and controls
36 lines (32 loc) · 954 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL