java前端界面展示一周时间段选择
时间: 2023-07-05 10:28:39 浏览: 160
java 显示一周各星期的名称
4星 · 用户满意度95%
好的,下面是一个简单的 Java 前端界面展示一周时间段选择的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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 WeekdayPicker extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final String[] WEEKDAYS = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
private static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
private JButton[] dayButtons;
private JLabel monthLabel;
private JLabel yearLabel;
private Calendar currentCalendar;
public WeekdayPicker() {
super("WeekdayPicker");
currentCalendar = Calendar.getInstance();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(400, 300));
setLocationRelativeTo(null);
setResizable(false);
setLayout(new BorderLayout());
initUI();
setVisible(true);
}
private void initUI() {
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton prevYearButton = new JButton("<<");
prevYearButton.setActionCommand("prevYear");
prevYearButton.addActionListener(this);
topPanel.add(prevYearButton);
JButton prevMonthButton = new JButton("<");
prevMonthButton.setActionCommand("prevMonth");
prevMonthButton.addActionListener(this);
topPanel.add(prevMonthButton);
monthLabel = new JLabel(getMonthString(currentCalendar.get(Calendar.MONTH)), SwingConstants.CENTER);
monthLabel.setFont(new Font("Arial", Font.BOLD, 18));
monthLabel.setPreferredSize(new Dimension(120, 30));
topPanel.add(monthLabel);
yearLabel = new JLabel(String.valueOf(currentCalendar.get(Calendar.YEAR)), SwingConstants.CENTER);
yearLabel.setFont(new Font("Arial", Font.BOLD, 18));
yearLabel.setPreferredSize(new Dimension(60, 30));
topPanel.add(yearLabel);
JButton nextMonthButton = new JButton(">");
nextMonthButton.setActionCommand("nextMonth");
nextMonthButton.addActionListener(this);
topPanel.add(nextMonthButton);
JButton nextYearButton = new JButton(">>");
nextYearButton.setActionCommand("nextYear");
nextYearButton.addActionListener(this);
topPanel.add(nextYearButton);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(7, 7));
dayButtons = new JButton[49];
for (int i = 0; i < 7; i++) {
centerPanel.add(new JLabel(WEEKDAYS[i], SwingConstants.CENTER));
}
for (int i = 0; i < 49; i++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(50, 50));
button.setBorder(BorderFactory.createEmptyBorder());
button.setFocusPainted(false);
button.addActionListener(this);
dayButtons[i] = button;
centerPanel.add(button);
}
updateDayButtons();
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
bottomPanel.add(cancelButton);
JButton okButton = new JButton("OK");
okButton.addActionListener(this);
bottomPanel.add(okButton);
add(bottomPanel, BorderLayout.SOUTH);
}
private void updateDayButtons() {
int year = currentCalendar.get(Calendar.YEAR);
int month = currentCalendar.get(Calendar.MONTH);
int dayOfWeek = currentCalendar.get(Calendar.DAY_OF_WEEK);
int firstDayOfWeek = currentCalendar.getFirstDayOfWeek();
currentCalendar.set(Calendar.DAY_OF_MONTH, 1);
int daysInMonth = currentCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int day = 1;
int i = 0;
for (; i < dayOfWeek - firstDayOfWeek; i++) {
dayButtons[i].setText("");
dayButtons[i].setEnabled(false);
dayButtons[i].setBackground(new Color(240, 240, 240));
}
for (; i < dayOfWeek - firstDayOfWeek + daysInMonth; i++) {
dayButtons[i].setText(String.valueOf(day));
dayButtons[i].setEnabled(true);
dayButtons[i].setBackground(new Color(255, 255, 255));
day++;
}
for (; i < dayButtons.length; i++) {
dayButtons[i].setText("");
dayButtons[i].setEnabled(false);
dayButtons[i].setBackground(new Color(240, 240, 240));
}
monthLabel.setText(getMonthString(month));
yearLabel.setText(String.valueOf(year));
}
private String getMonthString(int month) {
return MONTHS[month];
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "prevYear":
currentCalendar.add(Calendar.YEAR, -1);
break;
case "prevMonth":
currentCalendar.add(Calendar.MONTH, -1);
break;
case "nextMonth":
currentCalendar.add(Calendar.MONTH, 1);
break;
case "nextYear":
currentCalendar.add(Calendar.YEAR, 1);
break;
case "Cancel":
dispose();
break;
case "OK":
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
StringBuilder builder = new StringBuilder();
builder.append(sdf.format(currentCalendar.getTime())).append(" - ");
currentCalendar.add(Calendar.DAY_OF_MONTH, 6);
builder.append(sdf.format(currentCalendar.getTime()));
System.out.println(builder.toString());
dispose();
break;
default:
break;
}
updateDayButtons();
}
public static void main(String[] args) {
new WeekdayPicker();
}
}
```
该示例代码实现了一个简单的一周时间段选择的功能,具体如下:
1. 界面由上、中、下三个部分组成,分别是顶部的年月选择、中间的日期选择和底部的取消和确定按钮。
2. 日期选择部分使用 GridLayout 布局方式,排列为 7 行 7 列的网格,第一行为星期几,其余行为日期。使用 JButton 实现每个日期的按钮。
3. 使用 Calendar 类获取当前年月和计算每个月的日期,并且根据日期的星期几确定在网格中的位置,更新每个日期按钮的显示文本、是否可用和背景色。
4. 点击年月选择按钮或日期按钮,更新当前年月或者选择日期,并且更新日期选择部分的显示。
5. 点击确定按钮,将选择的时间段输出到控制台,并且关闭界面。
希望这个示例代码能够对您有所帮助。
阅读全文