博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Timer Swing
阅读量:7057 次
发布时间:2019-06-28

本文共 5129 字,大约阅读时间需要 17 分钟。

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

 

 

转载地址:http://iygol.baihongyu.com/

你可能感兴趣的文章
Ubuntu里的内核交叉编译和共享文件夹编译的区别
查看>>
阿里云消息队列MQ_HTTP接入 for .NetCore 简单例子
查看>>
ActiveMQ 报错 Temporary Store limit is 51200 mb
查看>>
【转】网络编程socket基本API详解
查看>>
查找某关键字在目标字符串中出现次数及位置
查看>>
新浪微博iOS版SDK“宝玉XP”框架学习笔记
查看>>
Jmeter集合点
查看>>
webSphere中文日志乱码,设置日志编码方法
查看>>
单元测试WebForm的UI逻辑及文件上传
查看>>
java操作excel进行导入导出
查看>>
黄聪:wordpress如何获取访问的网站的cookie值
查看>>
js将汉字转为相应的拼音
查看>>
不一样的大小写转换
查看>>
图论基础概念
查看>>
【电脑硬盘怎样才能用的长久 它的保养建议大分享】
查看>>
es6+最佳入门实践(5)
查看>>
LevelList Drawable用法
查看>>
C++ 定义全局数组
查看>>
linux操作系统的目录以及用户权权限的管理
查看>>
GAN
查看>>