Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am aware of the methods and coding required to set a JFrame item's position and size, but I need a bit of help knowing how to correctly size and position items. Is there a way to use another JFrame API/method to separate the window into sections to make positioning items easier? What are the maximum values for height and width? How do I set a button to an average size? If you have some experience with JFrames, please give me some info about this to help me understand how to position and size JFrame items.
You shouldn't be worrying about pixel perfect positioning of components. Swing wasn't meant to be used as such. Being a language that is meant to be run on many different platforms, the GUI library should be flexible as such. To maintain the flexibility, layout managers are introduced. You should be using these layout managers do the sizing and positioning for you.
You can see a visualization of how each work at A Visual Guide to Layout Managers. With these layout managers, you'll want to learn (at the very least)
Which ones respect preferred size and which ones don't.
how each ones are represented visually.
How to create white space, using gaps, empty borders, struts
How use use nested containers with different layout managers to get your desired result.
It may intimidating with all the different possibilities, but like learning anything new, take it one step at a time. Go through each tutorial for each layout manager.
Related
This question already has an answer here:
Positioning component inside card layout
(1 answer)
Closed 5 years ago.
I have read this SO Question regarding this problem, but I am still a little lost on how to freely move and position swing components. I am new to Swing, and so I am still learning the ins and outs of it. I have read other documentation and articles online, but I have yet to see anything that shows how to move components around. I have even tried things set as setBounds, but it has been to no avail.
What are some ways I can position components in the CardLayout? Any help is appreciated.
Your question doesn't make sense, in that it's asking something akin to, can I move an elevator sideways. No, you can't. A CardLayout is for swapping components, and that's all it can do. The JPanel that holds it is given a preferredSize by the layout determined by the maximal preferred sizes of the contained components. This layout is not for positioning, translating, or scaling components, and for these functionalities you'd need to use other layout managers. Fortunately you can nest panels, each using its own layout manager and thereby move or translate components as your use of layouts see fit.
For more detailed answer, consider creating and posting your minimal example program.
And in fact, the question that you linked to answers this much more comprehensively, and so I've made this answer a community wiki and have closed your question as a duplicate. I strongly urge you to read or re-read the Layout Manager Tutorial.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm creating a java application.there I want to keep jframe in maximized state.and I want to use it many of monitors that have different screen size.and I want to components in jframe to resize with jframe.also I need to add image to the background of the frame.that image also should resize with the jframe. how to do that?
Layout managers, layout managers, layout managers
Start by taking a look at
A Visual Guide to Layout Managers
How to Use Various Layout Managers
Using Layout Managers
You might also want to have a look at Full-Screen Exclusive Mode API, depending on what you're hoping to achieve
also I need to add image to the background of the frame.that image also should resize with the jframe. how to do that?
Java: maintaining aspect ratio of JPanel background image
How to set a background picture in JPanel
How do I resize images inside an application when the application window is resized?
I see lots of searching and research in your future.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I created a small program which is shown in the link. And I used
absolute layout. But I want to try other layouts. Are there any alternative layouts to use apart from absolute layout ?
Here is the link
As a different opinion, I wouldn't recommend GridBagLayout. It is the most flexible so you can do pretty much anything with it but it is pretty hard to use. A simpler way is to use a composed approach, meaning multiple panels in your main UI where each panel has its own layout. For example, for the two top components you can use a vertical BoxLayout and for the bottom part you can use a FormLayout
But the point is, it is better IMO to mix and match layouts than to use one single super flexible layout for everything. Check this link for more info. They mention what I recommended:
Layout Managers are often mixed together in a single frame or dialog,
where a top level container has its own Layout Manager (often a
BoxLayout or BorderLayout), and smaller parts use their own layout,
completely independent of the others.
Another nice tip is to use a GUI builder such as Eclipse's WindowBuilder or Netbean's Matisse; that way you don't design UIs in code, which is pretty abstract
Use gridbaglayout. To properly use it, read first about gridbagconstraints.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been trying to work out how could i achieve such a layout of labels, but was unsuccessful in all of my tries.
Could you give me an idea on how to achieve such a layout of labels in java?
You can nest panels with different layout managers to achieve your desired results. This can simplify the layout in many cases.
However, in this case you might be able to use a GridBagLayout. The GridBagLayout allows you to specify the row/column of each component. Start with a two column grid.
The first and second labels will take two columns on two separate rows.
Then the next four labels will each take up one column on two separate rows.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm using gridbaglayout and I want my window to be 700 x 500 always. I also was my JButtons to be the exact same size but if the word inside them is longer they get longer and I can't have this. How can I do this? Please help me with good code I been trying for hours.
I tried to do frame.setSize and panel.setSize but neither work and I need this fast! Please help!
The basic answer is, you don't. That's not the point of any layout manager.
A layout manager simple makes decisions about how best to layout it's children based on the sizing hints that they provide.
Remember, while it might look great on your screen, the next computer you run it on may make it look like crap.
If you "must" define the size of anything, then you need to override the getPreferredSize method of your component and return an appropriate size hint.
Having said that, I wouldn't do this for components like JButton (or actually anything other than JPanel and JComponent), the way they calculate their sizes are complicated and best left alone.
You can modify the size of components through the use of Borders and, in the case of GridBagLayout, Insets and modifying the GridBagConstraints properties.
Have a closer look at How to Use GridBagLayout for some more ideas