简体   繁体   English

Java日历网格布局

[英]Java calendar grid layout

I'm trying to get the month of April calendar to get the day count started. 我正在尝试获取4月的日历,以开始计算日期。 So far all I have is a pretty generic calendar that starts with one at the beggining of the grid. 到目前为止,我所拥有的只是一个相当通用的日历,它以网格开始时的日历开始。 How do I set it up so that the calendar matches the first day of the April (Tuesday). 如何设置日历,使其与四月的第一天(星期二)匹配。 Here is my code so far: 到目前为止,这是我的代码:

package Exercise15_5;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Exercise15_5 extends JFrame {
    public Exercise15_5(){
        //Create panel with gridlayout
        JPanel calendar = new JPanel(new BorderLayout());
        calendar.setLayout(new GridLayout(6,7));
        //Add headers
        String[] headers = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        for(int i = 0; i <7; i++){
            calendar.add(new JLabel("" + headers[i]));
        }
        // Add days to calendar, use String.valueOf
        Calendar calendar1 = new GregorianCalendar(2014,4, 13);
        for (int i = 1; i < 31; i++) {
          calendar.add(new JLabel(String.valueOf(i)));
        }

        JPanel monthHeader = new JPanel(new BorderLayout());
        monthHeader.add(new JTextField("\t\t\t04/2014"),    BorderLayout.NORTH);

        monthHeader.add(calendar, BorderLayout.CENTER);



        add(monthHeader);
    }

    public static void main(String[] args) {
        Exercise15_5 frame = new Exercise15_5();
        frame.setTitle("Exercise 15_5");
        frame.setSize(600,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    }

}

Here is one approach, it uses the this call in a constructor (to default the date), it uses Calendar and it uses the correct GridLayout to be dynamic - 这是一种方法,它在构造函数中使用this调用(默认为日期),它使用Calendar并使用正确的GridLayout进行动态处理-

public Exercise15_5() {
  this(new java.util.Date()); // <-- default to the current Date.
}

public Exercise15_5(java.util.Date date) {
  // Create panel with gridlayout
  JPanel calendar = new JPanel(new GridLayout(0, 7)); // <-- Use the Layout ONCE
  // Add headers
  String[] headers = { "Sunday", "Monday",
      "Tuesday", "Wednesday", "Thursday", "Friday",
      "Saturday" };
  // Just use for-each on the headers.
  for (String header : headers) {
    calendar.add(new JLabel(header));
  }

  Calendar c = Calendar.getInstance();
  c.setTime(date); // <-- EDIT: Set the calendar to the passed in date!
  c.set(Calendar.DAY_OF_MONTH, 1);
  // EDIT: fixes flaw with start date.
  int skipDays = 0;
  switch (c.get(Calendar.DAY_OF_WEEK)) {
  case Calendar.MONDAY:
    skipDays = 1;
    break;
  case Calendar.TUESDAY:
    skipDays = 2;
    break;
  case Calendar.WEDNESDAY:
    skipDays = 3;
    break;
  case Calendar.THURSDAY:
    skipDays = 4;
    break;
  case Calendar.FRIDAY:
    skipDays = 5;
    break;
  case Calendar.SATURDAY:
    skipDays = 6;
    break;
  default:
  }
  for (int i = 0; i < skipDays; i++) {
    calendar.add(new JLabel(""));
  }
  int maxDay = c.getMaximum(Calendar.DAY_OF_MONTH);
  for (int i = 1; i < maxDay; i++) {
    calendar.add(new JLabel(String.valueOf(i)));
  }

  JPanel monthHeader = new JPanel(new BorderLayout());
  int mm = 1 + c.get(Calendar.MONTH);
  int year = c.get(Calendar.YEAR);
  monthHeader.add(
      new JTextField(String.format("\t\t\t%d/%d",
          mm, year)), BorderLayout.NORTH);

  monthHeader.add(calendar, BorderLayout.CENTER);

  add(monthHeader);
}

Try this code to find out the week day of 1st of current month. 尝试使用此代码找出当前月1日的工作日。

    SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);

    Calendar calendar12 = Calendar.getInstance();
    calendar12.set(Calendar.DAY_OF_MONTH, 1);
    String weekDay = dayFormat.format(calendar12.getTime());
    System.out.println(weekDay);

Output for this month 本月的输出

Tuesday

Now on the basis of weekDay you can place your JLabels . 现在的基础上, weekDay ,你可以把你JLabels


Sample code: 样例代码:

add below code after adding header week names but before adding actual dates . 在添加标头周名称之后但在添加实际日期之后添加以下代码。

    for(String header:headers){
        if(header.equals(weekDay)){
           break; 
        }else{
            calendar.add(new JLabel());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM