⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configline.java

📁 Java Swing 日期控件 增加时间选择功能
💻 JAVA
字号:
/*
 * ConfigLine.java
 *
 * Created on 2007年7月12日, 下午10:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eRedLab.datachooser;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
 
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.text.SimpleDateFormat;

class ConfigLine extends JPanel {
    
    //数据配置--------------------//
    private TablePanel tablePanel = null;
    
    private Calendar showMonth = null; //当前显示的月份
    private int startYear = 0; //【最小】显示年份
    private int lastYear = 0;  //【最大】显示年份
    
    private int nowYear = 0;   //当前年份
    private int nowMonth = 0;  //当前月份
    private int nowHour = 0;  //当前小时
     private int nowMinute=0;//当前分钟
     private int nowSecond=0;//当前秒数
    
    /**
     * 计时器,RoundBox快速翻动
     */
    Timer timer = new Timer(true);
    
    
    //界面组件------------------//
    private RoundBox yearBox = null;
    private RoundBox monthBox = null;
    private RoundBox hourBox=null;
    private RoundBox minuteBox=null;
    private RoundBox secondBox=null;

    
    private JLabel txtYear = new JLabel("年");
    private JLabel txtMonth = new JLabel("月");
    private JLabel txtHour = new JLabel("时");
    private JLabel txtMinute = new JLabel("分");
    private JLabel txtSecond = new JLabel("秒");

 
 
 
 
//------构造方法/函数-----------------------------------------------//
 
 
/**
 * 构造方法
 *
 * @param tablePanel TablePanel
 * @param showMonth java.util.Calendar
 * @param startYear int
 * @param lastYear int
 */
    ConfigLine(TablePanel tablePanel, Calendar showMonth, 
                                      int startYear, int lastYear) {
                                        
        this.tablePanel = tablePanel;
        
        this.showMonth = showMonth;
        this.startYear = startYear;
        this.lastYear = lastYear;
        
        nowYear = Integer.valueOf(new SimpleDateFormat("yyyy")
                                    .format(showMonth.getTime())).intValue();
        nowMonth = Integer.valueOf(new SimpleDateFormat("M")
                                    .format(showMonth.getTime())).intValue();
         nowHour = Integer.valueOf(new SimpleDateFormat("H")
                                    .format(showMonth.getTime())).intValue();
          nowMinute = Integer.valueOf(new SimpleDateFormat("mm")
                                    .format(showMonth.getTime())).intValue();
          nowSecond = Integer.valueOf(new SimpleDateFormat("ss")
                                    .format(showMonth.getTime())).intValue();
        yearBox = new RoundBox(nowYear, startYear, lastYear);
        monthBox = new RoundBox(nowMonth, 1, 12);
        hourBox=new RoundBox(nowHour,0,23);
        minuteBox=new RoundBox(nowMinute,0,59);
        secondBox=new RoundBox(nowSecond,0,59);
                                             
        makeFace();    //界面制作
        addListener(); //添加事件监听
    }
    
    
    
    
//------方法/函数---------------------------------------------------//
 
/**
 * 方法:界面制作
 */
    private void makeFace() {
        
        Font txtFont = new Font("宋体", Font.PLAIN, 12);
        
        //自身配置-----------------------//
        this.setBorder(null);
        this.setBackground(Pallet.configLineColor);
        this.setLayout(new FlowLayout(1, 1, 1));
        this.setPreferredSize(new Dimension(60, 19));
        
        //标签配置---------------//
        txtYear.setForeground(Pallet.cfgTextColor);
        txtYear.setPreferredSize(new Dimension(14, 14));
        txtYear.setFont(txtFont);
        
        txtMonth.setForeground(Pallet.cfgTextColor);
        txtMonth.setPreferredSize(new Dimension(14, 14));
        txtMonth.setFont(txtFont);

        txtHour.setForeground(Pallet.cfgTextColor);
        txtHour.setPreferredSize(new Dimension(14, 14));
        txtHour.setFont(txtFont);

        txtMinute.setForeground(Pallet.cfgTextColor);
        txtMinute.setPreferredSize(new Dimension(14, 14));
        txtMinute.setFont(txtFont);

        txtSecond.setForeground(Pallet.cfgTextColor);
        txtSecond.setPreferredSize(new Dimension(14, 14));
        txtSecond.setFont(txtFont);
        
        monthBox.setShowWidth(17);
        hourBox.setShowWidth(17);
        minuteBox.setShowWidth(17);
        secondBox.setShowWidth(17);

        
        //总体布局-----------------------//
       add(yearBox);
        add(txtYear);
        add(monthBox);
        add(txtMonth);
        add(hourBox);
        add(txtHour);
        add(minuteBox);
        add(txtMinute);
        add(secondBox);
        add(txtSecond);
    }
 
 
 
 
