| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 855 人关注过本帖
标题:求助,求助!
只看楼主 加入收藏
tntchenmingye
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-12-10
收藏
 问题点数:0 回复次数:2 
求助,求助!
想弄个日历,编出来以后怎么弄都没搞定。
编译能通过但按钮不好使,还有Frame中panel4怎么总是盖在其他panel上面呢???
日历头为什么不能显示月份和年份,而是显示“null 0”呢?
第110行的  int year = Integer.parseInt(tfYear.getText()); 应该放在哪呢,我是在没想明白放在哪,随便放的.
我感觉逻辑混乱,结构有问题。
请大虾们帮帮忙,谢谢

代码如下:

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;

public class MyCalendar{
    int year , month;
    String output="";
    JFrame myFrame;
    JPanel panel1 , panel2 , panel3 , panel4;
    JRadioButton B1 , B2 , B3 , B4 , B5 , B6 , B7 , B8 , B9 , B10 , B11 , B12;
    JLabel labelYear,label1;
    JTextArea jtaCalendar;
    JTextField tfYear;
    ButtonGroup group;
   
    public MyCalendar(){
        myFrame = new JFrame("日历");
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();        
        
        label1 = new JLabel("              注:请先输入年份,然后选择月份");
        labelYear = new JLabel("输入年份");
        jtaCalendar = new JTextArea();
        tfYear = new JTextField(4);
        
        group = new ButtonGroup();        
        B1 = new JRadioButton("一  月");
        B2 = new JRadioButton("二  月");
        B3 = new JRadioButton("三  月");
        B4 = new JRadioButton("四  月");
        B5 = new JRadioButton("五  月");
        B6 = new JRadioButton("六  月");
        B7 = new JRadioButton("七  月");
        B8 = new JRadioButton("八  月");
        B9 = new JRadioButton("九  月");
        B10 = new JRadioButton("十  月");
        B11 = new JRadioButton("十一月");
        B12 = new JRadioButton("十二月");
        group.add(B1);
        group.add(B2);
        group.add(B3);
        group.add(B4);
        group.add(B5);
        group.add(B6);
        group.add(B7);
        group.add(B8);
        group.add(B9);
        group.add(B10);
        group.add(B11);
        group.add(B12);
        
        B1.addActionListener(new myListener());
        B2.addActionListener(new myListener());
        B3.addActionListener(new myListener());
        B4.addActionListener(new myListener());
        B5.addActionListener(new myListener());
        B6.addActionListener(new myListener());
        B7.addActionListener(new myListener());
        B8.addActionListener(new myListener());
        B9.addActionListener(new myListener());
        B10.addActionListener(new myListener());
        B11.addActionListener(new myListener());
        B12.addActionListener(new myListener());   
        
        tfYear.setEnabled(true);
        panel1.setLayout(new FlowLayout());
        panel1.add(labelYear);
        panel1.add(tfYear);
        
        panel2.setLayout(new GridLayout(2,6,2,2));
        panel2.add(B1);
        panel2.add(B2);
        panel2.add(B3);
        panel2.add(B4);
        panel2.add(B5);
        panel2.add(B6);
        panel2.add(B7);
        panel2.add(B8);
        panel2.add(B9);
        panel2.add(B10);
        panel2.add(B11);
        panel2.add(B12);
        
        panel3.setLayout(new GridLayout(1,2,2,2));        
        panel3.add(panel1);
        panel3.add(panel2);
        
        panel4.add(jtaCalendar);  
              
        myFrame.getContentPane().add(label1,BorderLayout.NORTH);
        myFrame.getContentPane().add(panel3,BorderLayout.CENTER);
        myFrame.getContentPane().add(panel4,BorderLayout.SOUTH);         
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack();
        myFrame.setResizable(true);
        myFrame.setVisible(true);
        
        printMonthBody();
    }
    public static void main(String s[]){
        new MyCalendar();      
    }
   
  //监听器类  
  class myListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
         int year = Integer.parseInt(tfYear.getText());
         try{
             if(e.getSource() == B1)
                 month = 1;
             if(e.getSource() == B2)
                 month = 2;
             if(e.getSource() == B3)
                 month = 3;
             if(e.getSource() == B4)
                 month = 4;
             if(e.getSource() == B5)
                 month = 5;
             if(e.getSource() == B6)
                 month = 6;
             if(e.getSource() == B7)
                 month = 7;
             if(e.getSource() == B8)
                 month = 8;
             if(e.getSource() == B9)
                 month = 9;
             if(e.getSource() == B10)
                 month = 10;
             if(e.getSource() == B11)
                 month = 11;
             if(e.getSource() == B12)
                 month = 12;
         }
         catch(NumberFormatException e1){
             System.out.print("Error");            
       }         
    }
}
  
             //获取月份名称   
             String getMonthName(){
                 String monthName = null;
                 switch(month){
                     case 1: monthName = "一    月"; break;   
                     case 2: monthName = "二    月"; break;
                     case 3: monthName = "三    月"; break;
                     case 4: monthName = "四    月"; break;
                     case 5: monthName = "五    月"; break;
                     case 6: monthName = "六    月"; break;
                     case 7: monthName = "七    月"; break;
                     case 8: monthName = "八    月"; break;
                     case 9: monthName = "九    月"; break;
                     case 10: monthName = "十    月"; break;
                     case 11: monthName = "十 一 月"; break;
                     case 12: monthName = "十 二 月"; break;
                 }
                 return monthName;
             }
            
             void printMonthBody(){
                 output = "  " + getMonthName() + "   " + year + "\n";
                 output += "- - - - - - - - - - - - - - - - - - - - - - - - - - - - -" + "\n";
                 output += "日                                    六" + "\n";
                 int startDay = getStartDay();
                 int daysInMonth = getDaysInMonth();
                 for(int i = 0;i < startDay;i++)
                     output += "  ";
                 for(int j = 1;j <= daysInMonth;j++){
                     if(j < 10)
                         output += "  " + j;
                     else
                         output += " " + j;
                     if((j + startDay) % 7 == 0)
                         output += "\n";
                 }
                 output += "\n";
                  jtaCalendar.setText(output);  
             }
            
             int getStartDay(){
                 int startDay1800 = 3;              //1800年1月1日是星期三
                 int totalDays = getTotalDays();
                 return (totalDays + startDay1800)%7;
             }
            
             int getTotalDays(){
                 int total = 0;
                 
                 for(int i = 1800;i < year;i++){
                     if(isLeapYear(i))
                         total = total + 366;
                     else
                         total = total + 365;
                 }
                 for(int i = 1;i < month;i++)
                     total = total + getDaysInMonth();
                 
                     return total;   
             }

             int getDaysInMonth(){
                 if(month == 1 || month == 3 || month == 5 || month == 7||
                    month == 8 || month == 10 || month == 12)
                    return 31;
                 if(month == 4 || month == 6 || month == 9 || month == 11)
                    return 30;
                 if(month == 2)
                    return isLeapYear(year)? 29 : 28;  
                    
                    return 0;               
             }
             boolean isLeapYear(int year){
                 return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
             }         
}
搜索更多相关主题的帖子: import java awt int 日历 
2008-03-24 22:35
tntchenmingye
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-12-10
收藏
得分:0 
有个地方发失误了,改
98行 是      myFrame.setResizable(false);    不是true
2008-03-24 22:37
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你的布局真够头疼的。。呵呵。。建议你自己先在纸上画一下。。然后在布局。。。。

学习需要安静。。海盗要重新来过。。
2008-03-24 23:21
快速回复:求助,求助!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056588 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved