JFrame、JApplet、Jpanel综合测试类

package WindowsandApplet;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Consoletest {
	public static String title(Object o) {
		String s = o.getClass().toString();
		// 判断名字中是否有class子字符串
		if (s.indexOf("class") != -1) {
			// 从索引为6,包括6截取至最后的字符串
			s = s.substring(6);
		}
		return s;
	}

	public static void run(JFrame frame, int width, int height) {
		// 使点击关闭x时退出程序,缺省为什么都不做,不退出程序
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.setVisible(true);
	}

	public static void run(JApplet applet, int width, int height) {
		JFrame frame = new JFrame(title(applet));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.getContentPane().add(applet);
		frame.setVisible(true);
		applet.init();
		applet.start();
	}

	public static void run(JPanel panel, int width, int height) {
		JFrame frame = new JFrame(title(panel));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.setVisible(true);
		frame.getContentPane().add(panel);
	}
}

这是一个图形界面的测试类,第一个方法run(JFrame frame, int width, int height)用以测试JFrame应用程序,第二个方法run(JApplet applet, int width, int height)用于测试JApplet程序,第三个方法测试的是Jpanel程序

猜你喜欢

转载自blog.csdn.net/bigseacoming/article/details/80218157