프리 정보 컨텐츠

자바 Swing Calendar 년도 달 버튼추가 본문

JAVA/Swing

자바 Swing Calendar 년도 달 버튼추가

쏜스 2021. 1. 27. 02:07
반응형
package calendar;
import java.util.Calendar;
import javax.swing.JButton;
public class CalendarFunction {
JButton[] buttons;
Calendar cal = Calendar.getInstance();
int year, month;
public CalendarFunction() {
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH) + 1;
}
public void setButtons(JButton[] buttons) {
this.buttons = buttons;
}
// Label -> 0000년 00월 문자열 리턴
public String getCalText() {
return year + "년" + month + "월";
}
// 버튼 날짜 출력
public void calSet() {
// calendar 객체 날짜 1일 설정
cal.set(year, month-1, 1);
// 그 달의 1일 요일 수
int firstDay = cal.get(Calendar.DAY_OF_WEEK);
// 요일 수 1일 시작, 배열 0부터 시작
firstDay--;
for(int i = 1; i <= cal.getActualMaximum(cal.DATE); i++) {
// buttons[0] ~ [6] : 일 ~ 토
// buttons[7] ~ : 날짜 출력
buttons[6 + firstDay + i].setText(String.valueOf(i));
}
}
// 달력 새로운 년월 출력
public void allInit(int gap) {
// 버튼 날짜 지우기
for(int i =7; i < buttons.length; i++) {
buttons[i].setText("");
}
month += gap;
if(month <= 0) {
year--;
month = 12;
} else if(month >= 13) {
year++;
month = 1;
}
calSet();
}
}
package calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SwingCalendar {
public static void main(String[] args) {
new Calendarmain();
}
}
class Calendarmain extends JFrame implements ActionListener{
Container container = getContentPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton buttonBefore = new JButton("Before");
JButton buttonAfter = new JButton("After");
JLabel label = new JLabel("00년 0월");
JButton[] buttons = new JButton[49];
String[] dayNames = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
CalendarFunction cF = new CalendarFunction();
public Calendarmain() {
setTitle("만년 달력");
setSize(550, 400);
setLocation(400, 400);
init();
start();
setVisible(true);
}
private void init() {
container.setLayout(new BorderLayout());
container.add("North", panel1);
container.add("Center", panel2);
panel1.setLayout(new FlowLayout());
panel1.add(buttonBefore);
panel1.add(label);
panel1.add(buttonAfter);
Font font = new Font("SansSerif", Font.BOLD, 20);
buttonAfter.setFont(font);
buttonBefore.setFont(font);
label.setFont(font);
label.setText(cF.getCalText());
panel2.setLayout(new GridLayout(7, 7, 5, 5));
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton();
panel2.add(buttons[i]);
buttons[i].setFont(new Font("SansSerif", Font.BOLD, 24));
if(i < 7) buttons[i].setText(dayNames[i]);
if(i%7 == 0) buttons[i].setForeground(Color.RED);
if(i%7 == 6) buttons[i].setForeground(Color.BLUE);
}
cF.setButtons(buttons);
cF.calSet();
}
private void start() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
buttonAfter.addActionListener(this);
buttonBefore.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int gap = 0;
if(e.getSource() == buttonAfter) { // 1달 후
gap = 1;
} else if(e.getSource() == buttonBefore ) { // 1달 전
gap = -1;
}
cF.allInit(gap);
label.setText(cF.getCalText()); // 년월 글자 갱신
}
}

 

Swing Calendar

※ 참고사항

Swing을 사용한 Calendar구현은 쉬웠으나,
Calendar의 기본적인 기능구현,
Calendar기능을 Swing으로 구현하는 데 있어서 이해가 부족함을 느꼈다.

이해할 때까지 반복학습 必

 

반응형
Comments