So I'm just creating a simple game screen and I want to lay out my two buttons like this:
I was advised to use CardLayout (I've not worked with layouts before) so I read up on the java docs and created this:
canvas.setLayout(new CardLayout());
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();
//buttons
final JButton btn1 = new JButton(play);
btn1.setBorder(BorderFactory.createEmptyBorder());
btn1.setContentAreaFilled(false);
final JButton btn2 = new JButton(instructions);
btn2.setBorder(BorderFactory.createEmptyBorder());
btn2.setContentAreaFilled(false);
card1.add(btn1);
card2.add(btn2);
canvas.add(card1);
canvas.add(card2);
However, it produces this:
I can't seem to find any information about positioning with card layout. My guess is that I shouldn't be using this layout at all, so what layout should I use?
Card layout is used to show only one of multiple components in the layout. You can select programmatically which card to show and the rest will be hidden from view.
If you add two buttons in card layout, you can see only one. You shouldn't be using it for your specific situation. you can use Box layout with X axis
The BoxLayout manager is constructed with an axis parameter that
specifies the type of layout that will be done. There are four
choices:
X_AXIS - Components are laid out horizontally from left to right.
Y_AXIS - Components are laid out vertically from top to bottom.
LINE_AXIS - Components are laid out the way words are laid out in a
line, based on the container's ComponentOrientation property. If the
container's ComponentOrientation is horizontal then components are
laid out horizontally, otherwise they are laid out vertically. For
horizontal orientations, if the container's ComponentOrientation is
left to right then components are laid out left to right, otherwise
they are laid out right to left. For vertical orientations components
are always laid out from top to bottom.
PAGE_AXIS - Components are
laid out the way text lines are laid out on a page, based on the
container's ComponentOrientation property. If the container's
ComponentOrientation is horizontal then components are laid out
vertically, otherwise they are laid out horizontally. For horizontal
orientations, if the container's ComponentOrientation is left to right
then components are laid out left to right, otherwise they are laid
out right to left. For vertical orientations components are always
laid out from top to bottom.
Related
I'm wondering how I can add (what LayoutManager should I use) to make the app automatically adjust the components to the current size when full screen mode is enabled or even when the frame is stretched.
Currently this is what the application looks like, when you enable full screen mode the components are static.
UPDATE: I'm pasting pictures so you can get an idea of what I wrote.
Component layout in IntelliJ: layout
The current design in the small window looks ok: current design
Appearance if we enlarge the window (window zooms in, elements are static): full screen
So I'd like the components to automatically resize to the current window when fullscreen mode is enabled.
Okay. I suggest the following design:
your mainPanel, that you assigned as contentPane for your frame gets a Borderlayout.
In the North, you put a panel with a label or just a label, with Allignment X to the left.
the West gets a panel I suggest we call WestPanel and the East gets another one, which I call EastPanel.
WestPanel gets a Boxlayout or a Gridlayout. If you use Gridlayout, you can use 2 Grids wide and for each line one grid depth. You then can add all the labels in the left column and the textfields on the right.
Or you can make panels, with Borderlayout, labels to the West, textfields to the east, and pile them with emptyBorders ontop of each other. I suggest using a method for that.
The EastPabel gets a Tabbed Pane inside of it. Each tab can have its own layout manager, depending on what you want it to be in the end.
Does this help you?
I would like to have a trade-off between FlowLayout and GridBagLayout. In a nutshell, I've some components to add in my JPanel (I don't know how many of these, it's a creation at runtime) and I would like that these components to be center vertically.
FlowLayout was ideal for my goal, but it adds components at the top of JPanel. So, I decided to use GridBagConstraints, as someone suggested here (Java layout manager vertical center) but my problem with this solution is that GridBagLayout doesn't create a new line automatically, so I've just one row (vertically center, at least!!!) where my components are inserted that goes beyond the screen size. Could anyone give me some tricks about that???
I would like that these components to be center vertically. FlowLayout was ideal for my goal, but it adds components at the top of JPanel
You are never forced to use a single panel or layout manager. You can use nested panels. For example:
JPanel centered = new JPanel( choose your layout manager );
Box vertical = Box.createVerticalBox();
vertical.add( Box.createVerticalGlue() );
vertical.add( centered );
vertical.add( Box.createVerticalGlue() );
frame.add( vertical );
The glue in the vertical panel will take up equal amounts of extra space which leaves the "centered" panel vertically centered. So you can add components to the centered panel using whatever layout you want.
So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.
I have a UI requirement in java swing wherein I need to achieve the below:
The 2 buttons on the top are placed in a JPanel. I need to draw a line through the center of that panel, upto the beginning of the 2 buttons. The panel below is a container of panels arranged in a card layout. As and when the button is clicked, the card is switched showing another panel.
So in all respects this looks like a JTabbedPane, with one difference, the tabs are buttons arranged in the center of the tabbed pane. I need this difference for the UI I am building.
As of now, the buttons and card layout panel, looks like the below
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
Put the Border around the outer panel. That is use a panel with a BorderLayout. This panel can have a LineBorder. Then you add your button panel to the NORTH and the panel with the CardLayout to the CENTER.
The line won't be drawn through the buttons but the buttons and panel will appear like they represent on unit.
I'm writing a Java Applet and on one of my CardLayout Panels, the GridLayout within the BorderLayout.CENTER is streching the TextFields horizontally and vertically (this is ok because I can set vertical spacing between the parts of the grid). Can I stop the TextFields from being so wide and can I center the text within them?
Consider nesting the JTextField-containing JPanels inside of another FlowLayout using JPanel. This way the FlowLayout using JPanel is the one that gets stretched while the JTextFields are sized based on what you set the int col property to be. Note that this will fail miserably if the nesting FlowLayout using JPanel is smaller than the preferredSize of its child JPanel.