一个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. Android数据存储——SQLite数据库(模板)

    本篇整合Android使用数据库,要保存一个实体类的样本. 首先看一下数据库语句: ORM:关系对象映射 添加数据: ContentValues values = new ContentValues( ...

  2. Ubuntu 上 hi3531 交叉编译环境 arm-hisiv100nptl-linux 建设过程

    安装SDK 1.Hi3531 SDK包的位置     在"Hi3531_V100R001***/01.software/board"夹,你可以看到一个 Hi3531_SDK_Vx. ...

  3. 怎么样putty打开图形化管理工具,在终端上

    有时需要在putty这种图形终端开放的图形化管理工具将出现以下错误: [root@node2 ~]# Traceback (most recent call last): File "/us ...

  4. struts2在&lt;s:select&gt;用动态标签

    后台传过来的必要性userlist成为一个下拉菜单.因此,认为使用<s:select>.但设置了很久设置的属性,在这个下跌. JSP代码: <s:select label=" ...

  5. Codeforces 9A-Die Roll(意甲冠军)

    A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...

  6. Xamarin.Android 使用 SQLiteOpenHelper 进行数据库操作

    一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...

  7. Git相关操作汇总

    git clone: 正如上图,当我们打开终端的情况下,默认我们所在的目录是在/home/shiyanlou的,大家可以在终端输入以下命令把目录切换到桌面cd  /home/Desktop这个时候输入 ...

  8. 合作编辑室计费系统(一)-SVN常见错误

    联合室已完成,在不到一个月的时间,我们的团队:嗤.陈琛.我.这段时间都挺辛苦的.从心里这次合作,真的让我们学习了非常多,学会了接纳和承担. 在我们開始合作机房的时候,社和师哥就给我们做了功课,说你们好 ...

  9. String和StringBuffer 常用方法总结

     String和StringBuffer 常用方法总结 一.不可变长度String 1.字符串---->char数组 char[] chars=str.toCharArray(); 2.字符串中 ...

  10. Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记

    面前app当完成测试,没问题,以完成整个老龄化阶段包含数据收发器,关键在 adb shell top -m 5  我发现我的 app pid 占用  CPU是最多的,事实上我想说写一个app是不难,你 ...