awt - sizing frame container by its contents

A

Alex

Hello all!

I need to set correct frame size by its contents (applet). I tried
pack(), it shrinks frame making it too small.
How to size frame by its content, or how to set preferred size ?
AWT (note, AWT-only, no swing!) code:

//Main method
public static void main(String[] args) {
applet = new MicroCadApplet();
frame = new ContainerFrame();

frame.add(applet, BorderLayout.CENTER);
frame.setSize(applet.getWidth()+10,
applet.getHeight()+30);


Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);

Plz do not send me to java.sun.com tutorials, its about swing.
 
B

Boudewijn Dijkstra

Alex said:
Hello all!

I need to set correct frame size by its contents (applet). I tried
pack(), it shrinks frame making it too small.

pack() should be the right method. If it doesn't work, then the
getPreferredSize() method(s) of the contents of the frame are bad.
 
A

Andrew Thompson

Plz do not send me to java.sun.com tutorials, its about swing.

Note that there is enough commonality between Swing and the AWT to
justify doing the Swing tutorial even if you only need the AWT.

E.G. This tutoral page on layouts mentions 6 layouts, only two
of which are Swing, the rest are AWT.
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>

Further, this page on Swing painting mentions the java.awt classes
Graphics and Graphics2D, as well as PrintJob and PrinterJob
<http://java.sun.com/docs/books/tutorial/uiswing/14painting/concepts2.html>

I suspect the reason for this is that Sun is now concentrating on
documenting Swing, rather than the AWT. They seem to be aiming at
an integrated 'how to make a UI' approach in the Swing tutorial, while
only working to document the parts of the AWT -not- -covered- by the
Swing tutorial.

For a tutorial focussed on the AWT itself, you might try..
<http://java.sun.com/developer/onlineTraining/awt/contents.html>

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top