🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #29113

setLayout ???

#29113
From: jsanz
Date: Thu, 15 Jan 1998 00:00
297 lines
8523 bytes
Buenas !!!

Alguien me puede poner un ejemplo de como utilizar los
ficheros de Recursos generador por el Visual J++ una vez
pasados por el Wizard Java .

Se utilizarlos para incorporarlos en un applet, pero en
una aplicacion normal no tengo claro como hacerlo, porque
utilizo el fichero DialogLayout para hacer un setLayout 
y no tira ... 

Si algun alma caritativa ...

javier@sinix.net 


---------------------------- cut  --------------------------------
// DialogLayout.java 
//
// This is a part of the Microsoft Visual J++ library.
// Copyright (C) 1996 Microsoft Corporation
// All rights reserved.

//package java.awt;

import java.util.Hashtable;
import java.awt.LayoutManager;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Label;

//
// class DialogLayout
//
// DialogLayout is a simple layout manager which works with what the
Win32
// API calls "dialog logical units" (DLUs).  DLUs are resolution
independent
// coordinates which work well for laying out controls on a dialog box. 
The
// mapping from DLUs to pixels is based on the font in use in the dialog
box.
// An x-coordinate DLU is described as 1/4 (.25) of the of the average
character
// width of the font used in the dialog.  A y-coordinate DLU is
described as
// 1/8 (.125) of the character height used in the dialog.  One tricky
issue to
// note: The average character width is not the average of all
characters --
// rather it is the average of all alpha characters both uppercase and
// lowercase. That is, it is the extent of the string "a...zA...Z"
divided
// by 52.
//
// This class allows you to associate a Rectangle (x, y, width, height)
with a
// Component in a Container.  If called upon to layout the container,
this
// layout manager will layout based on the translation of dialog units
to
// pixels.
//

