Button fills up the frame window completely..what am I doing wrong?

I am trying to open a frame from an applet and I have put a button in the frame .

But , it fills up the frame window..

How can I make it into normal sized button..?

code is given below..

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

class SampleFrame extends Frame {

String msg=" ";

Button b2;

SampleFrame(String title) {

super(title);

b2 = new Button("Iterate");

add(b2);

b2.addActionListener(this);

MyWindowAdapter adapter=new MyWindowAdapter(this);

addWindowListener(adapter);

}

public void paint(Graphics g)

{

g.drawString("This is in frame window", 10, 40);

}

}

class MyWindowAdapter extends WindowAdapter {

SampleFrame sampleFrame;

public MyWindowAdapter(SampleFrame sampleFrame) {

this.sampleFrame = sampleFrame;

}

public void windowClosing(WindowEvent we) {

sampleFrame.setVisible(false);

}

}

public class appframe extends Applet implements ActionListener {

Frame f;

Button b1;

public void init() {

b1 = new Button("Enter");

add(b1);

b1.addActionListener(this);

f = new SampleFrame("A Frame Window");

f.setSize(250, 250);

setBackground(Color.cyan);

}

public void stop()

{

f.setVisible(false);

}

public void actionPerformed(ActionEvent ae){

String str = ae.getActionCommand();

if(str.equals("Enter"))

f.setVisible(true);

}

public void paint(Graphics g)

{

g.drawString("This is in applet window", 10, 20);

}

}

[1681 byte] By [help.mea] at [2007-11-26 19:23:54]
# 1
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
CeciNEstPasUnProgrammeura at 2007-7-9 21:46:01 > top of Java-index,Java Essentials,Java Programming...
# 2

> I am trying to open a frame from an applet and I have

> put a button in the frame .

>

> But , it fills up the frame window..

> How can I make it into normal sized button..?

Welcome to the wonderful world of LayoutManagers. The button is the

only thing (Component) present in the Frame, and if you resize your

Frame, the Button takes up whatever space is available.

You can either:

- remove the LayourManager from the Frame and control the position

and size of your Button yourself (which, IMHO is a Very Bad Idea (tm))

or:

- add other components to your frame so your Button has to share the

total available space in the Frame. Have a look at the Box class

that serves as a Factory for several (invisible) Components that do take

up space.

kind regards,

Jos

JosAHa at 2007-7-9 21:46:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Learn how to use the forum correctly:

a) Quit multi-posting questions:

http://forum.java.sun.com/thread.jspa?threadID=5140233

http://forum.java.sun.com/thread.jspa?threadID=5140230

This is an AWT question and only needs to be posted in that forum.

b) Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 21:46:01 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks a lot guys...and CeciNEstPasUnProgrammeur...u just love to send these links ..don't u :)but they help..so thanks again.. :)and thanks Jos..
help.mea at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 5
... or change to another layout manager. E.g. BridbagLayoutKaj
kajbja at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Abt the multiposting thing ..I am sorry...but I was not getting a reply on those forums..so then finally posted it here..Sorry!
help.mea at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> Abt the multiposting thing ..I am sorry...but I was

> not getting a reply on those forums..so then finally

> posted it here..

>

> Sorry!

You should in that case say that the question is cross-posted, and post a link to the original question.

Kaj

kajbja at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 8

> but I was not getting a reply on those forums..

The question was only posted 30 minutes ago. Be patient.

So have you replied to those postings stating you have an answer so people don't waste their time attempting to answer the question when they do get around to reading the posting?

camickra at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 9
> and CeciNEstPasUnProgrammeur...u just love to send> these links ..don't u :)Yes - why should I re-type all the stuff that's already said there? As a programer you should know that duplication is bad.I wish more people would be like you and actually read
CeciNEstPasUnProgrammeura at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...
# 10

>Yes - why should I re-type all the stuff that's already said there?

> As a programer you should know that duplication is bad.

yup,no need to retype what already exists.

>I wish more people would be like you and actually read them.

If wishes were horses ... :)

help.mea at 2007-7-9 21:46:02 > top of Java-index,Java Essentials,Java Programming...