CalendarPanel.java
上传用户:njlgjx
上传日期:2022-08-07
资源大小:9105k
文件大小:4k
源码类别:

图形图象

开发平台:

Java

  1. /*
  2.  * CalendarPanel.java
  3.  *
  4.  * Created on 2008年6月28日, 下午2:48
  5.  */
  6. package com.mwq.map.calendar;
  7. import java.awt.Dimension;
  8. import java.awt.Point;
  9. /**
  10.  *
  11.  * @author  Administrator
  12.  */
  13. public class CalendarPanel extends javax.swing.JPanel {
  14.     private CalendarDialog dialog;
  15.     /** Creates new form CalendarPanel */
  16.     public CalendarPanel() {
  17.         initComponents();
  18.     }
  19.     /** This method is called from within the constructor to
  20.      * initialize the form.
  21.      * WARNING: Do NOT modify this code. The content of this method is
  22.      * always regenerated by the Form Editor.
  23.      */
  24.     @SuppressWarnings("unchecked")
  25.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  26.     private void initComponents() {
  27.         textField = new javax.swing.JTextField();
  28.         button = new javax.swing.JButton();
  29.         setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
  30.         textField.setColumns(9);
  31.         textField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  32.         textField.addKeyListener(new java.awt.event.KeyAdapter() {
  33.             public void keyPressed(java.awt.event.KeyEvent evt) {
  34.                 textFieldKeyPressed(evt);
  35.             }
  36.         });
  37.         add(textField);
  38.         button.setText("▼");
  39.         button.setMargin(new java.awt.Insets(0, 6, 0, 6));
  40.         button.addActionListener(new java.awt.event.ActionListener() {
  41.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  42.                 buttonActionPerformed(evt);
  43.             }
  44.         });
  45.         add(button);
  46.     }// </editor-fold>                        
  47.     @Override
  48.     public void setEnabled(boolean enabled) {
  49.         super.setEnabled(enabled);
  50.         textField.setEnabled(enabled);
  51.         button.setEnabled(enabled);
  52.     }
  53. private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
  54. // TODO add your handling code here:    
  55.     Dimension textFieldPreferredSize = getTextField().getPreferredSize();// 获得文本框的首选大小
  56.     Point locationOnScreen = getTextField().getLocationOnScreen();// 获得文本框在屏幕上的起始绘制点
  57.     int x = (int) locationOnScreen.getX();// 定义对话框在X轴的起始绘制点
  58.     int y = (int) (locationOnScreen.getY() 
  59.             + textFieldPreferredSize.getHeight());// 定义对话框在Y轴的起始绘制点
  60.     dialog = new CalendarDialog(null, true);// 创建对话框对象
  61.     Dimension dialogPreferredSize = dialog.getPreferredSize();// 获得对话框的首选大小
  62.     dialog.setBounds(x, y, (int) dialogPreferredSize.getWidth(), (int) dialogPreferredSize.getHeight());
  63.     new Thread() {// 创建并开启一个线程
  64.         @Override
  65.         public void run() {// 重构该方法
  66.             while (true) {
  67.                 if (MTableCell.getSelectedDay() != null) {// 如果用户选择了日期
  68.                     getTextField().setText(CalendarDialog.getYear() 
  69.                             + "-" + CalendarDialog.getMonth() 
  70.                             + "-" + MTableCell.getSelectedDay());
  71.                     dialog.dispose();// 销毁日期选择框
  72.                     MTableCell.setSelectedDay(null);
  73.                     break;// 跳出循环
  74.                 }
  75.                 try {
  76.                     Thread.sleep(1000);// 休眠1秒
  77.                 } catch (InterruptedException e) {
  78.                     e.printStackTrace();
  79.                 }
  80.             }
  81.         }
  82.     }.start();
  83.     dialog.setVisible(true);// 设置日期选择框可见
  84. }                                      
  85. private void textFieldKeyPressed(java.awt.event.KeyEvent evt) {                                     
  86. // TODO add your handling code here:
  87.     evt.consume();
  88. }                                    
  89.     // Variables declaration - do not modify                     
  90.     private javax.swing.JButton button;
  91.     private javax.swing.JTextField textField;
  92.     // End of variables declaration                   
  93.     public javax.swing.JTextField getTextField() {
  94.         return textField;
  95.     }
  96. }