sách gpt4 ai đã đi

java - 如何使 JPanel 大小相对于 JFrame 大小

In lại 作者:行者123 更新时间:2023-11-29 07:07:04 26 4
mua khóa gpt4 Nike

我是 Java(和编程)的新手。试图弄清楚如何使 Jpanel 大小相对于 JFrame 大小。我只是想创建一个日历,我有三个不同的面板:一个用于月份标题;一个星期几;另一个用于日历主体。

Đây là mã của tôi:

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

}

Sau đây là kết quả:

JFrame Calendar
(来源:staticflickr.com)

我该如何解决这个问题?例如,假设我想让车身面板占框架的 80%,另外两个面板各占 10%。

1 Câu trả lời

不要担心百分比,而是根据它们的大小和文本大小创建适合的组件。使用布局管理器的适当组合将很容易让你做到这一点。例如,整体布局可以是 BorderLayout,日历放置在 BorderLayout.CENTER 中。顶部的两个组件可以放在使用 BoxLayout 的 JPanel 中,也可以放在主 JPanel BorderLayout.PAGE_START 中。

如果您绝对需要使用百分比,那么您可能必须创建自己的布局管理器,这在您的阶段可能有点棘手,但同样如果绝对需要,也不是太难。

另外,正如 trashgod 所指出的,请考虑仅使用专为此目的而构建的工具,JCalendar .

关于java - 如何使 JPanel 大小相对于 JFrame 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18515876/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com