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等軟件中直接執行.
新聞熱點
疑難解答