Timer Swing

简介: 一个Swing的例子,按钮控件上中文出现乱码: 试了网上的设置Font,或将汉字使用new  String(str.getBytes(),"GBK")对展示的汉字进行编码。都无效。 解决办法:(1)更改项目的编码为GBK,汉字可以正式显示(2)将java.


一个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();
        }
    }
}

 

 

相关文章
|
5月前
|
Java
java的Timer定时器
java的Timer定时器
|
7月前
|
Java
Swing事件监听
Swing事件监听
|
8月前
|
Java 容器
Java Swing中的按钮和事件
Java Swing中的按钮和事件
87 0
|
Java 调度
Java Timer使用介绍
java.util包下提供了对定时任务的支持,涉及2个类: 1. Timer:定时器类 2. TimerTask:任务抽象类 使用该定时任务我们需要继承TimerTask抽象类,覆盖run方法编写任务执行代码,并利用Timer定时器对TimerTask进行调度。
109 0
|
API 调度 Android开发
【Android 异步操作】Timer 定时器 ( Timer 与 TimerTask 基本使用 | Timer 定时器常用用法 | Timer 源码分析 )
【Android 异步操作】Timer 定时器 ( Timer 与 TimerTask 基本使用 | Timer 定时器常用用法 | Timer 源码分析 )
739 0
|
消息中间件 Java C#
C# 三个Timer
C# 三个Timer
232 0
C# 三个Timer