万年历部分代码,为什么会有异常,请指导,谢谢

时间:2022-11-03 14:32:09
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JTabbedPane;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class CalculatorFrame extends JFrame {
    JPanel contentPane;
    Border border1 = BorderFactory.createLineBorder(SystemColor.controlText, 2);
    Border border2 = BorderFactory.createLineBorder(SystemColor.controlText, 2);
    JTabbedPane jTabbedPane1 = new JTabbedPane();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    Border border3 = BorderFactory.createLineBorder(SystemColor.controlText, 2);
    Border border4 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
            new Color(248, 248, 248), new Color(121, 121, 121));
    JComboBox jComboBox1 = new JComboBox();
    JPanel jPanel4 = new JPanel();
    GridLayout gridLayout1 = new GridLayout(7,7);
    java.util.List linkedList = new LinkedList();
    JSpinner jSpinner1;
    JLabel[] jLabel = new JLabel[49];
    GregorianCalendar cal1 = new GregorianCalendar();
    int month = cal1.get(Calendar.MONTH);
    int year  = 2007;
    JButton jButton1 = new JButton();


    public CalculatorFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(543, 444));
        setTitle("Calculator");
        jTabbedPane1.setBounds(new Rectangle(19, 37, 494, 336));
        jPanel1.setLayout(null);
        jPanel2.setBorder(border4);
        jPanel2.setBounds(new Rectangle(15, 28, 269, 241));
        jPanel2.setLayout(null);
        jComboBox1.setBounds(new Rectangle(13, 21, 82, 30));
        jComboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jComboBox1_actionPerformed(e);
            }
        });
        for(int i=1980;i<3000;i++){
        linkedList.add(new Integer(i));
        }
        jSpinner1 = new JSpinner(new SpinnerListModel(linkedList));
        jSpinner1.setToolTipText("");
        jSpinner1.setBounds(new Rectangle(163, 21, 82, 30));
        jPanel4.setBackground(Color.white);
        jPanel4.setBounds(new Rectangle(13, 65, 232, 157));
        jPanel4.setLayout(gridLayout1);
        jButton1.setBounds(new Rectangle(350, 102, 75, 61));
        jButton1.setText("jButton1");
        contentPane.add(jTabbedPane1);
        jTabbedPane1.add(jPanel1, "时间和日期");
        for(int i = 1;i<=12;i++){
            jComboBox1.addItem(i+"月");
        }
        jLabel[0] = new JLabel("日");
        jPanel4.add(jLabel[0]);
        for(int i =1;i<7;i++){
            jLabel[i] = new JLabel(""+i);
            jPanel4.add(jLabel[i]);
        }
        for(int i =7 ;i<49;i++){
            jLabel[i] = new JLabel();
            jPanel4.add(jLabel[i]);
        }
        jPanel1.add(jPanel2);
        jPanel1.add(jButton1);
        jPanel2.add(jComboBox1);
        jPanel2.add(jSpinner1);
        jPanel2.add(jPanel4);
        getDate1();
    }
    public void getDate1(){
       
        int day = 0;
        int a = 1;

        GregorianCalendar cal = new GregorianCalendar(year,month,1);
        int date = cal.get(Calendar.DAY_OF_WEEK);
        for(int i = 7; i<date-1+7;i++){
            jLabel[i].setText("");
        }
        int mon = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        if(!cal.isLeapYear(year)){
            if(mon==1){
                    day = 28;
                }
        }
        else{
            if(mon==1){
                    day = 29;
            }
        }
        if(mon == 0||mon ==2||mon ==4||mon ==6||mon ==7||mon==9||mon==11){
            day = 31;
         }
        if(mon == 3||mon == 5||mon == 8||mon == 10){
             day = 30;
        }
        for(int i = date-1+7; i<day+date-1+7;i++){
            jLabel[i].setText(""+a);
            a++;
        }
    }

    public void jComboBox1_actionPerformed(ActionEvent e) {
        month = jComboBox1.getSelectedIndex();
      //  getDate1(); 不能调用,否则会错,有异常,为什么???
         System.out.print("12345"); //在未产生此事件时,都能执行???
    }
}

9 个解决方案

#1


郁闷,你代码又不全.想看看的,可是又要加,唉........

#2


以上代码是我全部代码,程序写到这我编译的时候就出了异常,感觉jComboBox1_actionPerformed
这个方法自动就被调用了,我还没选择任何item的时候,System.out.print("12345")都能够执行,
我用的是 jdk1.4,不知道怎么回事,请哪位老大细心看下吧,在下新手,拜托了,谢谢 

