This program, as you must have figured out, will display five buttons in the window. Owing to the list() , in the DOS mode there will be a display of the current components and current layout. We suggest whenever you want to see the layout-related information, the list() comes in quite handy. Getting on with the layouts, just for the record, the following layouts are given to you by Java...(and if there are more, it'll probably take us a little more sleuthing)import java.applet.*; import java.awt.*; public class zzz extends Applet { Button a,b,c,d,e; public void init() { a = new Button("aa"); b = new Button("bb"); c = new Button("cc"); d = new Button("dd"); e = new Button("ee"); add(a); add(b); add(c); add(d); add(e); list(); } }
As you must have noticed, it adds the button to the different borders and the center button takes up all the space. The mouseUp() does not work because of the whole center being occupied. Scrap off the center button e , and the mouseUp() function will miraculously start working. Also note that if the positions (north, south etc.) are not mentioned in the add(), you will not see any effect.public void init() { setLayout(new BorderLayout()); a=new Button("aa"); b=new Button("bb"); c=new Button("cc"); d=new Button("dd"); e=new Button("ee"); add("North",a);add("South",b);add("West",c);add("East",d); add("Center",e); list(); } public boolean mouseUp(Event e, int x, int y) { list(); return true; }
Now consider this program to be sturdier with the concept of flow layouts...setLayout(new FlowLayout());
import java.applet.*; import java.awt.*; public class zzz extends Applet { Button a, b, c, d, e; public void init() { setLayout(new FlowLayout()); a = new Button("aa"); b = new Button("bb"); c = new Button("cc"); d = new Button("dd"); e = new Button("ee"); add(a); add(b); add(c); add(d); add(e); list(); } }
That will give an error "Constructor of GridLayout not found in java.awt.GridLayout". This means that there are more parameters to be passed to the GridLayout constructor. And these are the rows and columns.setLayout(new GridLayout());
setLayout(new GridLayout(2,3));
setLayout(new CardLayout());
Now when you run this program, all the buttons appear clubbed together. If we want some spacing, something called the weightx has to be given a value. This, by default, is zero.import java.applet.*; import java.awt.*; public class zzz extends Applet { protected void f(String s, GridBagLayout l, GridBagConstraints r) { Button n = new Button(s); l.setConstraints(n, r); add(n); } public void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); f("aa", g, i); f("bb", g, i); f("cc", g, i); f("dd", g, i); f("ee", g, i); } }
This command after the setLayout() . Now the buttons will appear seperated with a gap of 1.0 units.i.weightx=1.0;
GridBagConstraints fill - BOTH/HORIZONTAL/VERTICAL - stretches the button to cover up the spaces between the buttons that came as a result of the weightx . The buttons become larger and are joined. The dd button takes the remaining space on the screen. The ee button - new row touching both the vertical sides because the gridwidth=REMAINDERpublic void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); i.fill=GridBagConstraints.BOTH; i.weightx=1.0; f("aa",g,i); f("bb",g,i); f("cc",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("dd",g,i); f("ee",g,i); }
The noteworthy points in this program are BOTH, REMAINDER and RELATIVE. Try to figure out how they work; We'll be here with the elaboration soon.public void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); i.fill=GridBagConstraints.BOTH; i.weightx=1.0; f("aa",g,i); f("bb",g,i); f("cc",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("dd",g,i); i.weightx=0.0; i.gridwidth=GridBagConstraints.RELATIVE; f("ee",g,i); }
public void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); i.fill=GridBagConstraints.BOTH; i.weightx=1.0; f("aa",g,i); f("bb",g,i); f("cc",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("dd",g,i); i.weightx=0.0; i.gridwidth=GridBagConstraints.RELATIVE; f("ee",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("ff",g,i); }
public void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); i.fill=GridBagConstraints.BOTH; i.weightx=1.0; f("aa",g,i); f("bb",g,i); f("cc",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("dd",g,i); i.gridwidth=1; i.gridheight=2; i.weighty=1.0; f("ee",g,i); }
After all this, let us have one for the Cardlayout as well...public void init() { GridBagLayout g = new GridBagLayout(); GridBagConstraints i = new GridBagConstraints(); setLayout(g); i.fill=GridBagConstraints.BOTH; i.weightx=1.0; f("aa",g,i); f("bb",g,i); f("cc",g,i); i.gridwidth=GridBagConstraints.REMAINDER; f("dd",g,i); i.gridwidth=1; i.gridheight=2; i.weighty=1.0; f("ee",g,i); i.gridwidth=GridBagConstraints.REMAINDER; i.weighty=0.0; i.gridheight=1; f("ff",g,i); f("gg",g,i); }
import java.awt.*; import java.applet.Applet; class ccc extends Panel { ccc() { Panel p; p = new Panel(); p.setLayout(new BorderLayout()); p.add("North", new Button("one")); p.add("West", new Button("two")); Panel q; q = new Panel(); q.setLayout(new FlowLayout()); q.add("North", new Button("one")); q.add("West", new Button("two")); Panel r; r = new Panel(); r.setLayout(new GridLayout(2,1)); r.add("North", new Button("one")); r.add("West", new Button("two")); setLayout(new CardLayout()); add("one", p); add("two", q); add("third",r); } } public class zzz extends Applet { ccc c; public zzz() { setLayout(new BorderLayout()); c=new ccc(); add("Center", c); Panel p = new Panel(); p.setLayout(new FlowLayout()); add("South", p); p.add(new Button("first")); p.add(new Button("next")); p.add(new Button("previous")); p.add(new Button("last")); } public boolean action(Event e, Object o) { if ("first".equals(o)) { ((CardLayout)c.getLayout()).first(c); } else if ("next".equals(o)) { ((CardLayout)c.getLayout()).next(c); } else if ("previous".equals(o)) { ((CardLayout)c.getLayout()).previous(c); } else if ("last".equals(o)) { ((CardLayout)c.getLayout()).last(c); } return true; } }