我试图获得4月份的日历,以便开始计算日期。到目前为止,我所拥有的是一个非常通用的日历,从网格的开始处开始。如何设置它以使日历与4月(星期二)的第一天相匹配。到目前为止,这是我的代码:
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);
}
}
答案 0 :(得分:2)
以下是一种方法,它使用构造函数中的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);
}
答案 1 :(得分:0)
尝试使用此代码查找当月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);
本月的输出
Tuesday
现在,根据weekDay
,您可以放置JLabels
。
示例代码:
在添加标题周名称之后但在添加实际日期之前添加以下代码。
for(String header:headers){
if(header.equals(weekDay)){
break;
}else{
calendar.add(new JLabel());
}
}