public class DialogLayout
	implements LayoutManager
{
	protected Hashtable m_map = new Hashtable();
	protected int m_width;
	protected int m_height;

	// DialogLayout methods

	public DialogLayout(Container parent, int width, int height)
	{
		Construct(parent, width, height);
	}

	public DialogLayout(Container parent, Dimension d)
	{
		Construct(parent, d.width, d.height);
	}

	public void setShape(Component comp, int x, int y, int width, int
height)
	{
		m_map.put(comp, new Rectangle(x, y, width, height));
	}

	public void setShape(Component comp, Rectangle rect)
	{
		m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width,
rect.height));
	}

	public Rectangle getShape(Component comp)
	{
		Rectangle rect = (Rectangle)m_map.get(comp);
		return new Rectangle(rect.x, rect.y, rect.width, rect.height);
	}

	public Dimension getDialogSize()
	{
		return new Dimension(m_width, m_height);
	}

	// LayoutManager Methods

	public void addLayoutComponent(String name, Component comp) { }
	public void removeLayoutComponent(Component comp) { }

	public Dimension preferredLayoutSize(Container parent)
	{
		return new Dimension(m_width, m_height);
	}

	public Dimension minimumLayoutSize(Container parent)
	{
		return new Dimension(m_width, m_height);
	}

	public void layoutContainer(Container parent)
	{
		int count = parent.countComponents();
		Rectangle rect = new Rectangle();
		int charHeight = getCharHeight(parent);
		int charWidth = getCharWidth(parent);
		Insets insets = parent.insets();
		FontMetrics m = parent.getFontMetrics(parent.getFont());
		
		for (int i = 0; i < count; i++)
		{
			Component c = parent.getComponent(i);
			Rectangle r = (Rectangle)m_map.get(c);
			if (r != null)
			{
				rect.x = r.x;
				rect.y = r.y;
				rect.height = r.height;
				rect.width = r.width;
				mapRectangle(rect, charWidth, charHeight);
				if (c instanceof Label)
				{
					// Adjusts for space at left of Java labels.
					rect.x     -= 12;
					rect.width += 12;
				}

				rect.x += insets.left;
				rect.y += insets.top;
				c.reshape(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	// Implementation Helpers

	protected void Construct(Container parent, int width, int height)
	{
		Rectangle rect = new Rectangle(0, 0, width, height);
		mapRectangle(rect, getCharWidth(parent), getCharHeight(parent));
		m_width = rect.width;
		m_height = rect.height;
	}

	protected int getCharWidth(Container parent)
	{
		FontMetrics m = parent.getFontMetrics(parent.getFont());
		String s     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		int    width = m.stringWidth(s) / s.length();

		if (width <= 0)
			width = 1;
		return width;
	}

	protected int getCharHeight(Container parent)
	{
		FontMetrics m = parent.getFontMetrics(parent.getFont());
		int height = m.getHeight();
		return height;
	}

	protected void mapRectangle(Rectangle rect, int charWidth, int
charHeight)
	{
		rect.x      = (rect.x      * charWidth)  / 4;
		rect.y      = (rect.y      * charHeight) / 8;
		rect.width  = (rect.width  * charWidth)  / 4;
		rect.height = (rect.height * charHeight) / 8;
	}
}

---------------------------- end  --------------------------------



---------------------------- cut  --------------------------------

//------------------------------------------------------------------------------
// Ventana.java:
//		Implementation of "control creator" class Ventana
//------------------------------------------------------------------------------
import java.awt.*;
import DialogLayout;

public class Ventana
{
	Container    m_Parent       = null;
	boolean      m_fInitialized = false;
	DialogLayout m_Layout;

	// Control definitions

//--------------------------------------------------------------------------
	Button        Valido;
	Button        Cancelar;
	TextField     Texto;


	// Constructor

//--------------------------------------------------------------------------
	public Ventana (Container parent)
	{
		m_Parent = parent;
	}

	// Initialization.

//--------------------------------------------------------------------------
	public boolean CreateControls()
	{
		// CreateControls should be called only once
	
//----------------------------------------------------------------------
		if (m_fInitialized || m_Parent == null)
			return false;

		// m_Parent must be extended from the Container class
	
//----------------------------------------------------------------------
		if (!(m_Parent instanceof Container))
			return false;

		// Since a given font may not be supported across all platforms, it
		// is safe to modify only the size of the font, not the typeface.
	
//----------------------------------------------------------------------
	    Font OldFnt = m_Parent.getFont();
	    if (OldFnt != null)
	    {
			Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 8);

    		m_Parent.setFont(NewFnt);
	    }

		// All position and sizes are in dialog logical units, so we use a
		// DialogLayout as our layout manager.
	
//----------------------------------------------------------------------
		m_Layout = new DialogLayout(m_Parent, 186, 95);
		m_Parent.setLayout(m_Layout);
		m_Parent.addNotify();

		Dimension size   = m_Layout.getDialogSize();
		Insets    insets = m_Parent.insets();
		
		m_Parent.resize(insets.left + size.width  + insets.right,
                        insets.top  + size.height + insets.bottom);

		// Control creation
	
//----------------------------------------------------------------------
		Valido = new Button ("OK");
		m_Parent.add(Valido);
		m_Layout.setShape(Valido, 30, 65, 50, 14);

		Cancelar = new Button ("Cancel");
		m_Parent.add(Cancelar);
		m_Layout.setShape(Cancelar, 91, 65, 50, 14);

		Texto = new TextField ("");
		m_Parent.add(Texto);
		m_Layout.setShape(Texto, 15, 7, 152, 52);

		m_fInitialized = true;
		return true;
	}
}

---------------------------- end  --------------------------------
Technical Details
Message-ID: <34BDCE7A.33EA@sinix.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1116f3,e337f8f616cb7464,start
X-Google-Attributes: gid1116f3,public
From: jsanz <javier@sinix.net>
Subject: setLayout ???
Date: 1998/01/15
Message-ID: <34BDCE7A.33EA@sinix.net>
X-Deja-AN: 316125089
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=us-ascii
Organization: sni
Mime-Version: 1.0
Newsgroups: es.comp.lenguajes.java