公历日历GUI - 问题

时间:2012-07-04 02:10:02

标签: java swing user-interface gregorian-calendar

我必须为我的Java类创建一个完整的函数日历,我被困住了。我使用GregorianCalendar来获取当前日期,月份,年份等。我正在使用JButton来填充填写日历的42个字段,并在每个字段上发布每个日期个人JButton。我遇到问题,让每个月的第一天(7月)落入正确的JButton。日历将月份识别为7月,将年份识别为2012年,但是它将在周一而不是在星期日进行,并且在一周内关闭。这是我的代码,感谢您的帮助!

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Calendar extends JFrame {

    public Calendar() {
        this.setSize(800, 600);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.setTitle("Calendar");

        JPanel borderPanel = new JPanel();
        borderPanel.setLayout(new BorderLayout());
        getContentPane().add(borderPanel);

        // Initialize GregorianCalendar
        GregorianCalendar cal = new GregorianCalendar();
        int currYear = cal.get(GregorianCalendar.YEAR);
        int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH);
        int currMnth = cal.get(GregorianCalendar.MONTH);
        String month2 = "";
        switch (currMnth) {
            case 0:
                month2 = "January";
                break;
            case 1:
                month2 = "February";
                break;
            case 2:
                month2 = "March";
                break;
            case 3:
                month2 = "April";
                break;
            case 4:
                month2 = "May";
                break;
            case 5:
                month2 = "June";
                break;
            case 6:
                month2 = "July";
                break;
            case 7:
                month2 = "August";
                break;
            case 8:
                month2 = "September";
                break;
            case 9:
                month2 = "October";
                break;
            case 10:
                month2 = "November";
                break;
            case 11:
                month2 = "December";
                break;
        }
        int nod, som;
        nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
        som = cal.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH); // Start of the Month

        // Button Layout
        final int ROWS = 6;
        final int COLS = 7;
        JButton[][] days;
        JPanel calendar = new JPanel();
        calendar.setLayout(new GridLayout(6, 7));
        days = new JButton[ROWS][COLS];
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                days[row][col] = new JButton("");
                calendar.add(days[row][col]);
            }
        }

        // Add Some Number to the Buttons
        int[] tableMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int startDay = som;
        int daysInMonth = tableMonth[currMnth];
        for (int i = 0; i < daysInMonth; i++) {
            days[(startDay + i) / 7][(startDay + i) % 7].setText("" + (i + 1));
        }
        borderPanel.add(calendar, BorderLayout.CENTER);

        // North Panel - Current Month / Prev & Next Button
        JPanel month = new JPanel();
        month.add(new JButton("Previous"));
        month.add(new JLabel(month2));
        month.add(new JButton("Next"));
        borderPanel.add(month, BorderLayout.NORTH);

        // West Panel - Current Date
        JPanel year = new JPanel();
        year.add(new JButton("The Year is: " + currYear));
        borderPanel.add(year, BorderLayout.SOUTH);

        // South Panel - Current Year
        JPanel today = new JPanel();
        today.add(new JButton("Today is: " + month2 + " " + currDOM + ", " + currYear));
        borderPanel.add(today, BorderLayout.WEST);

    }

    public static void main(String[] args) {
        new Calendar();
    }
}

3 个答案:

答案 0 :(得分:2)

我将解决程序的逻辑部分与GUI部分分开,然后使用逻辑部分。我会使用SimpleDateFormat来获取我的特殊日历字符串,例如字符串的月份和星期几。例如:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MyCalendar {
   private static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("MMMM");
   private static final SimpleDateFormat DAY_OF_WEEK_FORMAT = new SimpleDateFormat("EEEE");
   private static final SimpleDateFormat MY_DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");

   public static void main(String[] args) {


      GregorianCalendar cal = new GregorianCalendar(2012, Calendar.JANUARY, 1);
      for (int j = 0; j < 24; j++) {
         Date date = cal.getTime();
         System.out.println("Date: " + MY_DATE_FORMAT.format(date));
         int year = cal.get(Calendar.YEAR);
         System.out.println("year := " + year);
         int month = cal.get(Calendar.MONTH);
         System.out.printf("month number: %d, month name: %s%n", month, MONTH_FORMAT.format(date));
         int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
         System.out.printf("day of week: %d, day of week string: %s%n", dayOfWeek, DAY_OF_WEEK_FORMAT.format(date));

         int maxDaysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
         System.out.println("maxDaysInMonth := " + maxDaysInMonth);
         cal.add(Calendar.MONTH, 1);
         System.out.println();
      }

      System.out.println("Today!");
      cal = new GregorianCalendar(); // today!
      Date date = cal.getTime();
      System.out.println("Date: " + MY_DATE_FORMAT.format(date));
      int year = cal.get(Calendar.YEAR);
      System.out.println("year := " + year);
      int month = cal.get(Calendar.MONTH);
      System.out.printf("month number: %d, month name: %s%n", month, MONTH_FORMAT.format(date));
      int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
      System.out.printf("day of week: %d, day of week string: %s%n", dayOfWeek, DAY_OF_WEEK_FORMAT.format(date));

      int maxDaysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
      System.out.println("maxDaysInMonth := " + maxDaysInMonth);
      cal.add(Calendar.MONTH, 1);


   }
}

