zoukankan      html  css  js  c++  java
  • Timer Swing


    一个Swing的例子,按钮控件上中文出现乱码:

    试了网上的设置Font,或将汉字使用new  String(str.getBytes(),"GBK")对展示的汉字进行编码。都无效。

    解决办法:
    (1)更改项目的编码为GBK,汉字可以正式显示
    (2)将java.awt.Button更改为javax.swing.JButton

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.util.Calendar;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class CloseComputer extends JFrame implements ActionListener {
        private static final long serialVersionUID = 1L;
        // 创建实现BorderLayout布局的面板对象panelmain,用于放panelSubnorth面板和panelSubcenter面板
        private JPanel panelMain;
        // 创建实现FlowLayout布局的面板对象panelSubnorth,用于放tag提示标签
        private JPanel panelSubnorth;
        // 创建实现FlowLayout布局的面板对象panelSubcenter,用于放3个按钮
        private JPanel panelSubcenter;
    
        // 创建三个按钮
        private JButton countdown;
        private JButton time;
        private JButton cancel;
        private String key;
        // 创建一个提示标签
        private JLabel tag;
    
        public CloseComputer() {
            initComponents();
        }
    
        public void initComponents() {
            panelMain = new JPanel(new BorderLayout(5, 10));
            panelSubnorth = new JPanel(new FlowLayout(3));
            panelSubcenter = new JPanel(new FlowLayout(1, 5, 5));
    
            countdown = new JButton("倒计时关机");
            time = new JButton("定时关机");
            cancel = new JButton("取消关机");
    
            tag = new JLabel("请选择关机方式");
    
            // 将panelMain添加到窗体中
            this.getContentPane().add(panelMain);
            // 添加对象panelSubnorth到对象panelMain窗口里
            panelMain.add(panelSubnorth, BorderLayout.NORTH);
            // 添加对象panelSubcenter到对象panelMain窗口里
            panelMain.add(panelSubcenter, BorderLayout.CENTER);
    
            // 添加标签对象tag到对象panelSubnorth窗口里
            panelSubnorth.add(tag);
    
            // 添加3个按钮到对象panelSubcenter里
            panelSubcenter.add(countdown);
            panelSubcenter.add(time);
            panelSubcenter.add(cancel);
    
            // 为3个按钮注册事件监听器
            countdown.addActionListener(this);
            time.addActionListener(this);
            cancel.addActionListener(this);
        }
    
        /**
         * 倒计时关机
         */
        public void countdown() {
            key = JOptionPane.showInputDialog(this, "请输入倒计时关机剩余的时间(秒)", "输入框", JOptionPane.INFORMATION_MESSAGE);
            String command = "shutdown -s -t " + key;
            executeCommand(command);
        }
    
        /**
         * 定时关机
         */
        public void time() {
            Calendar calendar = Calendar.getInstance();
            int h = calendar.get(Calendar.HOUR); // 获取小时
            int m = calendar.get(Calendar.MINUTE); // 获取分钟
            int s = calendar.get(Calendar.SECOND); // 获取秒
            // 输入时间
            String hourInputValue, minuteInputValue, secordInputValue; // 保存输入的时间
            hourInputValue = JOptionPane.showInputDialog(this, "请输入关机的小时(12小时制)", "输入", JOptionPane.INFORMATION_MESSAGE);
            minuteInputValue = JOptionPane.showInputDialog(this, "请输入关机的分钟", "输入", JOptionPane.INFORMATION_MESSAGE);
            secordInputValue = JOptionPane.showInputDialog(this, "请输入关机的秒钟", "输入", JOptionPane.INFORMATION_MESSAGE);
            // 转换时间
            int hour, minute, secord; // 保存转换后的时间
            hour = Integer.parseInt(hourInputValue);
            minute = Integer.parseInt(minuteInputValue);
            secord = Integer.parseInt(secordInputValue);
    
            long setTime = timeSum(hour, minute, secord); // 计算输入时间的总和
            long currentlyTime = timeSum(h, m, s); // 计算当前系统时间的总和
            long discrepancyTime = setTime - currentlyTime; // 获取时间差
    
            if (discrepancyTime < 0) {
                String command = "shutdown -s";
                executeCommand(command);
            } else {
                String command = "shutdown -s  -t " + discrepancyTime;
                executeCommand(command);
                JOptionPane.showMessageDialog(this, "恭喜你,设置成功!", "确认", JOptionPane.WARNING_MESSAGE);
            }
        }
    
        private void executeCommand(String command) {
            try {
                Runtime.getRuntime().exec(command);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 计算出时间总和,并返回
         */
        public int timeSum(int h, int m, int s) {
            return h * 3600 + m * 60 + s;
        }
    
        /**
         * 取消关机
         */
        public void cancel() {
            JOptionPane.showMessageDialog(this, "你已经成功取消了关机操作!", "消息", JOptionPane.WARNING_MESSAGE);
            String command = "shutdown -a";
            executeCommand(command);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CloseComputer frame = new CloseComputer();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.setResizable(false);
                    frame.setTitle("关机工具");
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.pack();
    
                    frame.setVisible(true);
                }
            });
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == countdown) {
                countdown();
            }
            if (e.getSource() == time) {
                time();
            }
            if (e.getSource() == cancel) {
                cancel();
            }
        }
    }

     

  • 相关阅读:
    Event notifications
    twobin博客样式
    Http协议
    ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
    理解计算机系统3
    游标-Oracle游标汇总
    Oracle10g 回收站及彻底删除table : drop table xx purge
    ora-01031:insufficient privileges
    &&运算符和||运算符的优先级问题 专题
    oracle connect by 和start with
  • 原文地址:https://www.cnblogs.com/softidea/p/4813019.html
Copyright © 2011-2022 走看看