#3


  要想别人给你看代码 关键地方标注释 你这样一发 叫人怎么看啊 象那些生成的代码,看着觉得很累,看了一半 累死!你的分俺不想挣。。。

#4


注释标了,在jComboBox1_actionPerformed(ActionEvent e)里面,只是没有加个提醒色
其实只要看看这里面的代码就可以了,代码很简单

#5


for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 

这里导致了事件的发生

实例化所有的jLabel[]后再写上面那段代码 
如:
 /*for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 
*/
        jLabel[0] = new JLabel("日"); 
        jPanel4.add(jLabel[0]); 
        for(int i =1;i <7;i++){ 
            jLabel[i] = new JLabel(""+i); 
            jPanel4.add(jLabel[i]); 
        } 
        for(int i =7 ;i <49;i++){ 
            jLabel[i] = new JLabel(); 
            jPanel4.add(jLabel[i]); 
        } 
////写到这里来
         for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 

#6


for(int i = 1;i  <=12;i++){  
            jComboBox1.addItem(i+"月");  
        }  
这里只是往jComboBox1里面添加项目,怎么会产生时间呢?
txq你的方法试了,通不过

#7


可是 我这里改了后就没问题了啊

import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.*; 
import javax.swing.JTabbedPane; 
import javax.swing.BorderFactory; 
import javax.swing.border.Border; 
import javax.swing.*; 
import javax.swing.border.EtchedBorder; 
import java.util.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

class CalculatorFrame extends JFrame { 
    JPanel contentPane; 
    Border border1 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    Border border2 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    JTabbedPane jTabbedPane1 = new JTabbedPane(); 
    JPanel jPanel1 = new JPanel(); 
    JPanel jPanel2 = new JPanel(); 
    Border border3 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    Border border4 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, 
            new Color(248, 248, 248), new Color(121, 121, 121)); 
    JComboBox jComboBox1 = new JComboBox(); 
    JPanel jPanel4 = new JPanel(); 
    GridLayout gridLayout1 = new GridLayout(7,7); 
    java.util.List linkedList = new LinkedList(); 
    JSpinner jSpinner1; 
    JLabel[] jLabel = new JLabel[49]; 
    GregorianCalendar cal1 = new GregorianCalendar(); 
    int month = cal1.get(Calendar.MONTH); 
    int year  = 2007; 
    JButton jButton1 = new JButton(); 


    public CalculatorFrame() { 
        try { 
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            jbInit(); 
        } catch (Exception exception) { 
            exception.printStackTrace(); 
        } 
    } 

    /** 
     * Component initialization. 
     * 
     * @throws java.lang.Exception 
     */ 
    private void jbInit() throws Exception { 
        contentPane = (JPanel) getContentPane(); 
        contentPane.setLayout(null); 
        setSize(new Dimension(543, 444)); 
        setTitle("Calculator"); 
        jTabbedPane1.setBounds(new Rectangle(19, 37, 494, 336)); 
        jPanel1.setLayout(null); 
        jPanel2.setBorder(border4); 
        jPanel2.setBounds(new Rectangle(15, 28, 269, 241)); 
        jPanel2.setLayout(null); 
        jComboBox1.setBounds(new Rectangle(13, 21, 82, 30)); 
        jComboBox1.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                jComboBox1_actionPerformed(e); 
            } 
        }); 
        for(int i=1980;i <3000;i++){ 
        linkedList.add(new Integer(i)); 
        } 
        jSpinner1 = new JSpinner(new SpinnerListModel(linkedList)); 
        jSpinner1.setToolTipText(""); 
        jSpinner1.setBounds(new Rectangle(163, 21, 82, 30)); 
        jPanel4.setBackground(Color.white); 
        jPanel4.setBounds(new Rectangle(13, 65, 232, 157)); 
        jPanel4.setLayout(gridLayout1); 
        jButton1.setBounds(new Rectangle(350, 102, 75, 61)); 
        jButton1.setText("jButton1"); 
        contentPane.add(jTabbedPane1); 
        jTabbedPane1.add(jPanel1, "时间和日期"); 
        
        jLabel[0] = new JLabel("日"); 
        jPanel4.add(jLabel[0]); 
        for(int i =1;i <7;i++){ 
            jLabel[i] = new JLabel(""+i); 
            jPanel4.add(jLabel[i]); 
        } 
        for(int i =7 ;i <49;i++){ 
            jLabel[i] = new JLabel(); 
            jPanel4.add(jLabel[i]); 
        } 
         for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 
        jPanel1.add(jPanel2); 
        jPanel1.add(jButton1); 
        jPanel2.add(jComboBox1); 
        jPanel2.add(jSpinner1); 
        jPanel2.add(jPanel4); 
      
        getDate1(); 
    } 
    public void getDate1(){ 
        
        int day = 0; 
        int a = 1; 

        GregorianCalendar cal = new GregorianCalendar(year,month,1); 
        int date = cal.get(Calendar.DAY_OF_WEEK); 
        for(int i = 7; i <date-1+7;i++){ 
            jLabel[i].setText(""); 
        } 
        int mon = cal.get(Calendar.MONTH); 
        int year = cal.get(Calendar.YEAR); 
        if(!cal.isLeapYear(year)){ 
            if(mon==1){ 
                    day = 28; 
                } 
        } 
        else{ 
            if(mon==1){ 
                    day = 29; 
            } 
        } 
        if(mon == 0 ||mon ==2||mon ==4 ||mon ==6 ||mon ==7||mon==9 ||mon==11){ 
            day = 31; 
         } 
        if(mon == 3 ||mon == 5||mon == 8||mon == 10){ 
             day = 30; 
        } 
        for(int i = date-1+7; i <day+date-1+7;i++){ 
            jLabel[i].setText(""+a); 
            a++; 
        } 
    } 

    public void jComboBox1_actionPerformed(ActionEvent e) { 
        month = jComboBox1.getSelectedIndex(); 
        getDate1(); //不能调用,否则会错,有异常,为什么??? 
         System.out.println("12345"); //在未产生此事件时,都能执行??? 
    } 
    
    public static void main(String[] args){
        new CalculatorFrame().setVisible(true);
    }

