⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example04_systemtool.java

📁 java课程设计教材上的程例题序代码
💻 JAVA
字号:
package example;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;

import java.util.*;

import javax.swing.JOptionPane;

public class Example04_SystemTool {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int select = 0;
		try {
			select = System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		switch (select) {
			case '1':
				func1();
				break;
			case '2':
				func2();
				break;
			case '3':
				func3();
				break;
			case '4':
				func4();
				break;
			default:
				System.out.println("Invalid input!");
		}
	}

	public static void func1() {
		String mes = "";
		String name = JOptionPane.showInputDialog(null, "Input a class name");
		Object object = null;
		try {
			object = Class.forName(name).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Class c = object.getClass();
		do {
			mes += c.getName() + "\n";
			c = c.getSuperclass();
		} while (c != null);
		showMessage(mes);
	}

	public static void func2() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		String mes = "";
		mes += "Width  = " + d.getWidth() + "\n";
		mes += "Height = " + d.getHeight() + "\n";
		showMessage(mes);
	}

	public static void func3() {
		String mes = "";
		long begin = System.currentTimeMillis();
		Random r = new Random();
		for (int i = 1; i <= 10; i++) {
			int x = r.nextInt(50);
			Toolkit.getDefaultToolkit().beep();
			mes += x + ",";
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		long end = System.currentTimeMillis();
		mes += "\n";
		mes += "time is : " + (end - begin);
		showMessage(mes);
	}

	public static void func4() {
		String mes = "";
		Runtime run = Runtime.getRuntime();
		mes += run.freeMemory() + "\n";
		String name = JOptionPane.showInputDialog(null, "Input a program name");
		try {
			run.exec(name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mes += run.freeMemory();
		showMessage(mes);
	}

	public static void showMessage(String mes) {
		JOptionPane.showMessageDialog(null, mes);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -