当前位置: 首页 > article >正文

地球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);
        });
    }
}


http://www.kler.cn/news/368703.html

相关文章:

  • 设计模式(二)
  • 代码随想录 -- 动态规划 -- 使用最小花费爬楼梯
  • Sentinel详解
  • spring final修饰
  • 虚拟光驱软件 PowerISO v8.7.0 中文激活版
  • 编写一个简单的Iinput_dev框架
  • GPU的使用寿命可能只有1~3年
  • 基于去哪儿旅游出行服务平台旅游推荐网站【源码+安装+讲解+售后+文档】
  • Linux 重启命令全解析:深入理解与应用指南
  • 51单片机完全学习——红外遥控
  • LeetCode——最小差值
  • RTMP视频推流EasyDSS平台重装服务器系统后无法启动是什么原因?
  • [LeetCode] 47. 全排列Ⅱ
  • 如何成为一个优秀的大数据开发工程师?
  • 基于SpringBoot的流浪动物管理系统设计与实现
  • Java面试题十三
  • 【Linux网络】Linux网络基础入门:初识网络,理解网络协议
  • 微知-Lecroy力科的PCIe协议分析仪型号命名规则(PCIe代,金手指lanes数量)
  • SQL Server 当前日期及其未来三天的日期
  • 【pytest中同一个用例多次执行生成一个测试报告的方法】
  • 学习FPGA需要掌握哪些语言
  • 线程支持库(C++11)
  • 【JavaEE初阶】网络原理-深入理解网络通信中协议的概念
  • 20241023软考架构-------软考案例5答案
  • 相关Coverage Path Planning的论文整理
  • C#的访问修饰符