#8


非常感谢小琴的帮助,你说的是对的,只要执行了jComboBox1.addItem(),就导致了actionPerformed,谢谢

#9


tangxiaoqin说的是触发事件,而不是产生时间。

另外你的那个getDate1写的也太复杂了吧,calendar都提供了你需要的那些信息吧。

#1


郁闷,你代码又不全.想看看的,可是又要加,唉........

#2


以上代码是我全部代码,程序写到这我编译的时候就出了异常,感觉jComboBox1_actionPerformed
这个方法自动就被调用了,我还没选择任何item的时候,System.out.print("12345")都能够执行,
我用的是 jdk1.4,不知道怎么回事,请哪位老大细心看下吧,在下新手,拜托了,谢谢 

#3


  要想别人给你看代码 关键地方标注释 你这样一发 叫人怎么看啊 象那些生成的代码,看着觉得很累,看了一半 累死!你的分俺不想挣。。。

#4


注释标了,在jComboBox1_actionPerformed(ActionEvent e)里面,只是没有加个提醒色
其实只要看看这里面的代码就可以了,代码很简单

#5


for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 

这里导致了事件的发生

实例化所有的jLabel[]后再写上面那段代码 
如:
 /*for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 
*/
        jLabel[0] = new JLabel("日"); 
        jPanel4.add(jLabel[0]); 
        for(int i =1;i <7;i++){ 
            jLabel[i] = new JLabel(""+i); 
            jPanel4.add(jLabel[i]); 
        } 
        for(int i =7 ;i <49;i++){ 
            jLabel[i] = new JLabel(); 
            jPanel4.add(jLabel[i]); 
        } 
////写到这里来
         for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 

#6


for(int i = 1;i  <=12;i++){  
            jComboBox1.addItem(i+"月");  
        }  
这里只是往jComboBox1里面添加项目,怎么会产生时间呢?
txq你的方法试了,通不过

#7


可是 我这里改了后就没问题了啊

import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.*; 
import javax.swing.JTabbedPane; 
import javax.swing.BorderFactory; 
import javax.swing.border.Border; 
import javax.swing.*; 
import javax.swing.border.EtchedBorder; 
import java.util.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

