2010. 4. 28. 09:59 Program.../Java Language
자바를 이용한 Swing 달력만들기
package cal;
import java.io.*;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingCalendar implements ActionListener
{
Calendar cal;
int int_Year, int_Month;
String year, month;
StringBuffer sb_Print;
JLabel lb_title, lb_YearSpace, lb_MonthSpace;
JTextField textField_Year, textField_Month;
JTextPane textPane;
JPanel panel_North, panel_NorthFloor;
JButton bt_Input;
public SwingCalendar()
{
cal = Calendar.getInstance();
setCalFrame();
}
public void setCalendar()
{
sb_Print = new StringBuffer();
sb_Print.append(" 일 월 화 수 목 금 토\n");
cal.set(int_Year, int_Month-1, 1);
int cal_Field = cal.get(7);
for(int j = 1; j < cal_Field; j++)
{
sb_Print.append(" ");
}
int cal_Gam = cal.getActualMaximum(5);
int comp = 1;
for(int k = cal_Field; comp <= cal_Gam; k++)
{
if (comp >= cal_Gam)
{
if (comp < 10)
{
sb_Print.append(" ");
}else sb_Print.append(" ");
sb_Print.append(comp);
textPane.setText(sb_Print.toString());
}else
{
if (comp < 10)
{
sb_Print.append(" ");
}else sb_Print.append(" ");
sb_Print.append(comp);
textPane.setText(sb_Print.toString());
}
if(k % 7 == 0) sb_Print.append("\n");
comp++;
}
}
public void setCalFrame()
{
lb_title = new JLabel("연도와 달을 입력하세요 -> ex) xxxx xx", SwingConstants.CENTER);
lb_YearSpace = new JLabel("년 ");
lb_MonthSpace = new JLabel("월 ");
textField_Year = new JTextField(5);
textField_Month = new JTextField(3);
textPane = new JTextPane();
panel_North = new JPanel(new GridLayout(2,1));
panel_NorthFloor = new JPanel();
bt_Input = new JButton("입력");
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(" Swing Calendar");
frame.add(panel_North, "North");
panel_North.add(lb_title);
panel_North.add(panel_NorthFloor);
panel_NorthFloor.add(textField_Year);
panel_NorthFloor.add(lb_YearSpace);
panel_NorthFloor.add(textField_Month);
panel_NorthFloor.add(lb_MonthSpace);
panel_NorthFloor.add(bt_Input);
frame.add(textPane, "Center");
textPane.setEditable(false);
textPane.setFont(new Font("font", Font.BOLD, 16));
textPane.setForeground(Color.blue);
bt_Input.addActionListener(this);
frame.setSize(260,280);
frame.setLocation(300,300);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (obj == bt_Input)
{
year = textField_Year.getText();
month = textField_Month.getText();
try
{
int_Year = Integer.parseInt(year);
int_Month = Integer.parseInt(month);
if(year.length() != 4 || month.length() != 2)
{
textPane.setText("년도 및 월 입력이 잘못되었습니다.");
return;
}else if ((int_Month == 0) || (12 < int_Month))
{
textPane.setText("년도 및 월 입력이 잘못되었습니다.");
return;
}else setCalendar();
}
catch (NumberFormatException ne)
{
textPane.setText("입력된 년도와 월이 숫자형태가 아닙니다.");
return;
}
}
}
public static void main(String[] args)
{
new SwingCalendar();
}
}
'Program... > Java Language' 카테고리의 다른 글
자바를 배워보자 Java Begin (4) (0) | 2010.04.28 |
---|---|
자바를 배워보자 Java Begin (3) (0) | 2010.04.28 |
자바를 배워보자 Java Begin (2) (0) | 2010.04.28 |
자바를 배워보자 Java Begin (1) (0) | 2010.04.28 |
Java 1~10 홀수별, 짝수별 합 구하기 (0) | 2009.10.28 |