Buttons in JFrames
I am making a game for my computer science class and cannot figure out how to place buttons in the JFrame where I want using dimensions. For instance, I do now want to use the gridbag or spring layouts. I want to be able to individually place all my buttons and labels excacly where I want them. Here is a sample code that does not place them where I want.
I imported a lot of stuff because I didn't know what I needed.
public class d
public static void main(String args)
JButton button1= new JButton("Start!");
JButton button2= new JButton("Score!");
JFrame frame= new JFrame();
I agree, avoid Spring and GridBagLayout if at all possible, but don't avoid using layouts in general. I suggest that you read the Swing tutorial section called Laying out Components in a Container, where you'll learn about all the easier to use layouts such as BoxLayout, BorderLayout, GridLayout, and FlowLayout. You'll find that it is far easier to nest JPanels each using a simple layout to get your JButtons and components just where you want to place them. You'll also learn about using a "null" layout if you want to do absolute positioning -- but even though this can be done, I don't recommend doing it in general.
Originally Posted by dane1193
I suggest you start here: Laying Out Components in a Container
I have a question for you fubarable, I am fairly new and was wondering if you are discouraging the use of grid bag because it is more difficult, or is it just not considered good to use? Sorry to ask this in someone else's post.
I believe he is discouraging the use of GridBag due to it being difficult to learn/use, especially for a newcomer. For more experienced users then Gridbag becomes very powerful and they could probably whip up a great looking GUI in fewer lines than a n00b using other layout managers.
Yep. Also, there are some good substitutes for GridBagLayout such as MIgLayout, though you'll have to download this.
Originally Posted by Junky