我想将下面这个applet程序转换成窗口Frame形式的程序,怎么转换呀?我不知道怎么处理按扭的事件.
请高手帮帮忙指教一下吧?
程序如下:
/******************************************
//本程序在窗口中建立三个按钮,分别完成不同的功能。
//按钮rect完成在屏幕上指定的位置画出一个指定大小的矩形,
//按钮text完成在屏幕上指定的位置显示出指定的文本,
//按钮clear完成在清屏的 功能
*/
//文件名buttondemo.java
import java.applet.*;
import java.awt.*;  //声明预定义库public class  buttondemo extends Applet
{
Button button1,button2,button3;
boolean bool1,bool2,bool3; public void init()
{
resize(320,200);
bool1=false;
bool2=false;
bool3=false; button1=new Button("rect");
button2=new Button("text");
button3=new Button("clear");
add(button1);
add(button2);
add(button3);
} public boolean action(Event e,Object o)
{
String caption=(String)o;
if(e.target instanceof Button)
{
if(caption=="rect")
{
bool1=true;
bool3=false;
repaint();
} if(caption=="text")
{
bool2=true;
bool3=false;
repaint();
} if(caption=="clear")
{
bool3=true;
bool1=false;
bool2=false;
repaint();
}
}
return true;
}
public void paint(Graphics g) 
{
if(bool1)
{g.drawRect(40,40,100,100);} if(bool2)
{g.drawString("这是一个正方形",40,160);} if(bool3)
{g.clearRect(1,1,320,200);}
}
}

解决方案 »

  1.   

    改为继承JFrame
    Component conponet=this.creatComponets();
    frame.getContentpane().add(button);显式调用init,repaint函数
      

  2.   

    不要继承JApplet 继承JPanel 然后写个Main函数 再把JPanel加入到面板里面
      

  3.   

    我也知道是这样,可是init(),repaint(),action()中的内容都怎么处理呀?
    哪位高手帮帮忙,给我写一下,我学习学习?
      

  4.   

    thinging in Java讲过这个问题的呵
    其实就是把applet放入一个frame
    然后显式的调用init() start()To create an applet that can be run from the console command line, you simply add a main( ) to your applet that builds an instance of the applet inside a JFrame.As a simple example, let’s look at Applet1b.java modified to work as both an application and an applet:
    //: c13:Applet1c.java
    // An application and an applet.
    // <applet code=Applet1c width=100 height=50>
    // </applet>
    import javax.swing.*;
    import java.awt.*;
    import com.bruceeckel.swing.*;public class Applet1c extends JApplet {
      public void init() {
        getContentPane().add(new JLabel("Applet!"));
      }
      // A main() for the application:
      public static void main(String[] args) {
        JApplet applet = new Applet1c();
        JFrame frame = new JFrame("Applet1c");
        // To close the application:
        Console.setupClosing(frame);
        frame.getContentPane().add(applet);
        frame.setSize(100,50);
        applet.init();
        applet.start();
        frame.setVisible(true);
      }
    } ///:~
      

  5.   

    import javax.swing.*;import java.applet.*;
    import java.awt.*;  //声明预定义库public class  buttondemo extends Applet
    {
    Button button1,button2,button3;
    boolean bool1,bool2,bool3; public void init()
    {
    resize(320,200);
    bool1=false;
    bool2=false;
    bool3=false; button1=new Button("rect");
    button2=new Button("text");
    button3=new Button("clear");
    add(button1);
    add(button2);
    add(button3);
    } public boolean action(Event e,Object o)
    {
    String caption=(String)o;
    if(e.target instanceof Button)
    {
    if(caption=="rect")
    {
    bool1=true;
    bool3=false;
    repaint();
    } if(caption=="text")
    {
    bool2=true;
    bool3=false;
    repaint();
    } if(caption=="clear")
    {
    bool3=true;
    bool1=false;
    bool2=false;
    repaint();
    }
    }
    return true;
    }
    public void paint(Graphics g) 
    {
    if(bool1)
    {g.drawRect(40,40,100,100);} if(bool2)
    {g.drawString("这是一个正方形",40,160);} if(bool3)
    {g.clearRect(1,1,320,200);}
    }

    public static void main(String[] args) {
    Applet applet = new buttondemo();
    JFrame frame = new JFrame("Applet1c");
    // To close the application:
    //Console.setupClosing(frame);
    frame.getContentPane().add(applet);
    frame.setSize(200,200);
    applet.init();
    applet.start();
    frame.setVisible(true);
    }
    }
      

  6.   

    谢谢楼上两位!我是在学习java,但是我现在想实现一个类似这样的程序,真正的窗口程序,而不是由applet程序生成的。就是在窗口中,点击按钮,然后实现相应的功能,
    比如:点击“rect”按钮,然后就在窗口中画出一个矩形。好像是利用事件监听,ActionListener和actionPerformed等方法来实现,再请指点?
    再次谢谢各位老师!向你们鞠躬了!