地球Online生存天数计算器(java小案例)
学日期工具类时的小案例,编程不该枯燥无味的,人生不过3万天,哎一看进度过了好多,朋友们共勉吧~
import java.awt.*;
import java.awt.event.*;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import javax.swing.*;
public class LiveDaysGUI extends JFrame {
private JTextField yearField;
private JTextField monthField;
private JTextField dayField;
private JButton calculateButton;
private JLabel resultLabel;
public LiveDaysGUI() {
setTitle("地球Online生存天数计算器----written by frandiy");
setSize(750, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // 窗口居中
// 左侧图片标签
JLabel imageLabel = new JLabel();
ImageIcon imageIcon = new ImageIcon(getClass().getResource("/picture.png"));
imageIcon = new ImageIcon(imageIcon.getImage().getScaledInstance(264, 340, Image.SCALE_SMOOTH));
imageLabel.setIcon(imageIcon);
imageLabel.setPreferredSize(new Dimension(264, 340));
// 右侧输入面板
JPanel inputPanel = new JPanel();
inputPanel.setOpaque(false);
inputPanel.setLayout(new GridBagLayout());
JLabel promptLabel = new JLabel("请输入生日:");
promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
// 年月日输入框
yearField = new JTextField(4);
monthField = new JTextField(2);
dayField = new JTextField(2);
yearField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
monthField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
dayField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
JLabel yearLabel = new JLabel("年");
JLabel monthLabel = new JLabel("月");
JLabel dayLabel = new JLabel("日");
yearLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
monthLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
dayLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
// 组装日期输入组件
JPanel datePanel = new JPanel();
datePanel.setOpaque(false);
datePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
datePanel.add(yearField);
datePanel.add(yearLabel);
datePanel.add(monthField);
datePanel.add(monthLabel);
datePanel.add(dayField);
datePanel.add(dayLabel);
calculateButton = new JButton("计算");
calculateButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
calculateButton.setBackground(new Color(0, 123, 255));
calculateButton.setForeground(Color.WHITE);
calculateButton.setFocusPainted(false);
resultLabel = new JLabel();
resultLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
resultLabel.setPreferredSize(new Dimension(380, 60)); // 设置固定尺寸
resultLabel.setVerticalAlignment(SwingConstants.TOP);
// 设置布局
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
inputPanel.add(promptLabel, c);
c.gridx = 0;
c.gridy = 1;
inputPanel.add(datePanel, c);
c.gridx = 0;
c.gridy = 2;
inputPanel.add(calculateButton, c);
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
inputPanel.add(resultLabel, c);
// 添加按钮事件监听
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculateLiveDays();
}
});
// 创建主面板,使用JSplitPane分隔左右面板
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(imageLabel);
splitPane.setRightComponent(inputPanel);
splitPane.setDividerLocation(280); // 设置分隔线位置
splitPane.setEnabled(false); // 禁止拖动分隔线
// 添加到内容面板
setContentPane(splitPane);
}
private void calculateLiveDays() {
String yearText = yearField.getText();
String monthText = monthField.getText();
String dayText = dayField.getText();
try {
int year = Integer.parseInt(yearText);
int month = Integer.parseInt(monthText);
int day = Integer.parseInt(dayText);
LocalDate birthDay = LocalDate.of(year, month, day);
LocalDate today = LocalDate.now();
long between = ChronoUnit.DAYS.between(birthDay, today);
resultLabel.setText("<html>到现在您已在地球Online中生活了:<br/><b>" + between + " </b>天!祝您游戏顺利~</html>");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "日期格式错误,请输入有效的年月日。", "错误", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
try {
// 设置系统外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
LiveDaysGUI gui = new LiveDaysGUI();
gui.setVisible(true);
});
}
}