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

📄 datefield.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            ItemStateListener isl = null;            synchronized (Display.LCDUILock) {                if (cmd == OK) {                    field.saveDate(calendar.getTime());                    item = field;                    form = (Form)item.getOwner();                    isl = form.getItemStateListener();                }                currentDisplay.setCurrent(returnScreen);            } // synchronized            // SYNC NOTE: Move the call to the application's            // ItemStateChangedListener outside the lock            if (form != null) {                form.itemStateChanged(item, isl);            }        }        /**         * Set the current date and time         *         * @param currentValue The Date to set the editor to         * @param mode The operating mode of the DateField         */        void setDateTime(Date currentValue, int mode) {            calendar.setTime(currentValue);            this.mode = mode;            if (CLOCK_USES_AM_PM && !ampmAfterTime) {                timeSel = Calendar.AM_PM;            } else {                timeSel = Calendar.HOUR;            }            // highlight = calendar.get(Calendar.DATE);            highlight = -1; // highlight the year to start with            repaint();        }        /**         * Paint the content of this editor         *         * @param g The Graphics object to paint to         */        void paintContent(Graphics g) {            if (mode == TIME) {                paintClock(g);            } else {                paintCalendar(g);            }        }        /**         * Layout the content of this editor given the width/height         *         * @param w The allowable width of this editor         * @param h The allowable height of this editor         * @return int The height needed to fully display this editor         *              given the allowable width         */        int layoutContent(int w, int h) {            this.width = w;            this.height = h;            if (mode == TIME) {                return (50 + large.getHeight());            }            return (regular.getHeight() - 1 +                    7 * regular.getBaselinePosition() + 7);        }        /**         * Initialize the highlight of this editor         *         * @param vpY         * @param vpH         * @return int Always returns 0         */        int initHilight(int vpY, int vpH) {            if (mode == DATE) {                int hilightBottom = highlightY(true);                if (hilightBottom > vpH) {                    return (hilightBottom - vpH);                }            }            return 0;        }        /**         * Traverse this editor         *         * @param action         * @param top         * @param bottom         * @return int         */        int traverse(int action, int top, int bottom) {            if (mode == DATE) {                return traverseDate(action, top, bottom);            }            return traverseClock(action, top, bottom);        }        /**         * Handle a keypress         *         * @param keyCode The integer code of the key that was pressed         */        void keyPressed(int keyCode) {            synchronized (Display.LCDUILock) {                super.keyPressedImpl(keyCode);                if (Display.getGameAction(keyCode) == Canvas.FIRE) {                    selectFired();                }            }        }        /**         * Handle a selection         */        void selectFired() {            if (!SELECT_TRANSFERS_FOCUS) {                return;            }            if (mode == DATE) {                if (highlight > 0) {                    highlight = -1;                } else if (highlight == -1) {                    highlight = 0;                } else {                    highlight = calendar.get(Calendar.DATE);                }                if (highlight > 0) {                    calendar.set(Calendar.DATE, highlight);                }            } else {                if (timeSel == Calendar.MINUTE) {                    timeSel = Calendar.HOUR;                } else {                    timeSel = Calendar.MINUTE;                }            }            repaint();        }        /**         * Paint the clock         *         * @param g The Graphics to paint to         */        void paintClock(Graphics g) {            int hour   = calendar.get(Calendar.HOUR) % 12;            int minute = calendar.get(Calendar.MINUTE);            g.setColor(Display.ERASE_COLOR);            g.fillRect(0, 0, width, height);            int digits_height = large.getHeight()                               + ARROW_UP.getHeight()                              + ARROW_DOWN.getHeight()                              + 2;            int clockSize = height - digits_height;            if (width < clockSize) {                clockSize = width;            }            if (60 < clockSize) {                clockSize = 60;            }            // For reference, the above if statements are replacing            // this Math() call below.            // Math.min(60, Math.min(width, height - digits_height));            g.translate((width - clockSize) / 2,                         (height - (clockSize + digits_height)) / 2);            g.setColor(Display.FG_COLOR);            g.drawRoundRect(0, 0, clockSize, clockSize,                             clockSize / 2, clockSize / 2);            g.drawLine(clockSize / 2, 0, clockSize / 2, 5);            g.drawLine(clockSize / 2, clockSize,                        clockSize / 2, clockSize - 5);            g.drawLine(0, clockSize / 2, 5, clockSize / 2);            g.drawLine(clockSize, clockSize / 2,                        clockSize - 5, clockSize / 2);            int minuteAngle = 90 - (minute * 6);            int hourAngle   = 90 - (hour * 30 + (minute / 2));            g.translate(clockSize / 2, clockSize / 2);            g.drawLine(0, 0,                        (cos(hourAngle)*clockSize / 4) >> 16,                       -(sin(hourAngle)*clockSize / 4) >> 16);            g.drawLine(0, 0,                        (cos(minuteAngle)*(clockSize / 2 - 10)) >> 16,                       -(sin(minuteAngle)*(clockSize / 2 - 10)) >> 16);            g.translate(0, clockSize / 2 + 2 + ARROW_UP.getHeight());            g.setFont(large);            if (CLOCK_USES_AM_PM) {                String ampm = Resource.getString(ampmString(calendar));                if (hour == 0) {                    hour = 12;                }                String timeString;                if (ampmAfterTime) {                    timeString =                         twoDigits(hour) + ":" + twoDigits(minute) + " " + ampm;                } else {                    timeString =                         ampm + " " + twoDigits(hour) + ":" + twoDigits(minute);                }                g.translate(-large.stringWidth(timeString) / 2, 0);                g.drawString(timeString,                             0, 0, Graphics.LEFT | Graphics.TOP);                int dX;                int w;                int h = large.getBaselinePosition() + 1;                int offset;                int len;                if (ampmAfterTime) {                    if (timeSel == Calendar.HOUR) {                        offset = 0;                        len = 2;                    } else if (timeSel == Calendar.MINUTE) {                        offset = 3;                        len = 2;                    } else {                        offset = 6;                        len = timeString.length() - offset;                    }                } else {                    offset = ampm.length();                    if (timeSel == Calendar.HOUR) {                        offset += 1;                        len = 2;                    } else if (timeSel == Calendar.MINUTE) {                        offset += 4;                        len = 2;                    } else {                        len = offset;                        offset = 0;                    }                }                dX = large.substringWidth(timeString, 0, offset);                w = large.substringWidth(timeString, offset, len);                g.fillRect(dX, 1, w, h -1);                g.setColor(Display.FG_H_COLOR);                g.drawSubstring(timeString, offset, len,                                dX, 0, Graphics.LEFT | Graphics.TOP);                if (ampmAfterTime) {                    if (timeSel != Calendar.HOUR) {                        g.drawImage(ARROW_LEFT,                                     -1, h / 2 + 2,                                    Graphics.RIGHT | Graphics.VCENTER);                    }                    if (timeSel != Calendar.AM_PM) {                        g.drawImage(ARROW_RIGHT,                                    large.stringWidth(timeString) + 1,                                    h / 2 + 2,                                    Graphics.LEFT | Graphics.VCENTER);                    }                } else {                    if (timeSel != Calendar.AM_PM) {                        g.drawImage(ARROW_LEFT,                                    -1, h / 2 + 2,                                    Graphics.RIGHT | Graphics.VCENTER);                    }                    if (timeSel != Calendar.MINUTE) {                        g.drawImage(ARROW_RIGHT,                                    large.stringWidth(timeString) + 1,                                    h / 2 + 2,                                    Graphics.LEFT | Graphics.VCENTER);                    }                }                g.drawImage(ARROW_UP,                            dX + w / 2, 0, Graphics.HCENTER | Graphics.BOTTOM);                g.drawImage(ARROW_DOWN,                            dX + w / 2, h + 1,                            Graphics.HCENTER | Graphics.TOP);            } else {                g.drawString(":", 0, 0, Graphics.LEFT | Graphics.TOP);                hour = calendar.get(Calendar.HOUR_OF_DAY);                String str = hour + "";                int w = large.stringWidth(str);                int h = large.getBaselinePosition() + 1;                g.translate(-1, 0);                if (timeSel == Calendar.HOUR) {                    g.setColor(Display.BG_H_COLOR);                    g.fillRect(-w, 1, w + 1, h - 1);                    g.setColor(Display.FG_H_COLOR);                    g.drawImage(ARROW_UP, -w / 2, 0,                                 Graphics.HCENTER | Graphics.BOTTOM);                    g.drawImage(ARROW_DOWN,                                -w / 2, h + 1,                                Graphics.HCENTER | Graphics.TOP);                } else {                    g.setColor(Display.FG_COLOR);                    g.drawImage(ARROW_LEFT, -(w + 1), h / 2,                                 Graphics.RIGHT | Graphics.VCENTER);                }                g.drawString(str, 0, 0,                             Graphics.RIGHT | Graphics.TOP);                str = twoDigits(minute);                w   = large.stringWidth(str);                g.translate(1 + large.charWidth(':'), 0);                if (timeSel == Calendar.MINUTE) {                    g.setColor(Display.BG_H_COLOR);                    g.fillRect(0, 1, w + 1, h - 1);                    g.setColor(Display.FG_H_COLOR);                    g.drawImage(ARROW_UP, w / 2, 0,                                 Graphics.HCENTER | Graphics.BOTTOM);                    g.drawImage(ARROW_DOWN, w / 2,  h + 1,                                Graphics.HCENTER | Graphics.TOP);                } else {                    g.setColor(Display.FG_COLOR);                    g.drawImage(ARROW_RIGHT, w + 2, h / 2,                                 Graphics.LEFT | Graphics.VCENTER);                }                g.drawString(twoDigits(minute), 0, 0,                             Graphics.LEFT | Graphics.TOP);            }        }        /**         * The selected time element

⌨️ 快捷键说明

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