Practical 3: Write a program to design simple calculator with the use of GridLayout

1) Write a program to demonstrate Grid of 5 * 5
Ans:
import java.awt.*;
public class GridDemo 
{
	public static void main( String args[] )
	{
		Frame f = new Frame();
		f.setVisible(true);
		f.setSize(400,400);
		f.setLayout(new GridLayout(2,2));
		
		Font font = new Font("TimesRoman",Font.BOLD,25);
		f.setFont(font);

		Label l[] = new Label[25];		
	
		for(int i = 0 ; i < 25 ; i++)
		{
			String s = "";
			s = s.valueOf(i+1);
			Color c = new Color(i,i+10,i+20);
			l[i] = new Label();
			System.out.println(c);
			l[i].setBackground(c);
			l[i].setText(s);
		}
		
		for(int i = 0 ; i < 25;i++)
		{
			f.add(l[i]);
		}
	}
}


Output:
GridLayout Console Output


2)Write a program to generate following output using Border Layout.

Ans:
import java.awt.*;
public class BorderDemo
{
	public static void main( String args[] )
	{
		Frame f = new Frame();
		f.setVisible(true);
		f.setSize(400,400);
		f.setLayout(new BorderLayout());
		
		Button b1 = new Button("North");	
		Button b2 = new Button("South");	
		Button b3 = new Button("East");	
		Button b4 = new Button("West");	
		Button b5 = new Button("Center");	
		
		f.add(b1,BorderLayout.NORTH);
		f.add(b2,BorderLayout.SOUTH);
		f.add(b3,BorderLayout.EAST);
		f.add(b4,BorderLayout.WEST);
		f.add(b5,BorderLayout.CENTER);
	}
}


Previous
Next Post »