答案 1 :(得分:2)

除了@ HFOE的出色指导外,以下建议可能会有所帮助:

  • 使用符合您要求的最高抽象级别。

    public static void main(String[] args) {
    
        Calendar cal = Calendar.getInstance();
        cal.set(2012, Calendar.JANUARY, 1);
        for (int j = 0; j < 24; j++) {
        ...
        }
    } 
    
  • 构建代码,以模块化方式构建组件和面板,如herehere所示。

  • 使用event dispatch thread

答案 2 :(得分:0)

好的,一切正常,但我被困在Prev和Next按钮上。我有ActionListener设置,当我按下按钮时,它将分别打印到我的控制台+1或-1,但日历不刷新。我不知道我需要做些什么来让这部分工作得太好。这就是我现在拥有的......

公共类Calendar扩展了JFrame {

static JButton btnNext, btnPrev;
static int currYear, currDOM, currentMonth;

public Calendar() {
    this.setSize(800,600);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setVisible(true);
    this.setTitle("Calendar");


    JPanel borderPanel = new JPanel();
    borderPanel.setLayout(new BorderLayout());
    getContentPane().add(borderPanel);

    // Initialize GregorianCalendar
    GregorianCalendar cal = new GregorianCalendar();
    int currYear = cal.get(GregorianCalendar.YEAR);
    int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH);
    int currMnth = cal.get(GregorianCalendar.MONTH);
    currentMonth = currMnth;
    String StringMnth = "";
        switch(currMnth) {
        case 0: StringMnth = "January"; break;
        case 1: StringMnth = "February"; break;
        case 2: StringMnth = "March"; break;
        case 3: StringMnth = "April"; break;
        case 4: StringMnth = "May"; break;
        case 5: StringMnth = "June"; break;
        case 6: StringMnth = "July"; break;
        case 7: StringMnth = "August"; break;
        case 8: StringMnth = "September"; break;
        case 9: StringMnth = "October"; break;
        case 10: StringMnth = "November"; break;
        case 11: StringMnth = "December"; break;
    }
    int nod, som;
    cal.set(GregorianCalendar.DATE, 1);
    nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
    som = (cal.get(GregorianCalendar.DAY_OF_WEEK) - GregorianCalendar.SUNDAY + 7)%7; // Start of the Month

    // Button Layout
    final int ROWS = 6;
    final int COLS = 7;
    JButton[][] days;
    JPanel calendar = new JPanel();
    calendar.setLayout(new GridLayout(6,7));
    days = new JButton[ROWS][COLS];
    for(int row = 0; row < ROWS; row++) {
        for(int col = 0; col < COLS; col++) {
            days[row][col] = new JButton("");
            calendar.add(days[row][col]);
        }
    }

    // Add Some Number to the Buttons
    int[] tableMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    int startDay = som;
    int daysInMonth = tableMonth[currMnth];
    for(int i = 0; i < daysInMonth; i++){
        days[(startDay + i)/7][(startDay + i) % 7].setText("" + (i + 1));
    }
    borderPanel.add(calendar, BorderLayout.CENTER);

    // North Panel - Current Month / Prev & Next Button
    btnNext = new JButton(">>");
    btnPrev = new JButton("<<");
    JPanel month = new JPanel();    
    month.add(btnPrev);
    month.add(new JLabel(StringMnth));
    month.add(btnNext);
    borderPanel.add(month, BorderLayout.NORTH);

    //Listeners
    btnPrev.addActionListener(new btnPrev_Action());
    btnNext.addActionListener(new btnNext_Action());

    // West Panel - Current Date
    JPanel year = new JPanel();
    year.add(new JButton("The Year is: " + currYear ));
    borderPanel.add(year, BorderLayout.SOUTH);

    // South Panel - Current Year
    JPanel today = new JPanel();
    today.add(new JButton("Today is: " + StringMnth + " " + currDOM + ", " + currYear));
    borderPanel.add(today, BorderLayout.WEST);

}

static class btnNext_Action implements ActionListener{
    public void actionPerformed (ActionEvent e){
        currentMonth += 1;
        System.out.print(currentMonth);
    }
}

static class btnPrev_Action implements ActionListener{
    public void actionPerformed (ActionEvent e){
        currentMonth -= 1;
        System.out.print(currentMonth);
    }
}
public static void main(String[] args) {
    new Calendar();
}

}