class CalculatorFrame extends JFrame { 
    JPanel contentPane; 
    Border border1 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    Border border2 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    JTabbedPane jTabbedPane1 = new JTabbedPane(); 
    JPanel jPanel1 = new JPanel(); 
    JPanel jPanel2 = new JPanel(); 
    Border border3 = BorderFactory.createLineBorder(SystemColor.controlText, 2); 
    Border border4 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, 
            new Color(248, 248, 248), new Color(121, 121, 121)); 
    JComboBox jComboBox1 = new JComboBox(); 
    JPanel jPanel4 = new JPanel(); 
    GridLayout gridLayout1 = new GridLayout(7,7); 
    java.util.List linkedList = new LinkedList(); 
    JSpinner jSpinner1; 
    JLabel[] jLabel = new JLabel[49]; 
    GregorianCalendar cal1 = new GregorianCalendar(); 
    int month = cal1.get(Calendar.MONTH); 
    int year  = 2007; 
    JButton jButton1 = new JButton(); 


    public CalculatorFrame() { 
        try { 
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            jbInit(); 
        } catch (Exception exception) { 
            exception.printStackTrace(); 
        } 
    } 

    /** 
     * Component initialization. 
     * 
     * @throws java.lang.Exception 
     */ 
    private void jbInit() throws Exception { 
        contentPane = (JPanel) getContentPane(); 
        contentPane.setLayout(null); 
        setSize(new Dimension(543, 444)); 
        setTitle("Calculator"); 
        jTabbedPane1.setBounds(new Rectangle(19, 37, 494, 336)); 
        jPanel1.setLayout(null); 
        jPanel2.setBorder(border4); 
        jPanel2.setBounds(new Rectangle(15, 28, 269, 241)); 
        jPanel2.setLayout(null); 
        jComboBox1.setBounds(new Rectangle(13, 21, 82, 30)); 
        jComboBox1.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                jComboBox1_actionPerformed(e); 
            } 
        }); 
        for(int i=1980;i <3000;i++){ 
        linkedList.add(new Integer(i)); 
        } 
        jSpinner1 = new JSpinner(new SpinnerListModel(linkedList)); 
        jSpinner1.setToolTipText(""); 
        jSpinner1.setBounds(new Rectangle(163, 21, 82, 30)); 
        jPanel4.setBackground(Color.white); 
        jPanel4.setBounds(new Rectangle(13, 65, 232, 157)); 
        jPanel4.setLayout(gridLayout1); 
        jButton1.setBounds(new Rectangle(350, 102, 75, 61)); 
        jButton1.setText("jButton1"); 
        contentPane.add(jTabbedPane1); 
        jTabbedPane1.add(jPanel1, "时间和日期"); 
        
        jLabel[0] = new JLabel("日"); 
        jPanel4.add(jLabel[0]); 
        for(int i =1;i <7;i++){ 
            jLabel[i] = new JLabel(""+i); 
            jPanel4.add(jLabel[i]); 
        } 
        for(int i =7 ;i <49;i++){ 
            jLabel[i] = new JLabel(); 
            jPanel4.add(jLabel[i]); 
        } 
         for(int i = 1;i <=12;i++){ 
            jComboBox1.addItem(i+"月"); 
        } 
        jPanel1.add(jPanel2); 
        jPanel1.add(jButton1); 
        jPanel2.add(jComboBox1); 
        jPanel2.add(jSpinner1); 
        jPanel2.add(jPanel4); 
      
        getDate1(); 
    } 
    public void getDate1(){ 
        
        int day = 0; 
        int a = 1; 

        GregorianCalendar cal = new GregorianCalendar(year,month,1); 
        int date = cal.get(Calendar.DAY_OF_WEEK); 
        for(int i = 7; i <date-1+7;i++){ 
            jLabel[i].setText(""); 
        } 
        int mon = cal.get(Calendar.MONTH); 
        int year = cal.get(Calendar.YEAR); 
        if(!cal.isLeapYear(year)){ 
            if(mon==1){ 
                    day = 28; 
                } 
        } 
        else{ 
            if(mon==1){ 
                    day = 29; 
            } 
        } 
        if(mon == 0 ||mon ==2||mon ==4 ||mon ==6 ||mon ==7||mon==9 ||mon==11){ 
            day = 31; 
         } 
        if(mon == 3 ||mon == 5||mon == 8||mon == 10){ 
             day = 30; 
        } 
        for(int i = date-1+7; i <day+date-1+7;i++){ 
            jLabel[i].setText(""+a); 
            a++; 
        } 
    } 

    public void jComboBox1_actionPerformed(ActionEvent e) { 
        month = jComboBox1.getSelectedIndex(); 
        getDate1(); //不能调用,否则会错,有异常,为什么??? 
         System.out.println("12345"); //在未产生此事件时,都能执行??? 
    } 
    
    public static void main(String[] args){
        new CalculatorFrame().setVisible(true);
    }

#8


非常感谢小琴的帮助,你说的是对的,只要执行了jComboBox1.addItem(),就导致了actionPerformed,谢谢

#9


tangxiaoqin说的是触发事件,而不是产生时间。

另外你的那个getDate1写的也太复杂了吧,calendar都提供了你需要的那些信息吧。