gpt4 book ai didi

Java 日历网格布局

转载 作者:行者123 更新时间:2023-11-30 03:56:32 24 4
gpt4 key购买 nike

我正在尝试获取四月份的日历来开始计算天数。到目前为止,我所拥有的只是一个非常通用的日历,该日历从网格开头的日历开始。我如何设置才能使日历与四月的第一天(星期二)相匹配。这是到目前为止我的代码:

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);


}

}

最佳答案

这是一种方法,它在构造函数中使用 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);
}

关于Java 日历网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com