import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
public class r1 extends JFrame implements ItemListener{
/** Creates new form r1 */
public r1() {
initComponents();
}
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
y = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
m = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
d = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jLabel4.setText("\u51fa\u751f\u65e5\u671f:");
getContentPane().add(jLabel4);
for(int i=1980;i<2022;i++){
y.addItem(i);
}
getContentPane().add(y);
jLabel1.setText("\u5e74");
getContentPane().add(jLabel1);
for(int i=1;i<13;i++){
m.addItem(i);
}
getContentPane().add(m);
jLabel2.setText("\u6708");
getContentPane().add(jLabel2);
for(int i=1;i<32;i++){
d.addItem(i);
}
getContentPane().add(d);
jLabel3.setText("\u65e5");
getContentPane().add(jLabel3);
m.addItemListener(this);
y.addItemListener(this);
this.setTitle("出生日期");
this.setLocation(200,200);
this.setPreferredSize(new Dimension(400,180));
pack();
}// </editor-fold>
//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new r1().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JComboBox d;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JComboBox m;
private javax.swing.JComboBox y;
public void itemStateChanged(ItemEvent e) {
int mm=0,mmm=30,yy=0;
mm=Integer.parseInt(m.getSelectedItem().toString());
yy=Integer.parseInt(y.getSelectedItem().toString());
switch(mm){
case 4:
case 6:
case 9:
case 11:
d.remove(mmm);
break;
case 2:
if(yy%4==0 && yy%100!=0 || yy%400==0){
d.remove(mmm);
d.remove(mmm-1);
}else{
d.remove(mmm);
d.remove(mmm-1);
d.remove(mmm-2);
}
}
}
}
运行选择月份时,报错,数组下标越界,求解
这是最终效果图