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();
}
}
}
Timer Swing的更多相关文章
- java swing中Timer类的学习
最近在完成学校课程的java平时作业,要实现一个计时器,包含开始.暂停以及重置三个功能.由于老师规定要用这个timer类,也就去学习了一下,顺便记录一下. 首先呢去查了一下java手册上的东西,发现t ...
- Java Swing 之Timer配合JProgressBar的使用
Timer作为java开发中常用的一个定时工具,配合JProgressBar使用起来还真是方便,只需要调用timer.start()方法就能激活并运行,然后调用stop()方法便能停止,还可以再次通过 ...
- [Java]利用javax.swing.Timer类在窗口上实现动画效果
javax.swing.Timer类在创建时需要指定时间间隔和定时器到时间需要执行的动作,即ActionListener. Timer timer = new Timer(100, taskPerfo ...
- javax.swing.Timer
javax.swing 类 Timer java.lang.Object javax.swing.Timer 所有已实现的接口: Serializable public class Timerexte ...
- java swing 双人五子棋源代码
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...
- java swing文件内容检索工具
Java相关技术 - 文件内容检索工具 拿到一个几百M甚至上G的project让你去学习 有时候你会想知道某个关键词是在哪个文件里 比如:spring MVC配置的@RequestMapping,你从 ...
- 使用Timer类的两个实例 动态时钟
package chapter16; import javax.swing.*; import chapter15.StillClock; import java.awt.event.*; publi ...
- Swing基础
Swing基础 JFrame JPanel 绘图:paint 监听事件: ActionListener KeyListener Listener和Adapter 计时器:Timer Time ...
- 定时执行Timer
JAVA import java.awt.event.*; import java.io.BufferedWriter;import java.io.File;import java.io.FileO ...
随机推荐
- OpenStack_Swift源代码分析——Ring基本原理及一致性Hash算法
1.Ring的基本概念 Ring是swfit中最重要的组件.用于记录存储对象与物理位置之间的映射关系,当用户须要对Account.Container.Object操作时,就须要查询相应的Ring文件( ...
- centos6.4设备hadoop-2.5.1(完全分布式)
环境介绍: 在这两种装备centos6.4(32位置)的server安装Hadoop-2.5.1分布式集群(2台机器,主要试验用.哈哈). 1.改动主机名和/etc/hosts文件 1)改动主机名(非 ...
- MVC快速分页
.NET手记-ASP.NET MVC快速分页的实现 对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实 ...
- cocos2d之Box2D详细说明 鼠标联合实现
cocos2d之Box2D具体解释 鼠标关节实现 DionysosLai2014-5-7 我们常常要移动物理世界中的某个物体,例如说石头.木块等.假设我们直接改变这些物体的位置,让这些物体尾随我们手指 ...
- 神马是AB測试?
手机响起了短信提示音,桂芬拿起手机,滑动解锁,看到是来福的短信,心里不禁一阵小鹿乱撞. "喜欢看<冰封重生之门>吗?" 如此充满暗示意味的短信让桂芬激动 ...
- 水声通信(传声)于iOS、Android在情景-depth分析(包括一些声通信源)
最近的水声通信非常热,特别是,非常嵌入式设备备受瞩目使用,前段时间公布了声通信部分源代码(iOS和Android版本号.下载源的最新版本:点击打开链接 http://download.csdn.net ...
- C++学习笔记9-运算符重载
1. 重载运营商必须有一个类类型的操作数 对于内置类型运营商.它的意义不能改变. 例如,内置整数加法运算不能被重新定义: // error: cannotredefine built-in opera ...
- 在C#环境中动态调用IronPython脚本(一)
本文讲述用C#调用Ironpython运行环境,解析并运行动态pyhton脚本.这种情况应用在那些需要滞后规定行为的场合,例如,动态计算项(计算引擎),用户可以自定义计算内容.计算公式等. 本文的代码 ...
- 重写onBackPressed方法
android手机back按键响应方法重构: long exitTime = System.currentTimeMillis() - 2000; public void onBackPressed( ...
- Android Widget 小部件(一) 简单实现
在屏幕上加入Widget:或长按屏幕空白处,或找到WidgetPreview App选择. 原生系统4.0下面使用长按方式,4.0及以上 打开WIDGETS 创建Widget的一般步骤: 在menif ...