/**
 * 方法:添加事件监听
 */
    private void addListener() {
        
        //yearBox 事件配置-------------------------//
        
        yearBox.bt_UP.addMouseListener(new MouseAdapter() {
            
            //yearBox.bt_UP 按下
            public void mousePressed(MouseEvent e) {
                btPressed(yearBox, 1);
            }
            
            //yearBox.bt_UP 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(yearBox, 1);
                nowYear = yearBox.showNow;
                
                tablePanel.setMonth(nowYear, nowMonth);
            }
        });
        
        yearBox.bt_DOWN.addMouseListener(new MouseAdapter() {
            
            //yearBox.bt_DOWN 按下
            public void mousePressed(MouseEvent e) {
                btPressed(yearBox, 2);
            }
            
            //yearBox.bt_DOWN 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(yearBox, 2);
                nowYear = yearBox.showNow;
                
                tablePanel.setMonth(nowYear, nowMonth);
            }
        });
        
        
        
        //monthBox 事件配置------------------------//
        
        monthBox.bt_UP.addMouseListener(new MouseAdapter() {
            
            //monthBox.bt_UP 按下
            public void mousePressed(MouseEvent e) {
                btPressed(monthBox, 1);
            }
            
            //monthBox.bt_UP 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(monthBox, 1);
                nowMonth = monthBox.showNow;
                
                tablePanel.setMonth(nowYear, nowMonth);
            }
        });
        
        monthBox.bt_DOWN.addMouseListener(new MouseAdapter() {
            
            //monthBox.bt_DOWN 按下
            public void mousePressed(MouseEvent e) {
                btPressed(monthBox, 2);
            }
            
            //monthBox.bt_DOWN 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(monthBox, 2);
                nowMonth = monthBox.showNow;
                
                tablePanel.setMonth(nowYear, nowMonth);
            }
        });
          //hourBox 事件配置-------------------------//

       hourBox.bt_UP.addMouseListener(new MouseAdapter() {

            //yearBox.bt_UP 按下
            public void mousePressed(MouseEvent e) {
                btPressed(hourBox, 1);
            }

            //yearBox.bt_UP 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(hourBox, 1);
                nowHour = hourBox.showNow;

                 tablePanel.setTime(nowHour, nowMinute, nowSecond);
            }
        });

        hourBox.bt_DOWN.addMouseListener(new MouseAdapter() {

            //hourBox.bt_DOWN 按下
            public void mousePressed(MouseEvent e) {
                btPressed(hourBox,2);
            }

            //hourBox.bt_DOWN 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(hourBox, 2);
                nowHour = hourBox.showNow;

               tablePanel.setTime(nowHour, nowMinute, nowSecond);

            }
        });
         //minuteBox 事件配置-------------------------//

       minuteBox.bt_UP.addMouseListener(new MouseAdapter() {

            //minuteBox.bt_UP 按下
            public void mousePressed(MouseEvent e) {
                btPressed(minuteBox, 1);
            }

            //minuteBox.bt_UP 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(minuteBox, 1);
                nowMinute = minuteBox.showNow;

               tablePanel.setTime(nowHour, nowMinute, nowSecond);
            }
        });

        minuteBox.bt_DOWN.addMouseListener(new MouseAdapter() {

            //minuteBox.bt_DOWN 按下
            public void mousePressed(MouseEvent e) {
                btPressed(minuteBox, 2);
            }

            //minuteBox.bt_DOWN 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(minuteBox, 2);
                nowMinute = minuteBox.showNow;

               tablePanel.setTime(nowHour, nowMinute, nowSecond);

            }
        });
        //secondBox 事件配置-------------------------//

       secondBox.bt_UP.addMouseListener(new MouseAdapter() {

            //secondBox.bt_UP 按下
            public void mousePressed(MouseEvent e) {
                btPressed(secondBox, 1);
            }

            //yearBox.bt_UP 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(secondBox, 1);
                nowSecond = secondBox.showNow;

                tablePanel.setTime(nowHour, nowMinute, nowSecond);
            }
        });

        secondBox.bt_DOWN.addMouseListener(new MouseAdapter() {

            //secondBox.bt_DOWN 按下
            public void mousePressed(MouseEvent e) {
                btPressed(secondBox, 2);
            }

            //hourBox.bt_DOWN 弹起
            public void mouseReleased(MouseEvent e) {
                btReleased(secondBox, 2);
                nowSecond = secondBox.showNow;

               tablePanel.setTime(nowHour, nowMinute, nowSecond);

            }
        });
    }
    
    
    
    
/**
 * RoundBox 统一按钮按下事务
 *
 * @param box RoundBox
 * @param theBT int
 */
    private void btPressed(RoundBox box, int theBT) {
        final RoundBox theBox = box;
        
        if(theBT == 1) {  //"+"按钮
            timer = new Timer(true);
            timer.schedule(new TimerTask() {
                public void run() {
                    if(theBox.showNow < theBox.showMax) {
                        theBox.showing.setText("" + (theBox.showNow+1));
                        
                        theBox.showNow++;
                    }
                }
            }, 500, 100);
        }
        else if(theBT == 2) {  //"-"按钮
            timer = new Timer(true);
            timer.schedule(new TimerTask() {
                public void run() {
                    if(theBox.showNow > theBox.showMin) {
                        theBox.showing.setText("" + (theBox.showNow-1));
                        
                        theBox.showNow--;
                    }
                }
            }, 500, 100);
        }
    }
 
 
 
 
/**
 * RoundBox 统一按钮弹起事务
 *
 * @param box RoundBox
 * @param theBT int
 */
    private void btReleased(RoundBox box, int theBT) {
        
        final RoundBox theBox = box;
        
        timer.cancel();
        
        if(theBT == 1) {  //"+"按钮
            if(theBox.showNow < theBox.showMax) {
                theBox.showing.setText("" + (theBox.showNow+1));
                        
                theBox.showNow++;
            }
        }
        
        else if(theBT == 2) {  //"-"按钮
            if(theBox.showNow > theBox.showMin) {
                theBox.showing.setText("" + (theBox.showNow-1));
                        
                theBox.showNow--;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -