一个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. SQL Server :理解数据记录结构

    原文:SQL Server :理解数据记录结构 在SQL Server :理解数据页结构我们提到每条记录都有7 bytes的系统行开销,那这个7 bytes行开销到底是一个什么样的结构,我们一起来看下 ...

  2. 新浪微博。。openapi 分享 图画+ 写作

    新浪微博困难啊 .. . .. .郁闷死了. .在此记录它 1.使用界面:https://api.weibo.com/2/statuses/upload_url_text.json 能够申请,.高级权 ...

  3. Foursquare 8.0 :聪明人给互联网公司上的流量转化课

    今年 5 月上线的 Swarm 虽然应用制作精良,但不免让人怀疑是 Foursquare一次失败的互联网公司服务越界和用户忠诚度试水.但非常快这群聪明人让我们发现事情并没有这么简单:他们给互联网公司们 ...

  4. javascript---在自由落体实现

    实现一些简单的物业自由落体需要理解: clientHeight:浏览器客户机的整体高度 offsetHeight:物(实例div)高低 offsetTop:从对象client最顶层的距离 简单demo ...

  5. Xamarin之 环境错误集锦

    错误信息:   connection of the layout renderer failed.this may be caused by a misconfiguration of java .p ...

  6. json2.js参考

    json2.js使用參考 json2.js提供了json的序列化和反序列化方法,能够将一个json对象转换成json字符串,也能够将一个json字符串转换成一个json对象. <html> ...

  7. Tuxedo学习门户网站

    中间件简介: 介于客户机和server之间的夹层,突破了传统的c/s架构,为构建大规模,高性能.分布式c/s应用程序提供了通信.事物,安全.容错等基础服务,屏蔽了底层应用细节,应用程序不必从底层开发, ...

  8. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  9. OutputCache说明

    当用户访问该页面,整个页面会server存储在内存中,因此,该页面缓存.当用户再次访问该页面,页面不会再次运行数据操作,页面首先检查server中是否存在缓存.假设缓存存在,则直接从缓存中获取页面信息 ...

  10. richedit设置滚动条的位置和更新内容

    需要txt发现读者richedit的scrollbar位置(为了便于下一次读,直接访问与上次读取下一个读取位置)不值得治疗,采用GetScrollPos.SetScrollPos你可以设置scorll ...