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 ...
随机推荐
- WebView使用配置文件
录制webview示例使用,以免以后忘记. 布局文件: <WebView android:layout_width="fill_parent" android:layout_ ...
- muduo网络图书馆评测
上个月看到朋友推荐mudo网络图书馆,该代码是在国内同行中,开源工程后.甚至钦佩.根据mudo手动和035代码的版本看起来正在建设中.感觉是一个比较成熟且易于使用的网络库.我的手也有自己的网络库,虽然 ...
- Node.js : 我只需要一个店小二
刚刚开始接触Node.js时, google了很多文章,但发现大部分都是泛泛的介绍安装,配置,以及介绍几个小例子 有一种雾里观花的感觉,所以非常困惑,不知道Node.js到底解决了什么问题,它的优势到 ...
- ORACLE触发特定的解释
ORACLE PL/SQL编程八: 把触发器说透 本篇主要内容例如以下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 ...
- 【DataStructure】The description of Java Collections Framework
The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...
- 代码的未来读书笔记<二>
代码的未来读书笔记<二> 3.1语言的设计 对Ruby JavaScript Java Go 从服务端client以及静态动态这2个角度进行了对照. 这四种语言因为不同的设计方针,产生了不 ...
- linux下一个oracle11G DG建立(一个):准备环境
linux下一个oracle11G DG建立(一个):准备环境 周围环境 名称 主库 备库 主机名 bjsrv shsrv 软件版本号 RedHat Enterprise5.5.Oracle 11g ...
- 返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
原文:返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test [索引页] [源码下载] 返璞归真 ...
- 懒与馋的平衡:餐饮O2O市场广阔,发展不易
餐饮行业是众多行业中O2O起步较早的,现在方兴未艾的团购站点中最先涉足的领域就有餐饮版块.长时间的合作推广,很多餐饮商家已经从中尝到甜头,可以说餐饮行业市场基础培育的比較好,所以餐饮O2O 已经是大势 ...
- 计时器 Timer
计时器 Timer 不多说了,守则.