java中见面设计,怎样在一个JTextFirld中添加一个日期选择器。 今天做老师布置的项目是发现,界面中,在JTextField中输入日期时使用一个日期选择器感觉很专业的样子。哪位大哥大姐帮帮小弟。有现成的日期选择类或接口之类的东西吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个东东试试import java.awt.Toolkit;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.text.*;@SuppressWarnings("serial")public class JDateDocument extends PlainDocument { private JTextComponent textComponent; private SimpleDateFormat dateFormat; public JDateDocument(JTextComponent jtextcomponent, SimpleDateFormat simpledateformat) throws UnsupportedOperationException { this(jtextcomponent, simpledateformat, getCurrentDate(simpledateformat)); } public JDateDocument(JTextComponent jtextcomponent, SimpleDateFormat simpledateformat, String s) throws UnsupportedOperationException { setDateFormat(simpledateformat); textComponent = jtextcomponent; try { insertString(0, s, null); } catch (BadLocationException badlocationexception) { throw new UnsupportedOperationException(badlocationexception .getMessage()); } } public void setDateFormat(SimpleDateFormat simpledateformat) { dateFormat = simpledateformat; } public SimpleDateFormat getDateFormat() { return dateFormat; } public static String getCurrentDate(SimpleDateFormat simpledateformat) { return simpledateformat.format(new Date()); } @Override public void insertString(int i, String s, AttributeSet attributeset) throws BadLocationException { if (s.length() == 1) { try { Integer.parseInt(s); } catch (Exception exception) { Toolkit.getDefaultToolkit().beep(); return; } int j = i; if (i == 4 || i == 7 || i == 10 || i == 13 || i == 16) { j++; textComponent.setCaretPosition(j); } if (i == dateFormat.toPattern().length()) return; String s1 = textComponent.getText(); s1 = s1.substring(0, j) + s + s1.substring(j + 1); boolean flag = isValidDate(s1); if (!flag) { Toolkit.getDefaultToolkit().beep(); return; } super.remove(j, 1); super.insertString(j, s, attributeset); } else if (s.length() == 10 || s.length() == 19) { if (!isValidDate(s)) { Toolkit.getDefaultToolkit().beep(); return; } super.remove(0, getLength()); super.insertString(0, s, attributeset); } } @Override public void remove(int i, int j) throws BadLocationException { if (i == 4 || i == 7 || i == 10 || i == 13 || i == 16) textComponent.setCaretPosition(i - 1); else textComponent.setCaretPosition(i); } private boolean isValidDate(String s) { int l = 0; int i1 = 0; int j1 = 0; int l1 = getDateFormat().toPattern().length(); if (s == null) return false; s = s.trim(); if (s.length() != l1) return false; for (int i2 = 0; i2 < 10; i2++) if (s.charAt(i2) > '\377') return false; int i; int j; int k; try { i = Integer.parseInt(s.substring(0, 4)); j = Integer.parseInt(s.substring(5, 7)); k = Integer.parseInt(s.substring(8, 10)); } catch (Exception exception) { return false; } int k1 = textComponent.getCaretPosition(); boolean flag = true; if (j > 12 || j < 1) { j = Math.min(12, Math.max(1, j)); flag = false; } if (k < 1) { k = 1; flag = false; } switch (j) { case 4: // '\004' case 6: // '\006' case 9: // '\t' case 11: // '\013' if (k > 30) { k = 30; flag = false; } break; case 2: // '\002' if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { if (k > 29) { k = 29; flag = false; } } else if (k > 28) { k = 28; flag = false; } break; case 3: // '\003' case 5: // '\005' case 7: // '\007' case 8: // '\b' case 10: // '\n' default: if (k > 31) { k = 31; flag = false; } break; } if (l1 > 10) { try { l = Integer.parseInt(s.substring(11, 13)); i1 = Integer.parseInt(s.substring(14, 16)); j1 = Integer.parseInt(s.substring(17)); } catch (Exception exception1) { return false; } if (l > 23 || l < 0) { l = Math.min(23, Math.max(0, l)); flag = false; } if (i1 > 59 || i1 < 0) { i1 = Math.min(59, Math.max(0, i1)); flag = false; } if (j1 > 59 || j1 < 0) { j1 = Math.min(59, Math.max(0, j1)); flag = false; } } if (!flag) { textComponent.setText(toDateString(i, j, k, l, i1, j1)); textComponent.setCaretPosition(k1 + 1); } return flag; } private String toDateString(int i, int j, int k, int l, int i1, int j1) { j = Math.max(1, Math.min(12, j)); k = Math.max(1, Math.min(31, k)); switch (j) { case 3: // '\003' case 5: // '\005' case 7: // '\007' case 8: // '\b' case 10: // '\n' default: break; case 4: // '\004' case 6: // '\006' case 9: // '\t' case 11: // '\013' k = Math.min(30, k); break; case 2: // '\002' if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) k = Math.min(29, k); else k = Math.min(28, k); break; } l = Math.max(1, Math.min(24, l)); i1 = Math.max(1, Math.min(59, i1)); j1 = Math.max(1, Math.min(59, j1)); String s = getDateFormat().toPattern(); String s1 = rPad0(4, "" + i); String s2 = rPad0(2, "" + j); String s3 = rPad0(2, "" + k); String s4 = s1 + s.substring(4, 5) + s2 + s.substring(7, 8) + s3; if (s.length() == 19) s4 = s4 + s.substring(10, 11) + rPad0(2, "" + l) + s.substring(13, 14) + rPad0(2, "" + i1) + s.substring(16, 17) + rPad0(2, "" + j1); return s4; } private String rPad0(int i, String s) { if (s.length() < i) s = "0" + s; return s; }} SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); JDateDocument dateDocument = new JDateDocument(this, dateFormat); textfield.setDocument(dateDocument); JTextField textfield = new JTextfield();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");JDateDocument dateDocument = new JDateDocument(textfield, dateFormat);textfield.setDocument(dateDocument); 话说我曾经用swing包过一个日期盘,给公司项目用的。界面都是自己用swing画的,挺麻烦的,可能是我写的麻烦,我也不知道有没有现成的可以用,swing得有点底子才能写吧,一步步来,别小看了swing,个人认为swing是JDK中设计最合理,书写最优美的代码之一,仔细参阅源码收获颇多啊 swing控件 八位二进制转double,关键是有正负 JDBC 4.0 请问有java中文帮助吗? Eclipse运行APPLET小程序出现未初始化小程序,求教 急求助!!!!! Remote和Local问题 一个GUI的烤鸭程序, 可难坏我这个菜鸟了, 各位大虾帮一把吧, 多谢了. 关于利用文件流进行文件读取显示的问题 抽象类有没有默认的构造函数? java 解析 文件 java入门求教,如何学好java,求教各位大虾 大家帮忙看一下我的java小程序,为何jpanel上没有显示东西
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.text.*;@SuppressWarnings("serial")
public class JDateDocument extends PlainDocument { private JTextComponent textComponent;
private SimpleDateFormat dateFormat; public JDateDocument(JTextComponent jtextcomponent,
SimpleDateFormat simpledateformat)
throws UnsupportedOperationException {
this(jtextcomponent, simpledateformat, getCurrentDate(simpledateformat));
} public JDateDocument(JTextComponent jtextcomponent,
SimpleDateFormat simpledateformat, String s)
throws UnsupportedOperationException {
setDateFormat(simpledateformat);
textComponent = jtextcomponent;
try {
insertString(0, s, null);
} catch (BadLocationException badlocationexception) {
throw new UnsupportedOperationException(badlocationexception
.getMessage());
}
} public void setDateFormat(SimpleDateFormat simpledateformat) {
dateFormat = simpledateformat;
} public SimpleDateFormat getDateFormat() {
return dateFormat;
} public static String getCurrentDate(SimpleDateFormat simpledateformat) {
return simpledateformat.format(new Date());
} @Override
public void insertString(int i, String s, AttributeSet attributeset)
throws BadLocationException {
if (s.length() == 1) {
try {
Integer.parseInt(s);
} catch (Exception exception) {
Toolkit.getDefaultToolkit().beep();
return;
}
int j = i;
if (i == 4 || i == 7 || i == 10 || i == 13 || i == 16) {
j++;
textComponent.setCaretPosition(j);
}
if (i == dateFormat.toPattern().length())
return;
String s1 = textComponent.getText();
s1 = s1.substring(0, j) + s + s1.substring(j + 1);
boolean flag = isValidDate(s1);
if (!flag) {
Toolkit.getDefaultToolkit().beep();
return;
}
super.remove(j, 1);
super.insertString(j, s, attributeset);
} else if (s.length() == 10 || s.length() == 19) {
if (!isValidDate(s)) {
Toolkit.getDefaultToolkit().beep();
return;
}
super.remove(0, getLength());
super.insertString(0, s, attributeset);
}
} @Override
public void remove(int i, int j) throws BadLocationException {
if (i == 4 || i == 7 || i == 10 || i == 13 || i == 16)
textComponent.setCaretPosition(i - 1);
else
textComponent.setCaretPosition(i);
} private boolean isValidDate(String s) {
int l = 0;
int i1 = 0;
int j1 = 0;
int l1 = getDateFormat().toPattern().length();
if (s == null)
return false;
s = s.trim();
if (s.length() != l1)
return false;
for (int i2 = 0; i2 < 10; i2++)
if (s.charAt(i2) > '\377')
return false; int i;
int j;
int k;
try {
i = Integer.parseInt(s.substring(0, 4));
j = Integer.parseInt(s.substring(5, 7));
k = Integer.parseInt(s.substring(8, 10));
} catch (Exception exception) {
return false;
}
int k1 = textComponent.getCaretPosition();
boolean flag = true;
if (j > 12 || j < 1) {
j = Math.min(12, Math.max(1, j));
flag = false;
}
if (k < 1) {
k = 1;
flag = false;
}
switch (j) {
case 4: // '\004'
case 6: // '\006'
case 9: // '\t'
case 11: // '\013'
if (k > 30) {
k = 30;
flag = false;
}
break; case 2: // '\002'
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
if (k > 29) {
k = 29;
flag = false;
}
} else if (k > 28) {
k = 28;
flag = false;
}
break; case 3: // '\003'
case 5: // '\005'
case 7: // '\007'
case 8: // '\b'
case 10: // '\n'
default:
if (k > 31) {
k = 31;
flag = false;
}
break;
}
if (l1 > 10) {
try {
l = Integer.parseInt(s.substring(11, 13));
i1 = Integer.parseInt(s.substring(14, 16));
j1 = Integer.parseInt(s.substring(17));
} catch (Exception exception1) {
return false;
}
if (l > 23 || l < 0) {
l = Math.min(23, Math.max(0, l));
flag = false;
}
if (i1 > 59 || i1 < 0) {
i1 = Math.min(59, Math.max(0, i1));
flag = false;
}
if (j1 > 59 || j1 < 0) {
j1 = Math.min(59, Math.max(0, j1));
flag = false;
}
}
if (!flag) {
textComponent.setText(toDateString(i, j, k, l, i1, j1));
textComponent.setCaretPosition(k1 + 1);
}
return flag;
} private String toDateString(int i, int j, int k, int l, int i1, int j1) {
j = Math.max(1, Math.min(12, j));
k = Math.max(1, Math.min(31, k));
switch (j) {
case 3: // '\003'
case 5: // '\005'
case 7: // '\007'
case 8: // '\b'
case 10: // '\n'
default:
break; case 4: // '\004'
case 6: // '\006'
case 9: // '\t'
case 11: // '\013'
k = Math.min(30, k);
break; case 2: // '\002'
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
k = Math.min(29, k);
else
k = Math.min(28, k);
break;
}
l = Math.max(1, Math.min(24, l));
i1 = Math.max(1, Math.min(59, i1));
j1 = Math.max(1, Math.min(59, j1));
String s = getDateFormat().toPattern();
String s1 = rPad0(4, "" + i);
String s2 = rPad0(2, "" + j);
String s3 = rPad0(2, "" + k);
String s4 = s1 + s.substring(4, 5) + s2 + s.substring(7, 8) + s3;
if (s.length() == 19)
s4 = s4 + s.substring(10, 11) + rPad0(2, "" + l)
+ s.substring(13, 14) + rPad0(2, "" + i1)
+ s.substring(16, 17) + rPad0(2, "" + j1);
return s4;
} private String rPad0(int i, String s) {
if (s.length() < i)
s = "0" + s;
return s;
}
}
JDateDocument dateDocument = new JDateDocument(this, dateFormat);
textfield.setDocument(dateDocument);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JDateDocument dateDocument = new JDateDocument(textfield, dateFormat);
textfield.setDocument(dateDocument);