Java GUI program to develop a frame of 400*400 size with yellow background and text displayed at the centre

Wednesday, April 15, 2015




import java.awt.*;
import java.awt.event.*;
class MyFrame1 extends Frame
{
public MyFrame1(String title)
{
super(title); /*to access the constructor 
of frame class*/
this.setSize(400,400); //set size of frame
this.setBackground(Color.yellow); //set colour of frame
this.setVisible(true);
MyWindowAdapter obj=new MyWindowAdapter();
this.addWindowListener(obj);

}
public void paint(Graphics g)
{
Font f=new Font("Arial",Font.BOLD,24);
g.setFont(f);
g.drawString("Good Evening",120,200);
}
}
class Testf
{
public static void main(String[] args)
{
MyFrame1 fr=new MyFrame1("Sourabh Frame");
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e) //to close the frame
{
System.exit(0); //otherwise frame will not close
}
}

No comments:

Post a Comment