-
《
Java
语言程序设计(基础篇)
》
(第
10
版
梁勇
著)
第三十六章
练习题答案
36.1
import
ation;
import
import
import
import
ea;
import
eld;
import
Pane;
import
import
public
class
Exercise36_01
extends
Application {
private
CalendarPane calendarPane =
new
CalendarPane();
private
TextField tfUnicode =
new
TextField();
private
TextArea
taUnicodeCharacters =
new
TextArea();
@
Override
//
Override the start method in the Application
class
public
void
start(Stage
primaryStage) {
HBox hBox =
new
HBox(5);
ldren().addAll(
new
Label(
), tfUnicode);
gnment();
fColumnCount(4);
BorderPane pane =
new
BorderPane();
ter(taUnicodeCharacters);
(hBox);
//
Create a scene and place it in the
stage
Scene scene =
new
Scene(pane, 400, 300);
le(
);
// Set the stage title
ne(scene);
// Place the
scene in the stage
();
// Display the stage
ction(e -> {
int
code =
nt(t().toString(), 16);
n(code);
displayUnicode(code);
});
}
private
void
displayUnicode(
int
code) {
t(
null
);
for
(
int
i = 0; i < 20; i++) {
Text(
tring(code) +
);
for
(
int
j = 0; j < 16; j++) {
Text((
char
)code +
);
code++;
}
Text(
);
}
}
/**
* The main
method is only needed for the IDE with
limited
* JavaFX support.
Not needed for running from the command
line.
*/
public
static
void
main(String[] args) {
launch(args);
}
}
36.2
import
rmat;
import
import
import
import
ne;
import
ation;
import
import
import
ox;
import
import
eld;
import
import
import
public
class
Exercise36_02
extends
Application {
private
Locale[] availableLocales = ilableLocales();
private
String[] availableTimeZones = ilableIDs();
private
DateFormat dateFormat = eInstance();
private
DateFormat
timeFormat = eInstance();
private
Date date =
new
Date();
private
TextField tfDate =
new
TextField((date));
private
TextField tfTime =
new
TextField((date));
private
ComboBox
new
ComboBox<>();
private
ComboBox
new
ComboBox<>();
private
ComboBox
new
ComboBox<>();
private
ComboBox
new
ComboBox<>();
private
Locale locale =
private
String
timeZone = ault().getID();
private
int
dateStyle =
private
int
timeStyle =
@
Override
//
Override the start method in the Application
class
public
void
start(Stage
primaryStage) {
//
Initialize cboLocales with all available
locales
setAvailableLocales();
// Initialize cboTimeZones with all
available time zones
setAvailableTimeZones();
ms().addAll(
,
,
,
);
ms().addAll(
,
,
,
);
HBox hBox1 =
new
HBox(5);
ldren().addAll(
new
Label(
), tfDate,
new
Label(
), tfTime);
HBox hBox2 =
new
HBox(5);
ldren().addAll(
new
Label(
), cboLocale,
new
Label(
), cboTimeZone);
HBox hBox3 =
new
HBox(5);
ldren().addAll(
new
Label(
), cboDateStyle,
new
Label(
), cboTimeStyle);
gnment();
VBox vBox =
new
VBox(5);
ldren().addAll(hBox1, hBox2, hBox3);
// Create a scene and
place it in the stage
Scene scene =
new
Scene(vBox, 400, 200);
le(
);
// Set the
stage title
ne(scene);
// Place the scene in the
stage
();
//
Display the stage
ction(e -> {
locale =
availableLocales[ectionModel()
.getSelectedIndex()];
process();
});
ction(e -> {
timeZone =
availableTimeZones[ectionModel().getSelectedInde x()];
process();
});
ction(e -> {
dateStyle =
ectionModel().getSelectedIndex();
process();
});
ction(e -> {
timeStyle = ectionModel().getSelectedIndex();
process();
});
}
private
void
process() {
dateFormat = eInstance(dateStyle, locale);
timeFormat = eInstance(timeStyle,
locale);
eZone(eZone(timeZone));
eZone(eZone(timeZone));
t((
new
Date()));
t((
new
Date()));
}
private
void
setAvailableLocales() {
for
(
int
i = 0; i < i++)
ms().add(availableLocales[i]
.getDisplayName() +
+
availableLocales[i].toString());
ectionModel().selectFirst();
}
private
void
setAvailableTimeZones()
{
// Sort time
zones
(availableTimeZones);
for
(
int
i = 0; i < i++) {
ms().add(availableTimeZones[i]);
}
ectionModel().selectFirst();
}
/**
* The main
method is only needed for the IDE with
limited
* JavaFX support.
Not needed for running from the command
line.
*/
public
static
void
main(String[] args) {
launch(args);
}
}
36.3
import
rmat;
import
rmatSymbols;
import
DateFormat;
import
ar;
import
ianCalendar;
import
import
ne;
import
me;
import
ne;
import
ation;
import
Event;
import
andler;
import
import
import
import
Pane;
import
ne;
import
import
import
ignment;
import
import
on;
public
class
Exercise36_03
extends
Application {
@
Override
// Override the start method in the
Application class
public
void
start(Stage
primaryStage) {
HBox hBox =
new
HBox(5);
ldren().addAll(
new
CalendarPane(),
new
WorldClock());
// Create a
scene and place it in the stage
Scene scene =
new
Scene(hBox, 500, 200);
le(
);
// Set the stage title
ne(scene);
// Place the
scene in the stage
();
// Display the stage
}
/**
* The main
method is only needed for the IDE with limited
JavaFX support.
Not
* needed for running from the
command line.
*/
public
static
void
main(String[] args) {
launch(args);
}
class
WorldClock
extends
BorderPane {
private
TimeZone timeZone =
eZone(
);
private
Locale locale =
ault();
private
ClockPane clock =
new
ClockPane();
// Still clock
private
Label lblDigitTime =
new
Label();
public
WorldClock() {
setCenter(clock);
setBottom(lblDigitTime);
gnment(lblDigitTime, );
EventHandler
setCurrentTime();
//
Set a new clock time
};
//
Create an animation for a running clock
Timeline animation =
new
Timeline(
new
KeyFrame((1000),
eventHandler));
leCount(NITE);
();
// Start
animation
// Resize the clock
widthProperty().addListener(ov ->
(getWidth()));
heightProperty().addListener(ov -> (getHeight()));
}
public
void
setTimeZone(TimeZone
timeZone) {
this
.timeZone = timeZone;
}
public
void
setLocale(Locale
locale) {
this
.locale = locale;
}
private
void
setCurrentTime() {
Calendar calendar =
new
GregorianCalendar(timeZone, locale);
r(());
ute(());
ond(());
// Display digit time on the
label
DateFormat
formatter
=
eTimeInstance(,
, locale);
eZone(timeZone);
t((e()));
}
}
class
CalendarPane
extends
BorderPane {
// The header
label
private
Label lblHeader =
new
Label();
// Maximum number of
labels to display day names and days
private
Label[] lblDay =
new
Label[49];
private
Calendar calendar;
private
int
month;
//
The specified month
private
int
year;
// The
specified year
private
Locale locale =
public
CalendarPane() {
// Create labels for
displaying days
for
(
int
i = 0; i < 49; i++) {
lblDay[i] =
new
Label();
lblDay[i].setTextAlignment();
}
showDayNames();
// Display day names for the
locale
GridPane dayPane =
new
GridPane();
gnment();
p(10);
p(10);
for
(
int
i = 0; i < 49; i++) {
(lblDay[i], i % 7, i / 7);
}
// Place header and calendar body in
the pane
this
.setTop(lblHeader);
gnment(lblHeader, );
this
.setCenter(dayPane);
// Set
current month and year
calendar =
new
GregorianCalendar();
month = ();
year = ();
updateCalendar();
// Show calendar
showHeader();
showDays();
}
/**
* Update
the day names based on locale
*/
private
void
showDayNames() {
DateFormatSymbols dfs =
new
DateFormatSymbols(locale);
String
dayNames[] = kdays();
// jlblDay[0], jlblDay[1], ...,
jlblDay[6] for day names
for
(
int
i = 0; i < 7; i++) {
lblDay[i].setText(dayNames[i +
1]);
}
}
/**
* Update
the header based on locale
*/
private
void
showHeader() {
SimpleDateFormat sdf =
new
SimpleDateFormat(
, locale);
String header = (e());
t(header);
}
public
void
showDays() {
// Get the day of the
first day in a month
int
startingDayOfMonth =
(_OF_WEEK);
// Fill the calendar with the days
before this month
Calendar cloneCalendar = (Calendar) ();
(, -1);
// Becomes
preceding month
int
daysInPrecedingMonth =
ualMaximum(
_OF_MONTH);
for
(
int
i = 0; i <
startingDayOfMonth - 1; i++) {
lblDay[i + 7].setTextFill(RAY);
lblDay[i + 7].setText(daysInPrecedingMonth
- startingDayOfMonth +
2 + i +
);
}
//
Display days of this month
int
daysInCurrentMonth =
ualMaximum(
_OF_MONTH);
for
(
int
i = 1; i <=
daysInCurrentMonth; i++) {
lblDay[i - 2 + startingDayOfMonth +
7].setTextFill();
lblDay[i - 2
+ startingDayOfMonth + 7].setText(i +
);
}
// Fill
the calendar with the days after this
month
int
j = 1;
for
(
int
i = daysInCurrentMonth
- 1 + startingDayOfMonth + 7;
i < 49; i++) {
lblDay[i].setTextFill(RAY);
lblDay[i].setText(j++ +
);
}
}
/**
* Set the calendar to the first
day of the specified month and year
*/
public
void
updateCalendar() {
(, year);
(,
month);
(, 1);
}
public
int
getMonth() {
return
month;
}
public
void
setMonth(
int
newMonth) {
month = newMonth;
updateCalendar();
showHeader();
showDays();
}
public
int
getYear() {
return
year;
}
public
void
setYear(
int
newYear) {
year = newYear;
updateCalendar();
showHeader();
showDays();
}
public
void
setLocale(Locale
locale) {
this
.locale = locale;
updateCalendar();
showDayNames();
showHeader();
showDays();
}
}
}
-
-
-
-
-
-
-
-
-
上一篇:最新人教版七年级英语上册期中测试题(含答案)
下一篇:第一学期七年级英语试题