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程序