当选择当月的第一天时: - 2014年4月1日,
在jdatechooser中显示为: 91-04-2014 ,我的dateFormatString为:“dd-mm-yyyy”
这是我的代码:
public class Student extends javax.swing.JFrame {
/**
* Creates new form Student
*/
static JFormattedTextField gradeText;
public Student() {
//Methods
initComponents();
KeyTypos();
updateTable();
//Frame settings
pack();
setLocationRelativeTo(null);
ImageIcon ic = new ImageIcon("src\\main\\java\\com\\cyrax\\images\\Icon.png");
this.setIconImage(ic.getImage());
//initializations
stud.clearSelection();
gradeText = ((JSpinner.DefaultEditor) grd.getEditor()).getTextField();
dob.setDateFormatString("DD-MM-YYYY");
}
public final void KeyTypos() {
try {
gradeText = ((JSpinner.DefaultEditor) grd.getEditor()).getTextField();
gradeText.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(c >= '0' & c <= '9') | c == e.VK_PERIOD) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (!gradeText.getText().toString().isEmpty()) {
int val = Integer.parseInt(gradeText.getText());
if (val > 11) {
String oldVal = gradeText.getText().toString();
char str[] = oldVal.toCharArray();
StringBuilder sb = new StringBuilder(oldVal);
sb.deleteCharAt(sb.length() - 1);
String newVal = sb.toString();
gradeText.setText(newVal);
}
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
stud = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
lname = new javax.swing.JTextField();
fname = new javax.swing.JTextField();
mname = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
mtp = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
addr = new javax.swing.JTextField();
em = new javax.swing.JTextField();
sch = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
ftp = new javax.swing.JTextField();
grd = new javax.swing.JSpinner();
dob = new com.toedter.calendar.JDateChooser();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Student");
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/save.png"))); // NOI18N
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/rolledsave.png"))); // NOI18N
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar1.add(jButton1);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/delete.png"))); // NOI18N
jButton2.setBorderPainted(false);
jButton2.setContentAreaFilled(false);
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/rolleddelete.png"))); // NOI18N
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton2);
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/update.png"))); // NOI18N
jButton3.setBorderPainted(false);
jButton3.setContentAreaFilled(false);
jButton3.setFocusable(false);
jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton3.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/rolledupdate.png"))); // NOI18N
jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton3);
jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cyrax/images/Sign-Refresh-icon.png"))); // NOI18N
jButton4.setFocusable(false);
jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jButton4);
jPanel1.add(jToolBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1250, 60));
stud.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Student ID", "Student Name", "DOB", "Address", "Mothers TP", "Fathers TP", "School", "Grade", "E-mail"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(stud);
jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 1230, 190));
jLabel1.setText("Last Name :");
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 370, -1, 20));
jLabel2.setText("Mothers TP :");
jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 370, -1, 20));
jLabel3.setText("Middle Name :");
jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, -1, 20));
jPanel1.add(lname, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 370, 170, 20));
jPanel1.add(fname, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 290, 170, 20));
jPanel1.add(mname, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 330, 170, 20));
jLabel4.setText("First Name :");
jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 290, -1, 20));
jLabel5.setText("Grade :");
jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 370, -1, 20));
jPanel1.add(mtp, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 370, 170, 20));
jLabel6.setText("Address :");
jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 330, -1, 20));
jLabel7.setText("DOB :");
jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 290, -1, 20));
jLabel8.setText("E-mail :");
jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(880, 290, -1, 20));
jLabel9.setText("School :");
jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 330, -1, 20));
jPanel1.add(addr, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 330, 170, 20));
jPanel1.add(em, new org.netbeans.lib.awtextra.AbsoluteConstraints(960, 290, 170, 20));
jPanel1.add(sch, new org.netbeans.lib.awtextra.AbsoluteConstraints(680, 330, 170, 20));
jLabel10.setText("Fathers TP :");
jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 290, -1, 20));
jPanel1.add(ftp, new org.netbeans.lib.awtextra.AbsoluteConstraints(680, 290, 170, 20));
grd.setModel(new javax.swing.SpinnerNumberModel(0, 0, 11, 1));
grd.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
grdStateChanged(evt);
}
});
grd.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
grdInputMethodTextChanged(evt);
}
});
grd.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
grdKeyTyped(evt);
}
});
jPanel1.add(grd, new org.netbeans.lib.awtextra.AbsoluteConstraints(680, 370, 170, -1));
jPanel1.add(dob, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 290, 170, -1));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1250, 430));
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
private void grdStateChanged(javax.swing.event.ChangeEvent evt) {
}
private void grdInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
}
private void grdKeyTyped(java.awt.event.KeyEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
if(fieldCheck() && dateCheck()){
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
new Student().setVisible(true);
} catch (Exception e) {
}
}
});
}
private void updateTable() {
try {
ResultSet rs = DB.search("select * from stud");
DefaultTableModel dtm = (DefaultTableModel) stud.getModel();
Vector v = new Vector();
while (rs.next()) {
dtm.setRowCount(0);
v.add(rs.getString(1));
v.add(rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
v.add(rs.getString(5));
v.add(rs.getString(6));
v.add(rs.getString(7));
v.add(rs.getString(8));
v.add(rs.getString(9));
v.add(rs.getString(10));
v.add(rs.getString(11));
dtm.addRow(v);
}
} catch (Exception ex) {
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
}
}
private boolean fieldCheck() {
if(fname.getText().toString().isEmpty() ||
((JTextField)dob.getDateEditor().getUiComponent()).getText().toString().isEmpty() ||
addr.getText().toString().isEmpty() ||
sch.getText().toString().isEmpty() ||
gradeText.getText().isEmpty()) {
return false;
}else {
return true;
}
}
private boolean dateCheck() {
int daT = Integer.parseInt(((JTextField)dob.getDateEditor().getUiComponent()).getText().toString().replace("-", ""));
Calendar cal = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
String date = String.valueOf(cal.get(Calendar.YEAR))+String.valueOf(cal.get(Calendar.MONTH))+String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
int compDate = Integer.parseInt(date);
if(daT > compDate){
return false;
}else {
return true;
}
}
}
这是没有initcomponents的代码:
public class Student extends javax.swing.JFrame {
/**
* Creates new form Student
*/
static JFormattedTextField gradeText;
public Student() {
//Methods
initComponents();
KeyTypos();
updateTable();
//Frame settings
pack();
setLocationRelativeTo(null);
ImageIcon ic = new ImageIcon("src\\main\\java\\com\\cyrax\\images\\Icon.png");
this.setIconImage(ic.getImage());
//initializations
stud.clearSelection();
gradeText = ((JSpinner.DefaultEditor) grd.getEditor()).getTextField();
dob.setDateFormatString("DD-MM-YYYY");
}
public final void KeyTypos() {
try {
gradeText = ((JSpinner.DefaultEditor) grd.getEditor()).getTextField();
gradeText.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(c >= '0' & c <= '9') | c == e.VK_PERIOD) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (!gradeText.getText().toString().isEmpty()) {
int val = Integer.parseInt(gradeText.getText());
if (val > 11) {
String oldVal = gradeText.getText().toString();
char str[] = oldVal.toCharArray();
StringBuilder sb = new StringBuilder(oldVal);
sb.deleteCharAt(sb.length() - 1);
String newVal = sb.toString();
gradeText.setText(newVal);
}
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
try {
if(fieldCheck() && dateCheck()){
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
new Student().setVisible(true);
} catch (Exception e) {
}
}
});
}
private void updateTable() {
try {
ResultSet rs = DB.search("select * from stud");
DefaultTableModel dtm = (DefaultTableModel) stud.getModel();
Vector v = new Vector();
while (rs.next()) {
dtm.setRowCount(0);
v.add(rs.getString(1));
v.add(rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
v.add(rs.getString(5));
v.add(rs.getString(6));
v.add(rs.getString(7));
v.add(rs.getString(8));
v.add(rs.getString(9));
v.add(rs.getString(10));
v.add(rs.getString(11));
dtm.addRow(v);
}
} catch (Exception ex) {
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
}
}
private boolean fieldCheck() {
if(fname.getText().toString().isEmpty() ||
((JTextField)dob.getDateEditor().getUiComponent()).getText().toString().isEmpty() ||
addr.getText().toString().isEmpty() ||
sch.getText().toString().isEmpty() ||
gradeText.getText().isEmpty()) {
return false;
}else {
return true;
}
}
private boolean dateCheck() {
int daT = Integer.parseInt(((JTextField)dob.getDateEditor().getUiComponent()).getText().toString().replace("-", ""));
Calendar cal = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
String date = String.valueOf(cal.get(Calendar.YEAR))+String.valueOf(cal.get(Calendar.MONTH))+String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
int compDate = Integer.parseInt(date);
if(daT > compDate){
return false;
}else {
return true;
}
}
}
请帮我解决这个问题
谢谢