我对 Java(和编程)很陌生。试图弄清楚如何使 Jpanel 大小相对于 JFrame 大小。我只是想创建一个日历,我有三个不同的面板:一个用于月份标题;一周中的几天;另一个用于日历主体。
这是我的代码:
public DisplayACalendar() {
setLayout(new GridLayout(3, 1));
// create JPanel for month/year
JPanel monthTitle = new JPanel();
monthTitle.add(new JLabel(month + " " + year));
// create JPanel for Days of Week
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
JPanel daysOfWeekPanel = new JPanel(new GridLayout(1, 7));
for (int i = 0; i < 7; i++) {
daysOfWeekPanel.add(new JLabel(days[i]));
}
// Create JPanel for calendar dates
JPanel calendarMonth = new JPanel(new GridLayout(6, 7));
for (int i = 1; i < firstDay; i++)
calendarMonth.add(new JLabel());
for (int i = 1; i <= daysInMonth; i++) {
JLabel jlblDayCell = new DayCell(i);
jlblDayCell.setHorizontalAlignment(JLabel.RIGHT);
calendarMonth.add(jlblDayCell);
}
int daysLeft = 42 - daysInMonth - firstDay;
for (int i = 0; i < daysLeft; i++)
calendarMonth.add(new JLabel());
add(monthTitle);
add(daysOfWeekPanel);
add(calendarMonth);
}
结果如下:
(来源:staticflickr.com)
我该如何解决?例如,假设我想让车身面板占框架的 80%,另外两个各占 10%。