We can create a GUI using Frame in two ways:
1) By extending Frame class
2) By creating the instance of Frame class
Lets have a look at the example of each one.
1) By extending Frame class
2) By creating the instance of Frame class
Lets have a look at the example of each one.
AWT Example 1: creating Frame by extending Frame class
import java.awt.*;
/* We have extended the Frame class here,
* thus our class "SimpleExample" would behave
* like a Frame
*/
public class SimpleExample extends Frame{
SimpleExample(){
Button b=new Button("Button!!");
// setting button position on screen
b.setBounds(50,50,50,50);
//adding button into frame
add(b);
//Setting Frame width and height
setSize(500,300);
//Setting the title of Frame
setTitle("This is my First AWT example");
//Setting the layout for the Frame
setLayout(new FlowLayout());
/* By default frame is not visible so
* we are setting the visibility to true
* to make it visible.
*/
setVisible(true);
}
public static void main(String args[]){
// Creating the instance of Frame
SimpleExample fr=new SimpleExample();
}
}
Output:
creating Frame by creating instance of Frame class
import java.awt.*; public class Example2 { Example2() { //Creating Frame Frame fr=new Frame(); //Creating a label Label lb = new Label("UserId: "); //adding label to the frame fr.add(lb); //Creating Text Field TextField t = new TextField(); //adding text field to the frame fr.add(t); //setting frame size fr.setSize(500, 300); //Setting the layout for the Frame fr.setLayout(new FlowLayout()); fr.setVisible(true); } public static void main(String args[]) { Example2 ex = new Example2(); } }
Output:
No comments:
Post a Comment