views:

38

answers:

2

Hello I have a Problem with some Buttons

I would like to create a simple GUI with JButtons but when I start the App the Buttons Positions and Size changes every Time when i start the App. I use this Code:

    usr_in.setSize(120,40);

usr_in.setLocation(10,40); usr_in is a JButton

Created Like this:

JButton usr_ro = new JButton("Users");

and here I set them:

menu.setLayout(new GridLayout(1, 1, 0, 3));
menu.add(usr_ro);

I use the Libs:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.beans.*;
import java.applet.*;
import java.*;

thanks for help

A: 

First off, two questions: what is the type of menu? And is usr_in the same as usr_ro? The names obviously aren't the same, but your wording implies that they are meant to be the same.

From what you've provided so far, the problem seems to be that you're trying to set the location of the button(s) in two different ways. In one place, you're using setLocation(), which "is specified by the x and y parameters in the coordinate space of this component's parent." (source)

Later, you're using a GridLayout, which sets locations in its own way: fitting them to the grid. (source) I don't think the behavior is well-defined if you apply both, though I am a little surprised that you're not getting the same unwanted behavior every time.

Lord Torgamus
yes usr_ro is usr_in and thanks it works now ^^
TheAsker
A: 

I find the best practice is to put your button(s) in a JPanel, which uses a FlowLayout. Your button shouldn't varies in size if you're not fiddling too much with the parent container layout.

Nik
yes thats a good idea thanks
TheAsker