swing 날짜 구성 요소

14317 단어 swing

/**
 * chega
 * 2011-9-21  11:06:41
 */
public class CalendarUI extends MouseAdapter implements ActionListener,ItemListener{
	private JDialog dialog ;
	private JComboBox jcb_year;
	private JComboBox jcb_month;
	private JComboBox jcb_hour;
	private JComboBox jcb_min;
	private JComboBox jcb_second;
	private Vector<Integer> vec_year;
	private Vector<Integer> vec_month;
	private Vector<Integer> vec_hour;
	private Vector<Integer> vec_min;
	private Vector<Integer> vec_second;
	private Vector<String> displayName;
	private Calendar today;
	private Calendar current;
	private JButton jb_today;
	private JButton jb_clear;
	private JButton jb_ok;
	private JPanel panel_date ;
	private JTextField jtf_datetime;
	private JButton jb_ellipsis;
	private SimpleDateFormat sdf;
	private Color bgColor;
	private Color selectedColor;
	private JPanel calenarPanel;
	
	public static void main(String...args){
		JFrame frame = new JFrame("CalendarUI Test");
		frame.getContentPane().setLayout(null);
		CalendarUI ui = new CalendarUI();
		ui.setSdf("yyyy-MM-dd HH:mm");
		JPanel panel = ui.getInstance();
		panel.setBounds(new Rectangle(10,20,150,25));
		frame.getContentPane().add(panel);
		frame.setVisible(true);
		frame.setBounds(new Rectangle(215,20,300,200));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public CalendarUI(){
		initData();
	}
	/**
	 * 150*25
	 * @return  JPanel
	 */
	public JPanel getInstance(){
		calenarPanel = new JPanel();
		calenarPanel.setLayout(null);
		this.jb_ellipsis = new JButton("...");
		this.jb_ellipsis.setHorizontalTextPosition(SwingConstants.LEADING);
		this.jb_ellipsis.addActionListener(this);
		this.jtf_datetime = new JTextField(20);
		this.jtf_datetime.setEditable(false);
		this.jtf_datetime.setBounds(new Rectangle(0,0,128,25));
		calenarPanel.add(this.jtf_datetime);
		this.jb_ellipsis.setBounds(new Rectangle(130,0,20,25));
		calenarPanel.add(this.jb_ellipsis);
		return calenarPanel;
	}
	private void initCalendar(){
		this.panel_date = new JPanel();
		this.dialog = new JDialog(this.getParentFrame(this.jb_ellipsis),true);
		dialog.setTitle("\u65E5\u671F\u9009\u62E9\u5668");
		Container container = dialog.getContentPane();
		container.setLayout(null);
		JPanel panel_time = this.initTimeComp();
		panel_time.setBounds(new Rectangle(0, 0, 286, 64));
		container.add(panel_time);
		JPanel panel_date = this.initDateComp();
		panel_date.setBounds(new Rectangle(0, 65, 280, 152));
		container.add(panel_date);
		JPanel panel_btn = this.initBtnComp();
		panel_btn.setBounds(new Rectangle(0, 227, 286, 30));
		container.add(panel_btn);
		dialog.setSize(new Dimension(299, 288));
		dialog.setVisible(false);
		dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
	}
	private void restoreDateTime(){
		this.jcb_year.setSelectedItem(this.current.get(Calendar.YEAR));
		this.jcb_month.setSelectedItem(this.current.get(Calendar.MONTH)+1);
		this.jcb_hour.setSelectedItem(this.current.get(Calendar.HOUR_OF_DAY));
		this.jcb_min.setSelectedItem(this.current.get(Calendar.MINUTE));
		this.jcb_second.setSelectedItem(this.current.get(Calendar.SECOND));
		Component[] comps = this.panel_date.getComponents();
		for(int i=0;i<comps.length;i++){
			if(comps[i] instanceof JButton && (this.current.get(Calendar.DATE)+"").equals(((JButton)comps[i]).getText())){
				((JButton)comps[i]).setBackground(this.selectedColor);
			}
		}
	}
	private JPanel initTimeComp(){
		JPanel panel_time = new JPanel();
		this.jcb_year = new JComboBox(this.vec_year);
		this.jcb_year.addItemListener(this);
		JLabel label_year = new JLabel(" ");
		this.jcb_month = new JComboBox(this.vec_month);
		this.jcb_month.addItemListener(this);
		JLabel label_month = new JLabel(" ");
		this.jcb_hour = new JComboBox(this.vec_hour);
		this.jcb_hour.addItemListener(this);
		JLabel label_hour = new JLabel(" ");
		this.jcb_min = new JComboBox(this.vec_min);
		this.jcb_min.addItemListener(this);
		JLabel label_min = new JLabel(" ");
		this.jcb_second = new JComboBox(this.vec_second);
		this.jcb_second.addItemListener(this);
		JLabel label_second = new JLabel(" ");
		panel_time.setLayout(null);
		this.jcb_year.setBounds(new Rectangle(44, 5, 60, 21));
		panel_time.add(this.jcb_year);
		label_year.setBounds(new Rectangle(114, 5, 20, 21));
		panel_time.add(label_year);
		this.jcb_month.setBounds(new Rectangle(144, 5, 60, 21));
		panel_time.add(this.jcb_month);
		label_month.setBounds(new Rectangle(214, 5, 20, 21));
		panel_time.add(label_month);
		this.jcb_hour.setBounds(new Rectangle(20, 36, 60, 21));
		panel_time.add(this.jcb_hour);
		label_hour.setBounds(new Rectangle(90, 36, 20, 21));
		panel_time.add(label_hour);
		this.jcb_min.setBounds(new Rectangle(114, 36, 60, 21));
		panel_time.add(this.jcb_min);
		label_min.setBounds(new Rectangle(184, 36, 20, 21));
		panel_time.add(label_min);
		this.jcb_second.setBounds(new Rectangle(201, 36, 60, 21));
		panel_time.add(this.jcb_second);
		label_second.setBounds(new Rectangle(271, 36, 20, 21));
		panel_time.add(label_second);
		return panel_time;
	}
	private JPanel initDateComp(){
		this.panel_date.removeAll();
		Calendar firstDayOfThisMonth = (Calendar) this.current.clone();
		firstDayOfThisMonth.set(Calendar.DATE, 1);
		//System.out.println(firstDayOfThisMonth.get(Calendar.DAY_OF_WEEK));
		Calendar lastDayOfThisMonth = (Calendar) this.current.clone();
		lastDayOfThisMonth.set(Calendar.DATE, lastDayOfThisMonth.getActualMaximum(Calendar.DATE));
		int firstLineCount = this.displayName.size()-firstDayOfThisMonth.get(Calendar.DAY_OF_WEEK)+1;
		int lastLineCount = lastDayOfThisMonth.get(Calendar.DAY_OF_WEEK);
		int rowCount = (this.current.getActualMaximum(Calendar.DATE)-firstLineCount-lastLineCount)/7+3;
		panel_date.setLayout(new GridLayout(rowCount,this.displayName.size(),5,5));
//		System.out.println("     :"+firstLineCount+",      :"+lastLineCount);
//		System.out.println("  :"+rowCount);
		//     
		for(int i=0;i<this.displayName.size();i++){
			JLabel label = new JLabel(this.displayName.get(i));
			label.setHorizontalAlignment(JLabel.CENTER);
			if(i==0 || i==this.displayName.size()-1){
				label.setForeground(Color.red);
			}
			panel_date.add(label);
		}
		for(int i=1;i<=(rowCount-1)*this.displayName.size();i++){
			if(i<(this.displayName.size()-firstLineCount+1) || i>(this.current.getActualMaximum(Calendar.DATE)+this.displayName.size()-firstLineCount)){
				panel_date.add(new JLabel());
			}else{
				JButton btn = new JButton(""+(i-(this.displayName.size()-firstLineCount)));
				btn.setBorder(null);
				btn.addMouseListener(this);
				btn.setBackground(this.bgColor);
				if(i%this.displayName.size()==0 || i%this.displayName.size()==1){
					btn.setForeground(Color.red);
				}
				panel_date.add(btn);
			}
		}
		//this.panel_date.repaint();
		this.panel_date.validate();
		return panel_date;
	}
	
	private JPanel initBtnComp(){
		JPanel panel_btn = new JPanel();
		this.jb_clear = new JButton("  ");
		//this.jb_clear.setMargin(new Insets(5,5,5,5));
		this.jb_clear.setBounds(26, 0, 60, 25);
		this.jb_clear.addActionListener(this);
		this.jb_today = new JButton("  ");
		jb_today.setBounds(120, 0, 60, 25);
		this.jb_today.addActionListener(this);
		this.jb_ok = new JButton("  ");
		jb_ok.setBounds(203, 0, 60, 25);
		this.jb_ok.addActionListener(this);
		panel_btn.setLayout(null);
		panel_btn.add(this.jb_clear);
		panel_btn.add(this.jb_today);
		panel_btn.add(this.jb_ok);
		return panel_btn;
	}
	private void initData(){
		this.bgColor = new Color(240,240,240);
		this.selectedColor = new Color(200,250,200);
		this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.vec_year = new Vector<Integer>();
		for(int i=1972;i<2099;i++){
			this.vec_year.add(i);
		}
		this.vec_month = new Vector<Integer>();
		for(int i=1;i<13;i++){
			this.vec_month.add(i);
		}
		this.vec_hour = new Vector<Integer>();
		for(int i=0;i<24;i++){
			this.vec_hour.add(i);
		}
		this.vec_min = new Vector<Integer>();
		for(int i=0;i<60;i++){
			this.vec_min.add(i);
		}
		this.vec_second = new Vector<Integer>();
		for(int i=0;i<60;i++){
			this.vec_second.add(i);
		}
		this.displayName = new Vector<String>();
		this.displayName.add(" ");
		this.displayName.add(" ");
		this.displayName.add(" ");
		this.displayName.add(" ");
		this.displayName.add(" ");
		this.displayName.add(" ");
		this.displayName.add(" ");
		
		this.today = Calendar.getInstance();
		this.current = Calendar.getInstance();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.jb_ellipsis){
			this.today = Calendar.getInstance();
			this.initCalendar();
			if(!this.jtf_datetime.getText().equals("")){
				try {
					this.current.setTime(this.sdf.parse(this.jtf_datetime.getText()));
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
			}
			this.restoreDateTime();
			this.dialog.setLocation(new Point(this.jb_ellipsis.getLocationOnScreen().x-100,this.jb_ellipsis.getLocationOnScreen().y+20));
			this.dialog.setVisible(true);
		}
		if(e.getSource()==this.jb_clear){
			this.jtf_datetime.setText("");
			this.dialog.setVisible(false);
		}
		if(e.getSource()==this.jb_today){
			System.out.println(this.sdf.format(this.today.getTime()));
			this.jtf_datetime.setText(this.sdf.format(this.today.getTime()));
			this.dialog.setVisible(false);
		}
		if(e.getSource()==this.jb_ok){
			this.jtf_datetime.setText(this.sdf.format(this.current.getTime()));
			this.dialog.setVisible(false);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
	 *               
	 */
	@Override
	public void mouseClicked(MouseEvent e){
		int date = Integer.parseInt(((JButton)e.getSource()).getText());
		this.current.set(Calendar.DATE, date);
//		System.out.println(sdf.format(this.current.getTime()));
		if(e.getClickCount()==2){
			this.jtf_datetime.setText(this.sdf.format(this.current.getTime()));
			this.dialog.setVisible(false);
		}
		Component[] comps = this.panel_date.getComponents();
		for(int i=0;i<comps.length ;i++){
			if(comps[i] instanceof JButton){
				comps[i].setBackground(this.bgColor);
			}
		}
		if(e.getSource() instanceof JButton){
			((JButton)e.getSource()).setBackground(this.selectedColor);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getStateChange()==ItemEvent.SELECTED){
			if(e.getSource()==this.jcb_year){
				this.current.set(Calendar.YEAR, (Integer)this.jcb_year.getSelectedItem());
				this.initDateComp();
			}
			if(e.getSource()==this.jcb_month){
				this.current.set(Calendar.MONTH, (Integer)this.jcb_month.getSelectedItem()-1);
				this.initDateComp();
			}
			if(e.getSource()==this.jcb_hour){
				this.current.set(Calendar.HOUR_OF_DAY,(Integer)this.jcb_hour.getSelectedItem());
			}
			if(e.getSource()==this.jcb_min){
				this.current.set(Calendar.MINUTE, (Integer)this.jcb_min.getSelectedItem());
			}
			if(e.getSource()==this.jcb_second){
				this.current.set(Calendar.SECOND, (Integer)this.jcb_second.getSelectedItem());
			}
		}
	}
	
	private JFrame getParentFrame(Component comp){
		while(true){
//			System.out.println(comp);
			if(comp instanceof JFrame){
				//System.out.println("instance");
				return (JFrame)comp;
			}
			if(comp==null){
				return (JFrame)null;
			}
			comp = comp.getParent();
			
		}
	}
	
	/*
	 *       
	 */
	public String getDateTime(){
		return this.jtf_datetime.getText();
	}
	/*
	 *       
	 */
	public void setDateTime(String dateTime){
		try{
			this.sdf.format(dateTime);
		}catch(IllegalArgumentException e){
			IllegalArgumentException ee = new IllegalArgumentException("                yyyy-MM-dd HH:mi:ss  ,          ",e.getCause());
			throw ee;
		}
		this.jtf_datetime.setText(dateTime);
	}
	/*
	 *                 yyyy-MM-dd HH:mi:ss
	 * if the defined format is uncorrect, an IllegalArgumentException will be thrown
	 */
	public CalendarUI setSdf(String format){
		this.sdf = new SimpleDateFormat(format);
		return this;
	}
	/*
	 *                 yyyy-MM-dd HH:mi:ss
	 * if the defined format is uncorrect, an IllegalArgumentException will be thrown
	 */
	public CalendarUI setSdf(SimpleDateFormat format){
		this.sdf = format;
		return this;
	}
}


좋은 웹페이지 즐겨찾기