📄 sdadatetimepicker.java
字号:
selectLen = hourLen; selectText = String.valueOf(Hour); break; case 4: result = yearLen + monthLen + dayLen + hourLen + ft.stringWidth("-- :"); selectLen = minuteLen; selectText = String.valueOf(Minute); break; case 5: result = yearLen + monthLen + dayLen + hourLen + minuteLen + ft.stringWidth("-- ::"); selectLen = secondLen; selectText = String.valueOf(Second); break; } } if (dateKind == SDAConsts.dkTime) { switch (selectType) { case 3: result = 0; selectLen = hourLen; selectText = String.valueOf(Hour); break; case 4: result = hourLen + ft.charWidth(':'); selectLen = minuteLen; selectText = String.valueOf(Minute); break; case 5: result = hourLen + minuteLen + ft.stringWidth("::"); selectLen = secondLen; selectText = String.valueOf(Second); break; } } selectStart = result; } public void paint() { if (IsCanPaint()) { internalPaint(); PaintChilds(); } } private void internalPaint() { //画 Graphics g = form.getGraphics(); g.setFont(getFont()); SetClip(g); //画边框和箭头 drawControlRect(g); //最后打字 drawControlText(g); } private void drawControlRect(Graphics g) { int thisHeight = getHeight(); int thisWidth = getWidth(); g.setColor(backColor); fillRect(g, 0, 0, thisWidth, thisHeight); g.setColor(borderColor); drawRect(g, 0, 0, thisWidth, thisHeight); g.setColor(arrowBackColor); //两边的框 fillRect(g, 0, 0, arrowWidth, thisHeight); fillRect(g, thisWidth - arrowWidth, 0, arrowWidth, thisHeight); g.setColor(borderColor); drawRect(g, 0, 0, thisWidth, thisHeight); drawRect(g, 0, 0, arrowWidth, thisHeight); drawRect(g, thisWidth - arrowWidth, 0, arrowWidth, thisHeight); //左箭头 g.setColor(borderColor); fillTriangle(g, arrowWidth / 2 - 2, thisHeight / 2, arrowWidth / 2 + 2, thisHeight / 2 - 4, arrowWidth / 2 + 2, thisHeight / 2 + 4); //右箭头 fillTriangle(g, thisWidth - arrowWidth / 2 - 2, thisHeight / 2 - 4, thisWidth - arrowWidth / 2 - 2, thisHeight / 2 + 4, thisWidth - arrowWidth / 2 + 2, thisHeight / 2); //焦点 if (isFoucsed()) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } } private void drawControlText(Graphics g) { int thisHeight = getHeight(); int thisWidth = getWidth(); SetClip(g, arrowWidth, 1, thisWidth - 2 * arrowWidth, thisHeight - 1); g.setColor(foreColor); int pos = (thisWidth - getFont().stringWidth(dateTime)) / 2; int toppos = (thisHeight - getFont().getHeight()) / 2; drawString(g, dateTime, pos, toppos); if (canEdit) { //显示编辑框 NumEdit.left = pos + selectStart - 2; NumEdit.top = 0; NumEdit.width = selectLen + 6; NumEdit.height = height; NumEdit.text = selectText; if (selectType > -1 && NumEdit.visible) { NumEdit.paint(); } } else { //画选择焦点 if (selectType > -1) { setSelectTypePos(selectType); g.setColor(selectedBackColor); fillRect(g, pos + selectStart, toppos, selectLen, getFont().getHeight()); //内容 g.setColor(selectedFontColor); drawString(g, selectText, pos + selectStart, toppos); } } } private void doKeyUp(int keyCode) { String key = form.getKeyName(keyCode).toUpperCase(); if (key.equals(SDAConsts.KEY_UP)) { //左 if (dateKind != SDAConsts.dkTime) { if (selectType > 0) { selectType--; } } else { if (selectType > 3) { selectType--; } } if (canEdit) { NumEdit.visible = true; } } if (key.equals(SDAConsts.KEY_LEFT)) { setSelectValue(-1, true); } if (key.equals(SDAConsts.KEY_DOWN)) { if (canEdit) { if (!NumEdit.visible) { NumEdit.visible = true; return; } } //右 if (dateKind != SDAConsts.dkDate) { if (selectType < 5) { selectType++; } } else { if (selectType < 2) { selectType++; } } } if (key.equals(SDAConsts.KEY_RIGHT)) { setSelectValue(1, true); } repaintControl(); /* if (key.equals(SDAConsts.KEY_SELECT)) { this.popVisible = !popVisible; } */ setSelectTypePos(selectType); } private void doPointerPress(int x, int y) { int posx = screenXToClient(x); int posy = screenYToClient(y); int thisHeight = getHeight(); int thisWidth = getWidth(); if (InClientRect(posx, posy, 0, 0, thisWidth, thisHeight)) { if (InClientRect(posx, posy, 0, 0, arrowWidth, thisHeight)) { setSelectValue(-1, true); } else if (InClientRect(posx, posy, thisWidth - arrowWidth, 0, arrowWidth, thisHeight)) { setSelectValue(1, true); } getSelectTypeByPos(posx); } } //根据位置获取选择 private void getSelectTypeByPos(int x) { int oldType = selectType; boolean find = false; if (x > arrowWidth && x < getWidth() - arrowWidth) { int pos = (getWidth() - getFont().stringWidth(dateTime)) / 2; if (dateKind == SDAConsts.dkDate) { for (int i = 0; i < 3; i++) { setSelectTypePos(i); if (pos + selectStart < x && (pos + selectStart + selectLen > x)) { selectType = i; find = true; break; } } } if (dateKind == SDAConsts.dkDateTime) { for (int i = 0; i < 6; i++) { setSelectTypePos(i); if (pos + selectStart < x && (pos + selectStart + selectLen > x)) { selectType = i; find = true; break; } } } if (dateKind == SDAConsts.dkTime) { for (int i = 3; i < 6; i++) { setSelectTypePos(i); if (pos + selectStart < x && (pos + selectStart + selectLen > x)) { selectType = i; find = true; break; } } } } if (!find) { selectType = oldType; } } //对选择的内容增加和减少 private void setSelectValue(int value, boolean incValue) { try { switch (selectType) { case 0: if (incValue) { Year += value; } else { Year = value; } cl.set(Calendar.YEAR, Year); setDateTime(); break; case 1: if (incValue) { Month += value; } else { Month = value; } cl.set(Calendar.MONTH, Month - 1); setDateTime(); break; case 2: if (incValue) { Day += value; } else { Day = value; } cl.set(Calendar.DAY_OF_MONTH, Day); setDateTime(); break; case 3: if (incValue) { Hour += value; } else { Hour = value; } cl.set(Calendar.HOUR_OF_DAY, Hour); setDateTime(); break; case 4: if (incValue) { Minute += value; } else { Minute = value; } cl.set(Calendar.MINUTE, Minute); setDateTime(); break; case 5: if (incValue) { Second += value; } else { Second = value; } cl.set(Calendar.SECOND, Second); setDateTime(); break; } } catch (Exception e) { } } protected boolean canDownTabNext() { boolean result = false; if (dateKind == SDAConsts.dkDate) { if (selectType == 2) { result = true; } else { result = false; } } if (dateKind == SDAConsts.dkDateTime || dateKind == SDAConsts.dkTime) { if (selectType == 5) { result = true; } else { result = false; } } return result; } protected boolean canLeftTabPrior() { return false; } protected boolean canRightTabNext() { return false; } protected boolean canUpTabPrior() { boolean result = false; if (dateKind == SDAConsts.dkDate || dateKind == SDAConsts.dkDateTime) { if (selectType == 0) { result = true; } else { result = false; } } if (dateKind == SDAConsts.dkTime) { if (selectType == 3) { result = true; } else { result = false; } } return result; } public boolean isCanEdit() { return canEdit; } public void setCanEdit(boolean canEdit) { this.canEdit = canEdit; if (canEdit) { NumEdit.visible=true; NumEdit.tabStop=true; } else { NumEdit.visible=false; NumEdit.tabStop=false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -