Reputation:
I'm creating an applet and having problems with the positioning as size of my buttons. I've added two buttons, but the "OK" button seems to position and size itself correctly, but the "CLEAR" button fills the entire applet area behind the "OK" button. Any suggestions as to what is the problem?
@Override
public void init()
{
super.init();
setSize(J_WIDTH, J_HEIGHT);
setLayout(new BorderLayout());
btn_OK = new Button("OK");
btn_CLEAR = new Button("CLEAR");
btn_OK.setBounds(50, 450, 75, 50);
btn_CLEAR.setBounds(125, 50, 75, 50);
add(btn_OK);
add(btn_CLEAR);
btn_OK.addActionListener(this);
btn_CLEAR.addActionListener(this);
}
Upvotes: 2
Views: 365
Reputation: 83517
When using a BorderLayout
, you should specify a location where you want to place the component. If you don't, the default is BorderLayout.CENTER
. Also, each position can only contain one component. So when you call add(btn_OK)
, the OK button is added to the center of the panel. But then you replace it with the Clear button by calling add(btn_CLEAR);
.
In addition, each position in the BorderLayout
takes up a certain amount of space. The component at that position will stretch to fill that space. In particular, the CENTER takes up all remaining space not used by the other positions.
I think that BorderLayout
is not what you want here. Check out the Visual Guide to Layout Managers for more information on each LayoutManager. You can also follow the rest of the tutorial trail for details about how to implement each of them.
You should also bookmark and familiarize yourself with the Java API docs. These are an essential tool for every Java programmer and will help you answer many questions on your own.
Upvotes: 1