JavaFX¿¡¼ FXMLÀ» »ç¿ëÇÏ¿© ¿ù ´Þ·Â ȸéÀ» ±¸ÇöÇÏ·Á¸é ´ÙÀ½ ´Ü°è¸¦ µû¸£½Ã¸é µË´Ï´Ù.
- FXML ÆÄÀÏ »ý¼º:
Calendar.fxml
À̶ó´Â FXML ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù. ±âº»ÀûÀÎ ±¸Á¶´Â ´ÙÀ½°ú °°½À´Ï´Ù.
"1." encoding="UTF-8"?>
import javafx.scene.layout.GridPane?>
<VBox xmlns:fx="http://javafx.com/fxml" alignment="CENTER" spacing="10">
<Label fx:id="monthLabel" text="2023³â 10¿ù" style="-fx-font-size: 24px;"/>
<GridPane fx:id="calendarGrid" hgap="10" vgap="10" >
<Label text="ÀÏ" />
<Label text="¿ù" />
<Label text="È" />
<Label text="¼ö" />
<Label text="¸ñ" />
<Label text="±Ý" />
<Label textÅä" />
GridPane>
<Button text="ÀÌÀü" onAction="#handlePreviousMonth" />
<Button text="´ÙÀ½" onAction="#handleNextMonth" />
VBox>
- ÄÁÆ®·Ñ·¯ Ŭ·¡½º »ý¼º:
CalendarController.java
¶ó´Â Ŭ·¡½º¸¦ »ý¼ºÇÏ¿© FXML°ú ¿¬°áÇÕ´Ï´Ù.
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
public class CalendarController {
@FXML
private Label monthLabel;
@FXML
private GridPane calendarGrid;
private LocalDate currentDate;
public void initialize() {
currentDate = LocalDate.now();
updateCalendar();
}
private void updateCalendar() {
calendarGrid.getChildren().clear();
YearMonth yearMonth = YearMonth.from(currentDate);
monthLabel.setText(yearMonth.format(DateTimeFormatter.ofPattern("yyyy³â M¿ù")));
for (int day = 1; day <= yearMonth.lengthOfMonth(); day++) {
LocalDate date = yearMonth.atDay(day);
Button dayButton = new Button(String.valueOf(day));
calendarGrid.add(dayButton, date.getDayOfWeek().getValue() % 7, (day + date.getDayOfWeek().getValue() - 1) / 7);
}
}
@FXML
private void handlePreviousMonth() {
currentDate = currentDate.minusMonths(1);
updateCalendar();
}
@FXML
private void handleNextMonth() {
currentDate = currentDate.plusMonths(1);
updateCalendar();
}
}
- ¸ÞÀÎ ¾ÖÇø®ÄÉÀÌ¼Ç Å¬·¡½º ÀÛ¼º: JavaFX ¾ÖÇø®ÄÉÀ̼ÇÀ» ½ÇÇàÇÒ ¸ÞÀΠŬ·¡½º¸¦ ÀÛ¼ºÇÕ´Ï´Ù.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CalendarApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Calendar.fxml"));
primaryStage.setTitle("¿ù ´Þ·Â");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
- ÇÁ·ÎÁ§Æ® ½ÇÇà: ¸ðµç ÆÄÀÏÀÌ ÁغñµÇ¸é,
CalendarApp
Ŭ·¡½º¸¦ ½ÇÇàÇÏ¿© ¿ù ´Þ·Â ¾ÖÇø®ÄÉÀ̼ÇÀ» º¼ ¼ö ÀÖ½À´Ï´Ù.
ÀÌ·¸°Ô Çϸé JavaFX¿Í FXMLÀ» »ç¿ëÇÏ¿© °£´ÜÇÑ ¿ù ´Þ·Â ºä¸¦ ¸¸µé ¼ö ÀÖ½À´Ï´Ù. ÇÊ¿äÇÑ °æ¿ì ½ºÅ¸ÀÏÀ̳ª ±â´ÉÀ» Ãß°¡ÇÏ¿© °³¼±ÇÒ ¼ö ÀÖ½À´Ï´Ù.