|
Java Program To Demonstrate Grid Layout |
Algorithm Steps:
Step 1: Write an applet tag with code property set to class name and comment it
Step 2: import all the necessary packages
Step 3: Define a class which extends Applet
Step 4: Declare any number of buttons
Step 5: In the init() method do the following:
i) Create a panel
ii) create the buttons
iii) set the layout of the panel to Gridlayout
iv) add the buttons to the panel
v) set the layout of the applet to borderlayout
vi) add the panel to the center of the border layout
Step 6: Write empty paint() method
Java Program To Demonstrate Grid Layout
import java.applet.*;
import java.awt.*;
public class panels extends Applet
{
Panel values;
TextField red = new TextField(5);
TextField green = new TextField(5);
TextField blue = new TextField(5);
Canvas mixture;
public void init()
{
setLayout(new GridLayout(1,2,10,10));
mixture = new Canvas();
mixture.setBackground(new Color(0,0,0));
red.setText("0");
green.setText("0");
blue.setText("0");
values = new Panel();
values.setLayout(new GridLayout(3,2,10,10));
values.add(new Label("Red Value:"));
values.add(red);
values.add(new Label("Green Value:"));
values.add(green);
values.add(new Label("Blue Value:"));
values.add(blue);
add(values);
add(mixture);
}
public boolean action(Event evt, Object whatAction)
{
if((evt.target instanceof TextField))
{
setcolor();
return true;
}
return false;
}
public void setcolor()
{
String r = red.getText();
String g = green.getText();
String b = blue.getText();
Color c = new Color(Integer.parseInt(r),Integer.parseInt(g),Integer.parseInt(b));
mixture.setBackground(c);
mixture.repaint();
}
public Insets insets()
{
return new Insets(10,10,10,10);
}
}
SAMPLE OUTPUT SCREEN:
|
|
⇓ Student Projects ⇓
⇑ Student Projects ⇑
|