当前位置: 代码迷 >> Java相关 >> 求助:一个布局的问题
  详细解决方案

求助:一个布局的问题

热度:464   发布时间:2008-03-26 19:15:07.0
求助:一个布局的问题
功能已经实现,但是输入年份之后点击按钮,TextArea总是与按钮挤在一起,覆盖在按钮之上,调试很久都没成功,希望那个高手帮忙看一下,非常感谢
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import java.util.*;

public class MyCalendar1 extends JFrame{
    Calendar calendar;
    int year , month, startDay;
    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;
    int[] daysInMonth = new int[12];   

   
    public MyCalendar1(){
      
        super("日历");
        calendar = Calendar.getInstance();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();      
        
        label1 = new JLabel("注:请先输入年份,然后选择月份");
        labelYear = new JLabel("输入年份");
        
        tfYear = new JTextField(8);
        tfYear.setEnabled(true);
        panel1.setLayout(new FlowLayout());
        panel1.add(label1);
        panel1.add(labelYear);
        panel1.add(tfYear);
        
        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());   
        
        
        
        panel2.setLayout(new GridLayout(2,6,0,0));
        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 BoxLayout(panel3,BoxLayout.Y_AXIS));        
        panel3.add(panel1);
        panel3.add(panel2);
        
       // panel4.setLayout(new FlowLayout());
        jtaCalendar = new JTextArea();
        this.CalendarShow();
        jtaCalendar.setEditable(false);
        
        panel4.add(jtaCalendar);
        
         
        
        Container cp=getContentPane();
        cp.add(panel3,BorderLayout.NORTH);
        cp.add(panel4,BorderLayout.SOUTH);         
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.pack();
        this.setResizable(false);
        this.setVisible(true);
        this.setSize(400,300);
        
    }
    public static void main(String s[]){
        new MyCalendar1();      
    }
   
  //监听器类  
  class myListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
         int year = Integer.parseInt(tfYear.getText().trim());
         if(isLeapYear(year))
             daysInMonth = new int[]{31,29,31,30,31,30,31,31,30,31,30,31};
         else
             daysInMonth = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
         try{
             if(e.getSource() == B1){
                 month = 0;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.JANUARY);
             }   
             else if(e.getSource() == B2){
                 month = 1;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.FEBRUARY);
             }     
             else if(e.getSource() == B3){
                 month = 2;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.MARCH);
             }
             else if(e.getSource() == B4){        
                 month = 3;   
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.APRIL);
             }                 
             else if(e.getSource() == B5){
                 month = 4;   
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.MAY);
             }                     
             else if(e.getSource() == B6){
                 month = 5;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.JUNE);
             }                 
             else if(e.getSource() == B7){
                 month = 6;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.JULY);
             }                     
             else if(e.getSource() == B8){
                 month = 7;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.AUGUST);
             }                 
             else if(e.getSource() == B9){
                 month = 8;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.SEPTEMBER);
             }                    
             else if(e.getSource() == B10){
                 month = 9;   
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.OCTOBER);
             }                     
             else if(e.getSource() == B11){
                 month = 10;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.NOVEMBER);                  
             }      
             else if(e.getSource() == B12){
                 month = 11;
                 calendar.set(Calendar.YEAR,year);
                 calendar.set(Calendar.MONTH,Calendar.DECEMBER);
             }                  
             printMonthBody();
         }
         catch(NumberFormatException e1){
             System.out.print("Error");            
       }         
    }
  
             //获取月份名称   
             String getMonthName(){
                 String monthName = null;
                 switch(month){
                     case 0: monthName = "一    月"; break;   
                     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;
                 }
                 return monthName;
             }

            
             void printMonthBody(){   
                 calendar.set(Calendar.DAY_OF_MONTH,1);
                 int startDay = calendar.get(Calendar.DAY_OF_WEEK);
                 int year = Integer.parseInt(tfYear.getText().trim());            
                 output = "                                 " + getMonthName() + "                     " + year + "年" + "\n";
                 output += "  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" + "\n";
                 output += "    星期日    星期一    星期二    星期三    星期四    星期五    星期六" + "\n";
                                          
                 for(int i = 0;i < startDay - 1;i++)
                     output += "                ";
                 for(int j = 1;j <= daysInMonth[month];j++){
                     if(j < 10)
                         output += "             " + j;
                     else
                         output += "           " + j;
                     if((j + startDay - 1) % 7 == 0)
                         output += "\n";
                 }
                 output += "\n";
                 jtaCalendar.setText(output);  
             }

            
             boolean isLeapYear(int temp_year){
                 int year = Integer.parseInt(tfYear.getText().trim());
                 return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
             }         
      }
      void  CalendarShow()
      {
                 output = "                                                                                                      "+"\n";
                 output += "  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" + "\n";
                 output += "    星期日    星期一    星期二    星期三    星期四    星期五    星期六" + "\n";
                   output += "                                                                                                       "+"\n";
                 output += "                                                                                                       "+"\n";
                   output += "                                                                                                       "+"\n";
                   output += "                                                                                                       "+"\n";
                   output += "                                                                                                       "+"\n";
                   output += "                                                                                                       "+"\n";
                   
                   jtaCalendar.setText(output);    
          
          
          
          
          
          
          }
}
我只改个大概..可能和你一开始的时候JTextArea没有内容有关系...
本帖最近评分记录

学习需要安静。。海盗要重新来过。。
2008-03-26 13:20:29
tntchenmingye

等 级:新手上路
帖 子:13
专家分:0
注 册:2007-12-10
  得分:0 
回复 2# 的帖子
牛!!!谢谢!!!发自肺腑的!!!
----------------解决方案--------------------------------------------------------
  相关解决方案