当前位置: 首页 > news >正文

Java之API详解之Runtime的详细解析

3.1 概述

Runtime表示Java中运行时对象,可以获取到程序运行时设计到的一些信息

3.2 常见方法

常见方法介绍

我们要学习的Object类中的常见方法如下所示:

public static Runtime getRuntime()		//当前系统的运行环境对象
public void exit(int status)			//停止虚拟机
public int availableProcessors()		//获得CPU的线程数
public long maxMemory()				    //JVM能从系统中获取总内存大小(单位byte)
public long totalMemory()				//JVM已经从系统中获取总内存大小(单位byte)
public long freeMemory()				//JVM剩余内存大小(单位byte)
public Process exec(String command) 	//运行cmd命令

代码示例:

public class RunTimeDemo1 {public static void main(String[] args) throws IOException {/*public static Runtime getRuntime() 当前系统的运行环境对象public void exit(int status) 停止虚拟机public int availableProcessors() 获得CPU的线程数public long maxMemory() JVM能从系统中获取总内存大小(单位byte)public long totalMemory() JVM已经从系统中获取总内存大小(单位byte)public long freeMemory() JVM剩余内存大小(单位byte)public Process exec(string command) 运行cmd命令*///1.获取Runtime的对象//Runtime r1 =Runtime.getRuntime();//2.exit 停止虚拟机//Runtime.getRuntime().exit(0);//System.out.println("看看我执行了吗?");//3.获得CPU的线程数System.out.println(Runtime.getRuntime().availableProcessors());//8//4.总内存大小,单位byte字节System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);//4064//5.已经获取的总内存大小,单位byte字节System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);//254//6.剩余内存大小System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);//251//7.运行cmd命令//shutdown :关机//加上参数才能执行//-s :默认在1分钟之后关机//-s -t 指定时间 : 指定关机时间//-a :取消关机操作//-r: 关机并重启Runtime.getRuntime().exec("shutdown -s -t 3600");}
}

3.3 恶搞好基友

需求:

界面上方按钮默认隐藏

界面中间有一个提示文本和三个按钮

当你的好基友点击中间三个按钮的时候就在N秒之后关机,不同的按钮N的值不一样

任意一个按钮被点击之后,上方了按钮出现。当点击上方按钮之后取消关机任务

public class Test {public static void main(String[] args) {new MyJframe();}
}

 

public class MyJframe extends JFrame implements ActionListener {JButton yesBut = new JButton("帅爆了");JButton midBut = new JButton("一般般吧");JButton noBut = new JButton("不帅,有点磕碜");JButton dadBut = new JButton("饶了我吧!");//决定了上方的按钮是否展示boolean flag = false;public MyJframe() {initJFrame();initView();//显示this.setVisible(true);}private void initView() {this.getContentPane().removeAll();if (flag) {//展示按钮dadBut.setBounds(50, 20, 100, 30);dadBut.addActionListener(this);this.getContentPane().add(dadBut);}JLabel text = new JLabel("你觉得自己帅吗?");text.setFont(new Font("微软雅黑", 0, 30));text.setBounds(120, 150, 300, 50);yesBut.setBounds(200, 250, 100, 30);midBut.setBounds(200, 325, 100, 30);noBut.setBounds(160, 400, 180, 30);yesBut.addActionListener(this);midBut.addActionListener(this);noBut.addActionListener(this);this.getContentPane().add(text);this.getContentPane().add(yesBut);this.getContentPane().add(midBut);this.getContentPane().add(noBut);this.getContentPane().repaint();}private void initJFrame() {//设置宽高this.setSize(500, 600);//设置标题this.setTitle("恶搞好基友");//设置关闭模式this.setDefaultCloseOperation(3);//置顶this.setAlwaysOnTop(true);//居中this.setLocationRelativeTo(null);//取消内部默认布局this.setLayout(null);}@Overridepublic void actionPerformed(ActionEvent e) {Object obj = e.getSource();if (obj == yesBut) {//给好基友一个弹框showJDialog("xxx,你太自信了,给你一点小惩罚");try {Runtime.getRuntime().exec("shutdown -s -t 3600");} catch (IOException ioException) {ioException.printStackTrace();}flag = true;initView();} else if (obj == midBut) {System.out.println("你的好基友点击了一般般吧");//给好基友一个弹框showJDialog("xxx,你还是太自信了,也要给你一点小惩罚");try {Runtime.getRuntime().exec("shutdown -s -t 7200");} catch (IOException ioException) {ioException.printStackTrace();}flag = true;initView();} else if (obj == noBut) {System.out.println("你的好基友点击了不帅");//给好基友一个弹框showJDialog("xxx,你还是有一点自知之明的,也要给你一点小惩罚");try {Runtime.getRuntime().exec("shutdown -s -t 1800");} catch (IOException ioException) {ioException.printStackTrace();}flag = true;initView();} else if (obj == dadBut) {//给好基友一个弹框showJDialog("xxx,这次就饶了你~");try {Runtime.getRuntime().exec("shutdown -a");} catch (IOException ioException) {ioException.printStackTrace();}}}public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}
}

http://www.lryc.cn/news/142986.html

相关文章:

  • 机器学习之softmax
  • npm script命令
  • 【力扣周赛】第360场周赛
  • php环境变量的配置步骤
  • Kdtree
  • 算法leetcode|74. 搜索二维矩阵(rust重拳出击)
  • element浅尝辄止7:InfiniteScroll 无限滚动
  • Day05-Vue基础
  • 《机器学习在车险定价中的应用》实验报告
  • 14. Docker中实现CI和CD
  • 【多思路解决喝汽水问题】1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以喝多少汽水
  • P1591 阶乘数码(Java高精度)
  • Mybatis的动态SQL及关键属性和标识的区别(对SQL更灵活的使用)
  • mysql下载
  • 聚合函数与窗口函数
  • c语言实现堆
  • ubuntu 如何将文件打包成tar.gz
  • 前端优化页面加载速度的方法(持续更新)
  • 利用SSL证书的SNI特性建立自己的爬虫ip服务器
  • HTML和CSS
  • C#的IndexOf
  • 深度学习2.神经网络、机器学习、人工智能
  • 利用LLM模型微调的短课程;钉钉宣布开放智能化底座能力
  • 软件工程(七) UML之用例图详解
  • pd.cut()函数--Pandas
  • DataBinding的基本使用
  • eslint和prettier格式化冲突
  • matlab使用教程(26)—常微分方程的求解
  • 尚硅谷宋红康MySQL笔记 14-18
  • 香港全新的虚拟资产服务商发牌制度