please dont rip this site

Language Java Nosugar UI Code Layoutdemo.java

// 
//	LayoutDemo class
//	part of the set of documents known as Java no sugar.
//	Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk
//	placed into the public domain by the author
//
import java.awt.*;
import java.applet.*;

public class LayoutDemo extends Applet
{
	//*******************************************************************
	// globals
	//*******************************************************************
	static final String GRID = "GRID";
	static final String GRIDBAG = "GRIDBAG";
	static final String BORDER = "BORDER";
	static final String FLOW = "FLOW";
	static final String CARD = "CARD";
	
	//*******************************************************************
	// instance variables
	//*******************************************************************
	Button grid_button, gridbag_button, border_button, flow_button;
	Panel cards;
	
	//*******************************************************************
	// applet methods
	//*******************************************************************
		public boolean action(Event event, Object arg)
		{
			CardLayout layout;
			
			if (event.target == grid_button)
			{
			layout = (CardLayout) cards.getLayout();
			layout.show(cards, GRID);
			return true;
			}
			else if (event.target == gridbag_button)
			{
			layout = (CardLayout) cards.getLayout();
			layout.show(cards, GRIDBAG);
			return true;
			}
			else if (event.target == border_button)
			{
			layout = (CardLayout) cards.getLayout();
			layout.show(cards, BORDER);
			return true;
			}
			else if (event.target == flow_button)
			{
			layout = (CardLayout) cards.getLayout();
			layout.show(cards, FLOW);
			return true;
			}
			return(false);
			
			
		}	
	
	public void init()
	{
		String style;
		
		style = getParameter("STYLE");
		
		if (style == null)				System.exit(1);
		else if (style.equals(GRID)	)	init_grid(this);
		else if (style.equals(GRIDBAG))	init_gridbag(this);
		else if (style.equals(BORDER))	init_border(this);
		else if (style.equals(FLOW)	)	init_flow(this);
		else if (style.equals(CARD)	)	init_card(this);
	}

	//*******************************************************************
	// private methods
	//*******************************************************************
	private void init_grid( Container container )
	{
		GridLayout layout;
		
		// ------------------create layout manager ----------------------
		layout = new GridLayout(3,3);
		container.setLayout(layout);

		// ------------------add some buttons ----------------------
		container.add ( new Button("components") );
		container.add ( new Button("are") );
		container.add ( new Button("laid") );
		container.add ( new Button("out") );
		container.add ( new Button("left to right") );
		container.add ( new Button("top to bottom"));
		container.add ( new Button("and") );
		container.add ( new Button("each have"));
		container.add ( new Button("the"));
		container.add ( new Button("same size"));
	}

	private void init_gridbag( Container container)
	{
		GridBagLayout layout;
		GridBagConstraints	c;
		Button	thing;
		
		
		// ------------------create layout manager ----------------------
		layout = new GridBagLayout();
		container.setLayout(layout);

		// ------------------add some buttons ----------------------
		c = new GridBagConstraints();
		thing = new Button ("GridBagLayout");
		c.gridx = 0; c.gridy = 0; c.gridwidth=2; c.gridheight=1;
		c.fill = GridBagConstraints.HORIZONTAL;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("gives");
		c.gridx = 3; c.gridy = 0; c.gridwidth=1; c.gridheight=1;
		c.fill = GridBagConstraints.NONE;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("better");
		c.gridx = 0; c.gridy = 2; c.gridwidth=1; c.gridheight=5;
		c.fill = GridBagConstraints.VERTICAL;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("control");
		c.gridx = 1; c.gridy = 2; c.gridwidth=1; c.gridheight=1;
		c.fill = GridBagConstraints.NONE;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("over");
		c.gridx = 1; c.gridy = 3; c.gridwidth=1; c.gridheight=1;
		c.fill = GridBagConstraints.NONE;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("Positioning");
		c.gridx = 1; c.gridy = 4; c.gridwidth=2; c.gridheight=1;
		c.fill = GridBagConstraints.BOTH;
		layout.setConstraints(thing,c);
		container.add(thing);

		c = new GridBagConstraints();
		thing = new Button ("and");
		c.gridx = 2; c.gridy = 5; c.gridwidth=1; c.gridheight=1;
		c.fill = GridBagConstraints.BOTH;
		layout.setConstraints(thing,c);
		container.add(thing);
		
		c = new GridBagConstraints();
		thing = new Button ("size");
		c.gridx = 2; c.gridy = 6; c.gridwidth=2; c.gridheight=1;
		c.fill = GridBagConstraints.BOTH;
		layout.setConstraints(thing,c);
		container.add(thing);
	}

	private void init_border(Container container)
	{
		BorderLayout layout;
		TextArea textarea;
		
		// ------------------create layout manager ----------------------
		layout = new BorderLayout();
		container.setLayout(layout);

		// ------------------add some buttons ----------------------
		container.add ( "North",	new Button("North"));
		container.add ( "East",		new Button("East"));
		container.add ( "South",	new Button("South"));
		container.add ( "West",	new Button("West"));
		
		textarea = new TextArea();
		textarea.setEditable(false);
		textarea.appendText("Center\n-----\n\n");
		textarea.appendText("BorderLayout can contain\n");
		textarea.appendText("a maximum of 5 components.\n");
		textarea.appendText("The usefulness of these is limited.\n");
		textarea.appendText("In fact I cant think of even one\n");
		textarea.appendText("example where BorderLayout\n");
		textarea.appendText("would be better than GridBagLayout\n");
		container.add ( "Center",	textarea);
	}

	private void init_flow(Container container)
	{
		FlowLayout layout;
		
		// ------------------create layout manager ----------------------
		layout = new FlowLayout();
		container.setLayout(layout);

		// ------------------add some buttons ----------------------
		container.add ( new Button("components") );
		container.add ( new Button("are") );
		container.add ( new Button("laid") );
		container.add ( new Button("out") );
		container.add ( new Button("left to right") );
		container.add ( new Button("top to bottom") );
		container.add ( new Button("unlike GridLayout") );
		container.add ( new Button("components"));
		container.add ( new Button("don't") );
		container.add ( new Button("resize") );
	}

	private void init_card(Container container)
	{
		Panel controller_panel, card;
		
		// --------------- create a panel with the controlling buttons----
		controller_panel = new Panel();
		controller_panel.setLayout( new FlowLayout());
		grid_button = new Button(GRID);
		gridbag_button = new Button(GRIDBAG);
		flow_button = new Button(FLOW);
		border_button = new Button(BORDER);
		controller_panel.add( grid_button);
		controller_panel.add( gridbag_button);
		controller_panel.add( flow_button);
		controller_panel.add( border_button);
		
		// --------------- panel below has the card layout ----------------
		cards = new Panel();
		cards.setLayout ( new CardLayout() );
		
		card = new Panel();
		init_flow(card);
		cards.add(FLOW, card);
		
		card = new Panel();
		init_grid(card);
		cards.add(GRID, card);
		
		card = new Panel();
		init_gridbag(card);
		cards.add(GRIDBAG, card);

		card = new Panel();
		init_border(card);
		cards.add(BORDER, card);
		
		// ------------------create layout manager ----------------------
		this.setLayout(new GridLayout(2,1));
		this.add(controller_panel);
		this.add(cards);
	}
}

file: /Techref/language/java/nosugar/ui/code/LayoutDemo.java, 7KB, , updated: 1996/10/10 10:11, local time: 2024/11/14 10:19,
TOP NEW HELP FIND: 
18.191.130.149:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/language/java/nosugar/ui/code/LayoutDemo.java"> language java nosugar ui code LayoutDemo</A>

Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .