Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Swing
- Undo
- DBA
- 인스턴스
- 멀티쓰레드
- socket
- 카카오코딩테스트
- 깃허브
- 클라이언트
- 생성자
- 상속
- 친절한 SQL 튜닝
- Oracle
- 오라클
- springboot
- 백준
- 인덱스 튜닝
- 메소드
- SQL
- DB
- 컬렉션프레임워크
- SQLP
- db버퍼캐시
- 자바
- 친절한 sql튜닝
- 클래스
- java
- 서버
- 인덱스
- Spring
Archives
- Today
- Total
프리 정보 컨텐츠
자바 Swing Calendar 년도 달 버튼추가 본문
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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구현은 쉬웠으나,
Calendar의 기본적인 기능구현,
Calendar기능을 Swing으로 구현하는 데 있어서 이해가 부족함을 느꼈다.
이해할 때까지 반복학습 必
반응형