對於Java(和編程)來說,這很新穎。試圖找出如何使Jpanel的大小相對於JFrame的大小。我只是想創建一個日曆,我有三個不同的面板:一個用於月份標題;一個星期的一天;另一個是日曆主體。如何使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);
}
而這裏的結果:
我該如何解決這個問題?例如,假設我想讓框架面板的80%和其他兩個10%。
另請考慮[tag:jcalendar]。 – trashgod
@trashgod:謝謝。添加回答。 –