java编写应用程序,有一个窗口,该窗口的布局为BorderLayout布局。窗口的中心添加一个JPanel容器pCenter,pCenter的布局是7行7列的GriderLayout布局,pCenter中放置49个标签,用来显示日历。窗口的上面添加一个JPane容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth。单击后分别显示下一月和上一月的日历。窗口下面添加一个JPanel容器pSouth,其布局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息。界面如下图所示。
时间: 2023-11-28 19:47:13 浏览: 211
java界面布局—日历
这是一个比较典型的Java GUI程序的设计,需要使用Swing组件进行实现。以下是代码示例:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CalendarGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel pCenter, pNorth, pSouth;
private JLabel[] days;
private JLabel infoLabel;
private JButton nextMonthButton, previousMonthButton;
private int currentMonth, currentYear;
public CalendarGUI() {
super("Calendar");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
// Center panel
pCenter = new JPanel(new GridLayout(7, 7));
days = new JLabel[49];
for (int i = 0; i < 49; i++) {
days[i] = new JLabel("", SwingConstants.CENTER);
days[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
if (i < 7) {
days[i].setText(getWeekday(i));
days[i].setForeground(Color.BLUE);
} else {
days[i].setFont(new Font("Arial", Font.PLAIN, 16));
}
pCenter.add(days[i]);
}
displayCalendar(2021, 9);
// North panel
pNorth = new JPanel(new FlowLayout());
nextMonthButton = new JButton("Next Month");
nextMonthButton.addActionListener(e -> {
if (currentMonth == 12) {
displayCalendar(currentYear + 1, 1);
} else {
displayCalendar(currentYear, currentMonth + 1);
}
});
previousMonthButton = new JButton("Previous Month");
previousMonthButton.addActionListener(e -> {
if (currentMonth == 1) {
displayCalendar(currentYear - 1, 12);
} else {
displayCalendar(currentYear, currentMonth - 1);
}
});
pNorth.add(previousMonthButton);
pNorth.add(nextMonthButton);
// South panel
pSouth = new JPanel(new FlowLayout());
infoLabel = new JLabel("Some information here");
pSouth.add(infoLabel);
// Add panels to frame
add(pNorth, BorderLayout.NORTH);
add(pCenter, BorderLayout.CENTER);
add(pSouth, BorderLayout.SOUTH);
setVisible(true);
}
private void displayCalendar(int year, int month) {
currentYear = year;
currentMonth = month;
// Update center panel
for (int i = 7; i < 49; i++) {
days[i].setText("");
}
int firstDayOfWeek = getFirstDayOfWeek(year, month);
int numDays = getNumDaysInMonth(year, month);
for (int i = 1; i <= numDays; i++) {
days[firstDayOfWeek + i - 1].setText("" + i);
}
// Update info label
String monthName = getMonthName(month);
infoLabel.setText(monthName + " " + year);
}
private String getWeekday(int index) {
String[] weekdays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
return weekdays[index];
}
private String getMonthName(int month) {
String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
return months[month - 1];
}
private int getFirstDayOfWeek(int year, int month) {
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month - 1, 1);
return cal.get(java.util.Calendar.DAY_OF_WEEK) - 1;
}
private int getNumDaysInMonth(int year, int month) {
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month - 1, 1);
return cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
}
public static void main(String[] args) {
new CalendarGUI();
}
}
```
这个程序使用了Java的Lambda表达式来简化事件处理器的编写。在代码中,`displayCalendar`方法用于根据给定的年份和月份更新日历视图,`getFirstDayOfWeek`和`getNumDaysInMonth`方法用于计算月份中第一天是周几和该月的天数。`getWeekday`和`getMonthName`方法用于获取星期几和月份名称的文本。
阅读全文