4, ఏప్రిల్ 2012, బుధవారం

GRIDBAGLAYOUT AND ITS INTERNAL METHODS AND OPTATIONS IMPLEMENTATION

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.htmlfor the scenarios of all the layouts in java click the below link  follow the net beans tool
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
IN THIS CODE I HAVE EXPLAINED GRIDBAG LAYOUT BY DIVIDING A FRAME INTO 3 PARTS IN THE FIRST ROW I PLACED 3 BUTTON 2ND ROW ONLY BUTTON4 IN THE 3RD ROW FROM MIDDLE LOCATION TO THE RIGHT END I PLACED THE BUTTON5
import java.awt.*;
import javax.swing.*;
class gbagdemo extends JFrame
{
GridBagLayout gbag;
GridBagConstraints cons;
gbagdemo()
{
Container c=getContentPane();
gbag=new GridBagLayout();
c.setLayout(gbag);
JButton b1=new JButton("Button1");
JButton b2=new JButton("Button2");
JButton b3=new JButton("Button3");
JButton b4=new JButton("Button4");
JButton b5=new JButton("Button5");
cons=new GridBagConstraints();
cons.fill=GridBagConstraints.HORIZONTAL;
cons.gridx=0;
cons.gridy=0;
cons.weightx=0.7;
gbag.setConstraints(b1,cons);
c.add(b1);

cons.gridx=1;
cons.gridy=0;
gbag.setConstraints(b2,cons);
c.add(b2);

cons.gridx=2;
cons.gridy=0;
gbag.setConstraints(b3,cons);
c.add(b3);

cons.gridx=0;
cons.gridy=1;
cons.ipady=100;
cons.gridwidth=3;
gbag.setConstraints(b4,cons);
c.add(b4);

cons.gridx=1;
cons.gridy=2;
cons.ipady=0;
cons.weighty=1.0;
cons.anchor=GridBagConstraints.PAGE_END;
cons.gridwidth=2;
gbag.setConstraints(b5,cons);
c.add(b5);
}
public static void main(String args[])
{gbagdemo demo=new gbagdemo();
demo.setSize(800,400);
demo.show();
}}

program-2

IN THIS PROGRAM I CONCENTRATED ABOUT THE SOME ADDITIONAL METHODS FOR THE LAYOUT IMPLEMENTATIONS
import java.awt.*;   
import javax.swing.*;
class boxdemo extends JFrame
{
boxdemo()
{
JLabel l1=new JLabel("enter name");
JTextField t1=new JTextField(20);

t1.setMaximumSize(t1.getPreferredSize());
Box h1=Box.createHorizontalBox();
h1.add(l1);
h1.add(Box.createHorizontalStrut(20));
h1.add(t1);




JLabel l2=new JLabel("enter password");
JTextField t2=new JTextField(20);


t2.setMaximumSize(t2.getPreferredSize());
Box h2=Box.createHorizontalBox();
h2.add(l2);
h2.add(Box.createHorizontalStrut(20));
h2.add(t2);

JButton b1=new JButton("ok");
JButton b2=new JButton("cancel");

Box h3=Box.createHorizontalBox();
h3.add(b1);
h3.add(Box.createGlue());
h3.add(b2);


Box v=Box.createVerticalBox();
v.add(h1);
v.add(Box.createVerticalStrut(200));
v.add(h2);
v.add(Box.createVerticalStrut(200));
v.add(h3);

Container c=getContentPane();
c.add(v);
}

public static void main(String args[])
{
boxdemo bd=new boxdemo();
bd.setSize(500,400);
bd.show();
}}
output:-
If u runs this program it will produce the output with center two labels and text feilds and at the bottom two corners two buttons like OK, cancel






కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి