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

Timer Swing的更多相关文章

  1. java swing中Timer类的学习

    最近在完成学校课程的java平时作业,要实现一个计时器,包含开始.暂停以及重置三个功能.由于老师规定要用这个timer类,也就去学习了一下,顺便记录一下. 首先呢去查了一下java手册上的东西,发现t ...

  2. Java Swing 之Timer配合JProgressBar的使用

    Timer作为java开发中常用的一个定时工具,配合JProgressBar使用起来还真是方便,只需要调用timer.start()方法就能激活并运行,然后调用stop()方法便能停止,还可以再次通过 ...

  3. [Java]利用javax.swing.Timer类在窗口上实现动画效果

    javax.swing.Timer类在创建时需要指定时间间隔和定时器到时间需要执行的动作,即ActionListener. Timer timer = new Timer(100, taskPerfo ...

  4. javax.swing.Timer

    javax.swing 类 Timer java.lang.Object javax.swing.Timer 所有已实现的接口: Serializable public class Timerexte ...

  5. java swing 双人五子棋源代码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...

  6. java swing文件内容检索工具

    Java相关技术 - 文件内容检索工具 拿到一个几百M甚至上G的project让你去学习 有时候你会想知道某个关键词是在哪个文件里 比如:spring MVC配置的@RequestMapping,你从 ...

  7. 使用Timer类的两个实例 动态时钟

    package chapter16; import javax.swing.*; import chapter15.StillClock; import java.awt.event.*; publi ...

  8. Swing基础

    Swing基础 JFrame JPanel 绘图:paint 监听事件: ActionListener  KeyListener Listener和Adapter 计时器:Timer     Time ...

  9. 定时执行Timer

    JAVA import java.awt.event.*; import java.io.BufferedWriter;import java.io.File;import java.io.FileO ...

随机推荐

  1. WPF学习之绘图和动画--DarrenF

    Blend作为专门的设计工具让WPF如虎添翼,即能够帮助不了解编程的设计师快速上手,又能够帮助资深开发者快速建立图形或者动画的原型. 1.1   WPF绘图 与传统的.net开发使用GDI+进行绘图不 ...

  2. 分布式文件系统FastDFS介绍和配置过程

    http://ylw6006.blog.51cto.com/470441/948729/ 由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构 ...

  3. UML之轻松入门(3)-SRP做好厨子,让别人编程去吧

         一个厨子能够做出一手好菜,或许他是新东方毕业的或者是祖传秘方.你让他做上一桌佳肴那是简单.快乐而又高效的,然而让他编程就会成为一种苦恼并且让人想不通的一件事.或许这个比喻不是非常恰当,可是对 ...

  4. 数据市中心全省中国mysql脚本

    1.查尔斯省 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2h6aGFvY2hhbw==/font/5a6L5L2T/fontsize/400/fill ...

  5. MVC Razor视图引擎控件

    0.日期转化

  6. repo总结

    repo刚google使用Python脚本写通话git脚本.主要用于下载.管理Android工程仓库. 1. 下载 repo 的地址: http://android.git.kernel.org/re ...

  7. 判断闰年(go语言版本)

    import "strconv" func IsLeapYear(y string) bool { //y == 2000, 2004 //判断是否为闰年 year, _ := s ...

  8. html5移动开发--js温馨提示

    1.a标签执行js笔试 <a id="myID" href="javascript:myfuction();"></a> 2.实时监听i ...

  9. Directx11学习笔记【一】 最简单的windows程序HelloWin

    声明:本系列教程代码有部分来自dx11龙书及dx11游戏编程入门两本书,后面不再说明 首先,在vs2013中创建一个空的解决方案Dx11Demo,以后的工程都会放在这个解决方案下面.然后创建一个win ...

  10. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 分块

    分块大法好 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 2938  Solved: 13 ...