Friday, March 6, 2015

Applet Frame

// Create a child frame window from within an applet.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletFrame" width=400 height=600>
</applet>
*/
// Create a subclass of Frame.
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
// create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
}
public void paint(Graphics g) {
g.drawString("This is in frame window", 10, 40);
}
}
class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}
}
// Create frame window.
public class AppletFrame extends Applet implements ActionListener  {

 Button okButton;
TextField nameField;

Frame f;
public void init() {

f = new SampleFrame("A Frame Window");


okButton = new Button("Action!");

 nameField = new TextField("Type here Something",35);


add(okButton);


okButton.addActionListener(this);



}
public void start() {

}
public void stop() {
f.setVisible(false);
}


 public void actionPerformed(ActionEvent evt)
         {
  // Here we will ask what component called this method
              if (evt.getSource() == okButton)
{


f.setSize(150, 150);
f.setVisible(true);


f.add(nameField);
nameField.setText("hello");
//nameField.setText("hello"));
}


}

public void paint(Graphics g) {
g.drawString("This is in applet window", 15, 30);
}

}

No comments: