`
stjauns
  • 浏览: 90486 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

WeekselectPanel选择一年中的某周

    博客分类:
  • JAVA
阅读更多
系统要增加工作计划功能,让每个营业部每周末分派下周本部门每个人的工作量。
用到了这个周次选择Panel。网上随便搜搜没有,就自己弄了个。
界面如图:


主功能代码
package transms.app._0all.util.weekselect;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Insets;

import javax.swing.JButton;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JToggleButton;

import org.apache.log4j.Logger;

import transms.app._0all.util.VOUtil;
import transms.app._0all.util.providers.Colors;
import util.Info;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JToolBar;
import javax.swing.JComboBox;

public class WeekselectPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    /**
     * This is the default constructor
     */
    public WeekselectPanel() {
        super();
        initialize();
        selectedyear = Integer.valueOf(VOUtil.now().substring(0, 4));
        getComboYear().setSelectedItem(String.valueOf(selectedyear));
        try {
            addWeeks(selectedyear);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setToday();
    }

    private int selectedyear = -1;

    public String getYear() {
        return String.valueOf(selectedyear);
    }

    private int selectedweek = -1;

    public int getWeek() {
        return selectedweek;
    }

    /**
     * 选中今天
     */
    private void setToday() {
        try {
            int currentweek = du.getWeekNumOfYearDay(VOUtil.now());
            mapButton.get(currentweek).setSelected(true);
            action(String.valueOf(currentweek));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    Logger logger = Logger.getLogger(WeekselectPanel.class); // @jve:decl-index=0:

    Map<Integer, JToggleButton> mapButton = new HashMap<Integer, JToggleButton>(); // @jve:decl-index=0:

    Map<Integer, Color> mapMonthcolor = new HashMap<Integer, Color>(); // @jve:decl-index=0:

    Map<Integer, Color> mapSeasoncolor = new HashMap<Integer, Color>(); // @jve:decl-index=0:

    private JPanel panelWeek = null;

    private JToolBar jToolBar = null;

    private JComboBox comboYear = null;

    private JButton jButtonOK = null;

    {
        mapMonthcolor.put(1, Colors.Chartreuse3);
        mapMonthcolor.put(2, Colors.DarkOliveGreen2);
        mapMonthcolor.put(3, Colors.DarkOrange1);
        mapMonthcolor.put(4, Colors.Firebrick1);
        mapMonthcolor.put(5, Colors.Ivory2);
        mapMonthcolor.put(6, Colors.Khaki1);
        mapMonthcolor.put(7, Colors.LightSalmon);
        mapMonthcolor.put(8, Colors.OrangeRed);
        mapMonthcolor.put(9, Colors.PaleGreen);
        mapMonthcolor.put(10, Colors.PaleTurquoise1);
        mapMonthcolor.put(11, Colors.Purple3);
        mapMonthcolor.put(12, Color.CYAN);

        mapSeasoncolor.put(1, Colors.Ivory2);
        mapSeasoncolor.put(2, Colors.Ivory2);
        mapSeasoncolor.put(3, Colors.Ivory2);
        mapSeasoncolor.put(4, Colors.Chartreuse3);
        mapSeasoncolor.put(5, Colors.Chartreuse3);
        mapSeasoncolor.put(6, Colors.Chartreuse3);
        mapSeasoncolor.put(7, Colors.Purple3);
        mapSeasoncolor.put(8, Colors.Purple3);
        mapSeasoncolor.put(9, Colors.Purple3);
        mapSeasoncolor.put(10, Colors.DarkOrange1);
        mapSeasoncolor.put(11, Colors.DarkOrange1);
        mapSeasoncolor.put(12, Colors.DarkOrange1);
    }

    private DateUtil du = new DateUtil(); // @jve:decl-index=0:

    private void addWeeks(int year) throws ParseException {
        ButtonGroup bg = new ButtonGroup();
        getPanelWeek().removeAll();
        mapButton.clear();
        for (int i = 1; i < 55; i++) {
            String sun = du.getYearWeekFirstDay(year, i);
            String sat = du.getYearWeekEndDay(year, i);
            if (!sat.startsWith(String.valueOf(year))) {
                continue;
            }
            if (mapButton.get(i) == null) {
                JToggleButton button = new JToggleButton(String.valueOf(i));
                mapButton.put(i, button);
                getPanelWeek().add(mapButton.get(i));
                bg.add(mapButton.get(i));
                mapButton.get(i).addActionListener(getButtonAction());
            }
            mapButton.get(i).setMargin(new Insets(0, 0, 0, 0));
            mapButton.get(i).setToolTipText(
                    i + " " + du.getMonth(sun) + " " + sun + " - " + sat);
            mapButton.get(i).setForeground(mapMonthcolor.get(du.getMonth(sat)));
            mapButton.get(i).setBackground(mapSeasoncolor.get(du.getMonth(sat)));
            mapButton.get(i).setActionCommand(String.valueOf(i));

        }
    }

    private ActionListener getButtonAction() {
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                action(e.getActionCommand());
            }
        };
    }

    private void action(String actionCommand) {
        selectedweek = Integer.valueOf(actionCommand);
        try {
            getJButtonOK().setText(
                    actionCommand
                            + "周 "
                            + du.getYearWeekFirstDay(Integer.valueOf(getComboYear()
                                    .getSelectedItem().toString()), selectedweek)
                            + Info.labelInfo("to")
                            + du.getYearWeekEndDay(Integer.valueOf(getComboYear()
                                    .getSelectedItem().toString()), selectedweek));
        } catch (NumberFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setLayout(new BorderLayout());
        this.setSize(312, 102);
        this.add(getPanelWeek(), BorderLayout.CENTER);
        this.add(getJToolBar(), BorderLayout.NORTH);
        this.setBorder(BorderFactory.createEtchedBorder());
    }

    /**
     * This method initializes panelWeek
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getPanelWeek() {
        if (panelWeek == null) {
            GridLayout gridLayout1 = new GridLayout(0, 13);
            panelWeek = new JPanel();
            panelWeek.setLayout(gridLayout1);
        }
        return panelWeek;
    }

    /**
     * This method initializes jToolBar
     * 
     * @return javax.swing.JToolBar
     */
    private JToolBar getJToolBar() {
        if (jToolBar == null) {
            jToolBar = new JToolBar();
            jToolBar.add(getComboYear());
            jToolBar.add(getJButtonOK());
        }
        return jToolBar;
    }

    /**
     * This method initializes comboYear
     * 
     * @return javax.swing.JComboBox
     */
    private JComboBox getComboYear() {
        if (comboYear == null) {
            String[] years = { "2009", "2010", "2011", "2012", "2013", "2014", "2015",
                    "2016", "2017", "2018", "2019", "2020" };
            comboYear = new JComboBox(years);
            comboYear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    selectedyear = Integer
                            .valueOf(comboYear.getSelectedItem().toString());
                    try {
                        addWeeks(selectedyear);
                    } catch (NumberFormatException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (ParseException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            });
        }
        return comboYear;
    }

    /**
     * This method initializes jButtonOK
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButtonOK() {
        if (jButtonOK == null) {
            jButtonOK = new JButton();
            jButtonOK.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    logger.info(selectedyear + " " + selectedweek);
                }
            });
        }
        return jButtonOK;
    }

} // @jve:decl-index=0:visual-constraint="10,9"


日期处理工具,网上随便搜的:
package transms.app._0all.util.weekselect;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * 说明: 取日期时间工具
 */
public class DateUtil {
    /**
     * @see 取得当前日期(格式为:yyyy-MM-dd)
     * @return String
     */
    public String GetDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sDate = sdf.format(new Date());
        return sDate;
    }

    /**
     * @see 取得当前时间(格式为:yyy-MM-dd HH:mm:ss)
     * @return String
     */
    public static String GetDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(new Date());
        return sDate;
    }

    /**
     * @see 按指定格式取得当前时间()
     * @return String
     */
    public String GetTimeFormat(String strFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        String sDate = sdf.format(new Date());
        return sDate;
    }

    /**
     * @see 取得指定时间的给定格式()
     * @return String
     * @throws ParseException
     */
    public String SetDateFormat(String myDate, String strFormat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        String sDate = sdf.format(sdf.parse(myDate));
        return sDate;
    }

    public String FormatDateTime(String strDateTime, String strFormat) {
        String sDateTime = strDateTime;
        try {
            Calendar Cal = parseDateTime(strDateTime);
            SimpleDateFormat sdf = null;
            sdf = new SimpleDateFormat(strFormat);
            sDateTime = sdf.format(Cal.getTime());
        } catch (Exception e) {
        }
        return sDateTime;
    }

    public static Calendar parseDateTime(String baseDate) {
        Calendar cal = null;
        cal = new GregorianCalendar();
        int yy = Integer.parseInt(baseDate.substring(0, 4));
        int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
        int dd = Integer.parseInt(baseDate.substring(8, 10));
        int hh = 0;
        int mi = 0;
        int ss = 0;
        if (baseDate.length() > 12) {
            hh = Integer.parseInt(baseDate.substring(11, 13));
            mi = Integer.parseInt(baseDate.substring(14, 16));
            ss = Integer.parseInt(baseDate.substring(17, 19));
        }
        cal.set(yy, mm, dd, hh, mi, ss);
        return cal;
    }

    public int getDay(String strDate) {
        Calendar cal = parseDateTime(strDate);
        return cal.get(Calendar.DATE);
    }

    public int getMonth(String strDate) {
        Calendar cal = parseDateTime(strDate);
        return cal.get(Calendar.MONTH) + 1;
    }

    public int getWeekDay(String strDate) {
        Calendar cal = parseDateTime(strDate);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public String getWeekDayName(String strDate) {
        String mName[] = { "日", "一", "二", "三", "四", "五", "六" };
        int iWeek = getWeekDay(strDate);
        iWeek = iWeek - 1;
        return "星期" + mName[iWeek];
    }

    public int getYear(String strDate) {
        Calendar cal = parseDateTime(strDate);
        return cal.get(Calendar.YEAR) + 1900;
    }

    public String DateAdd(String strDate, int iCount, int iType) {
        Calendar Cal = parseDateTime(strDate);
        int pType = 0;
        if (iType == 0) {
            pType = 1;
        } else if (iType == 1) {
            pType = 2;
        } else if (iType == 2) {
            pType = 5;
        } else if (iType == 3) {
            pType = 10;
        } else if (iType == 4) {
            pType = 12;
        } else if (iType == 5) {
            pType = 13;
        }
        Cal.add(pType, iCount);
        SimpleDateFormat sdf = null;
        if (iType <= 2)
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        else
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(Cal.getTime());
        return sDate;
    }

    public String DateAdd(String strOption, int iDays, String strStartDate) {
        if (!strOption.equals("d"))
            ;
        return strStartDate;
    }

    public int DateDiff(String strDateBegin, String strDateEnd, int iType) {
        Calendar calBegin = parseDateTime(strDateBegin);
        Calendar calEnd = parseDateTime(strDateEnd);
        long lBegin = calBegin.getTimeInMillis();
        long lEnd = calEnd.getTimeInMillis();
        int ss = (int) ((lBegin - lEnd) / 1000L);
        int min = ss / 60;
        int hour = min / 60;
        int day = hour / 24;
        if (iType == 0)
            return hour;
        if (iType == 1)
            return min;
        if (iType == 2)
            return day;
        else
            return -1;
    }

    /***************************************************************************
     * @功能 判断某年是否为闰年
     * @return boolean
     * @throws ParseException
     **************************************************************************/
    public boolean isLeapYear(int yearNum) {
        boolean isLeep = false;
        /** 判断是否为闰年,赋值给一标识符flag */
        if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
            isLeep = true;
        } else if (yearNum % 400 == 0) {
            isLeep = true;
        } else {
            isLeep = false;
        }
        return isLeep;
    }

    /***************************************************************************
     * @功能 计算当前日期某年的第几周
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public int getWeekNumOfYear() {
        Calendar calendar = Calendar.getInstance();
        int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        return iWeekNum;
    }

    /***************************************************************************
     * @功能 计算指定日期某年的第几周
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public int getWeekNumOfYearDay(String strDate) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = format.parse(strDate);
        calendar.setTime(curDate);
        int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        return iWeekNum;
    }

    /***************************************************************************
     * @功能 计算某年某周的开始日期
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public String getYearWeekFirstDay(int yearNum, int weekNum) throws ParseException {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // 分别取得当前日期的年、月、日
        String tempYear = Integer.toString(cal.get(Calendar.YEAR));
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return SetDateFormat(tempDate, "yyyy-MM-dd");

    }

    /***************************************************************************
     * @功能 计算某年某周的结束日期
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public String getYearWeekEndDay(int yearNum, int weekNum) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // 分别取得当前日期的年、月、日
        String tempYear = Integer.toString(cal.get(Calendar.YEAR));
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return SetDateFormat(tempDate, "yyyy-MM-dd");
    }

    /***************************************************************************
     * @功能 计算某年某月的开始日期
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public String getYearMonthFirstDay(int yearNum, int monthNum) throws ParseException {

        // 分别取得当前日期的年、月、日
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(monthNum);
        String tempDay = "1";
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return SetDateFormat(tempDate, "yyyy-MM-dd");

    }

    /***************************************************************************
     * @功能 计算某年某月的结束日期
     * @return interger
     * @throws ParseException
     **************************************************************************/
    public String getYearMonthEndDay(int yearNum, int monthNum) throws ParseException {

        // 分别取得当前日期的年、月、日
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(monthNum);
        String tempDay = "31";
        if (tempMonth.equals("1") || tempMonth.equals("3") || tempMonth.equals("5")
                || tempMonth.equals("7") || tempMonth.equals("8")
                || tempMonth.equals("10") || tempMonth.equals("12")) {
            tempDay = "31";
        }
        if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9")
                || tempMonth.equals("11")) {
            tempDay = "30";
        }
        if (tempMonth.equals("2")) {
            if (isLeapYear(yearNum)) {
                tempDay = "29";
            } else {
                tempDay = "28";
            }
        }
        // System.out.println("tempDay:" + tempDay);
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return SetDateFormat(tempDate, "yyyy-MM-dd");
    }
}


自定义颜色类:
package transms.app._0all.util.providers;

import java.awt.Color;

public class Colors extends Color {

    private static final long serialVersionUID = 3680677118390160469L;

    public Colors(int rgb) {
        super(rgb);
    }

    /**
     * 淡黄
     */
    public static final Color Khaki1 = new Color(255, 246, 143);

    /**
     * 浅蓝
     */
    public static final Color PaleTurquoise1 = new Color(187, 255, 255);

    /**
     * 背景色
     */
    public static final Color Ivory2 = new Color(238, 238, 224);

    /**
     * 火红
     */
    public static final Color Firebrick1 = new Color(255, 48, 48);

    /**
     * 暗绿
     */
    public static final Color Chartreuse3 = new Color(69, 139, 0);

    /**
     * 紫
     */
    public static final Color Purple3 = new Color(125, 38, 205);

    /**
     * 橘红
     */
    public static final Color OrangeRed = new Color(255, 69, 0);

    /**
     * 暗橙
     */
    public static final Color DarkOrange1 = new Color(255, 127, 0);

    /**
     * 暗橄榄绿
     */
    public static final Color DarkOliveGreen2 = new Color(188, 238, 104);

    /**
     * 淡红
     */
    public static final Color LightSalmon = new Color(255, 160, 122);

    /**
     * 淡绿
     */
    public static final Color PaleGreen = new Color(152, 251, 152);
}
  • 大小: 6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics