package csdn.net.shao;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/**
*全代码抄录java编程思想加入了2个函数适应不同的显示组件
*/
public class FrameWork
{
//创建一个class名称的字符串
public static String title(Object o)
{
String t=o.getClass().toString();
//删除tostring()得到的class 的字符
if(t.indexOf("class")!=-1)
t=t.substring(6);
return t;
}
public static void setupClosing(JFrame frame)
{
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
}
public static void run(JFrame frame,int width,int height)
{
setupClosing(frame);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void run(JApplet applet,int width,int height)
{
JFrame frame=new JFrame(title(applet));
setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void run(Applet applet,int width,int height)
{
JFrame frame=new JFrame(title(applet));
setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void run(JPanel panel,int width,int height)
{
JFrame frame=new JFrame(title(panel));
setupClosing(frame);
frame.getContentPane().add(panel);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void run(Panel panel,int width,int height)
{
JFrame frame=new JFrame(title(panel));
setupClosing(frame);
frame.getContentPane().add(panel);
frame.setSize(width,height);
frame.setVisible(true);
}
}
应用时,比如说有一个public class A extends JApplet
那么先 import csdn.net.shao.FrameWork;
在你原先的applet代码的最后一个}前面加上
public static void main(String[] args)
{
FrameWork.run(new A(),200,50);
}
即可直接将applet frame panel japplet jpanel在editplus,ultraledit ,jcreator等软件中直接执行.
本文地址:http://com.8s8s.com/it/it13267.htm