Question:
(Java) I have trouble with getting the layout that i want on a really simple program.?
2010-12-21 09:13:31 UTC
i have a program with 3 classes, main , battle and window.
in battle i create a JPanel. In window i have 2 buttons and the JPanel from the battle class. I have images on my buttons. what i want is to get my JPanel to be at the top, and my buttons to be at the bottom, next to each other. But i can only get one button to show, really long and only half the image is there.

what i have is this:
add(myPanel, BorderLayout.NORTH);------------------ this is the JPanel
add(fire, BorderLayout.SOUTH);
add(heal, BorderLayout.SOUTH);

i feel like i could put co-ordinates instead of the 'BorderLayout' but i don't know how to. can I? and how?
but if anyone has a good suggestion on how to fix this problem please tell me.

(btw this is meant to be a really lame type of rpg)
Three answers:
ultimate reality1
2010-12-21 09:27:06 UTC
Create more panels. Or actually, you just need to create 1 more JPanel, attach the 2 buttons to that panel (you can use the old regular FlowLayout), and then attach that panel you just created to the south.



And I'd suggest attaching your main panel to the center instead of the north.



If you do this, then it will work:

JPanel buttonsPanel = new JPanel();

buttonsPanel.setPreferredSize (new Dimension(fixed width you want, fixed height you want));

buttonsPanel.add(fire);

buttonsPanel.add(heal);



add(buttonsPanel, BorderLayout.SOUTH);
deonejuan
2010-12-21 17:43:21 UTC
If you want coords, use VB and stay Microsoft. Java is ratios so gui thingys show up across platforms.



The Layouts work best using nested panels with their own layouts. If the user resizes the JFrame, the ratios do the best they can to resolve the original layout to the newer dimensions. A Null Layout and VB can not do that.
Neunerball
2010-12-21 17:19:18 UTC
For creating a GUI (Graphical User Interface), I'm using NetBeans. Since you can size and place buttons where you want them to be. For any other purposes, I'm using Eclipse. Both IDEs (Integrated Development Environment) are for free.



NetBeans download at: www.netbeans.org

Eclipse download at: www.eclipse.org


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...