How to vertically align with 100% width? - java

How to accomplish vertical alignment -- JPanels on top of each other with 100% width size?
I want something like this but am unclear how to go about it.

Setting a BoxLayout LayoutManager should help you to align vertically (see Y_AXIS) .

One simple way you can use BorderLayout for that:
frame = new JFrame();
frame.setBounds(100, 100, 495, 311);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JPanel jPanel = new JPanel();
frame.getContentPane().add(jPanel, BorderLayout.CENTER);

Related

Adding Multiple Panels with Different Sizes in One Frame

I am trying to add more panels to my frame, but the program seems to ignore all the other panels than the first one I added. How should I add the panels?
I have checked other people's questions and their answers, but none of them seemed to be the solution to mine.
frame = new JFrame("Hey");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mid = new JPanel(new GridLayout(7,7));
JPanel top = new JPanel();
frame.add(top);
frame.add(mid);
frame.pack();
frame.setVisible(true);
The program ignores the "top" panel, along with the buttons I added to it.
From the JFrame documentation:
The default content pane will have a BorderLayout manager set on it.
So you should use the BorderLayout regions in order to add your JPanels to the JFrame's content pane like this:
frame.add(top, BorderLayout.NORTH);
frame.add(mid, BorderLayout.CENTER);
Otherwise, the BorderLayout will default to adding everything to the CENTER region:
As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
and you will only see the JPanel added last because:
Each region may contain no more than one component

Align JButtons in center of nested JPanel (BoxLayout)

I have a JFrame's child class and have the followiwng layout inside it. I have one big panel and one small buttonsPanel with two JButtons. I add buttons to the smaller panel and add that panel to the first one. Buttons are supposed to be centered, but it doesn't happen.
panel=new JPanel();
add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1=new JButton("button1");
JButton button2=new JButton("button2");
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(button1, CENTER_ALIGNMENT);
buttonsPanel.add(button2, CENTER_ALIGNMENT);
panel.add(buttonsPanel, BorderLayout.CENTER);
What should I do?
You really need to read the Swing tutorial on Layout Managers. You need to understand what a "constraint" is and when to use it.
buttonsPanel.add(button1, CENTER_ALIGNMENT);
The buttons panel uses a BoxLayout. It does not support any constraint, so the CENTER_ALIGNMENT makes no sense.
panel.add(buttonsPanel, BorderLayout.CENTER);
Again, panel uses a BoxLayout. You can't just use a BorderLayout constraint.
The easiest way to center a component (vertically and horizontally on a frame is to use a GridBagLayout.
So the basic code might be something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(button1);
buttonsPanel.add(button2);
frame.setLayout( new GridBagLayout() );
frame.add(buttonsPanel, new GridBagConstraints());
If you want to try to use a BoxLayout then you need to use "glue" before and after the panel:
Box vertical = Box.createVerticalBox();
vertical.add(Box.createVerticalGlue());
vertical.add(buttonsPanel);
vertical.add(Box.createVerticalGlue());
Again, read the tutorial for more basic information about the BoxLayout.

Swing display JPanels in a row to the right corner

I am creating a custom decoration for my first customized Swing program window, I just started with layout managers, and it looks like I am doing something wrong, first I used BorderLayout and BorderLayout.EAST or WEST to display on the corner, but it only allows one panel to be displayed on a corner, like it won't display in a row.
Looks like this:
(source: gyazo.com)
With that code:
this.panel.setLayout(new BorderLayout());
this.panel.add(this.createToolButton("X"), BorderLayout.EAST);
But if I add another panel, the newest panel will be on the previous one (Note I used panels because JButton hates me, with it's default styles doesn't let me make it flat)
Now I used GridBagLayout
this.panel.setLayout(new GridBagLayout());
Box panels = new Box(BoxLayout.X_AXIS);
panels.add(this.createToolButton("X"));
this.panel.add(panels, BorderLayout.EAST);
But on run I get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
What am I doing wrong? how can I have the panels floated to right one by one?
EDIT:
this.panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.WEST;
this.panel.add(this.createToolButton("X"), gc);
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
Use GridBagConstraint with GridBagLayout.
this.panel.setLayout(new GridBagLayout());
GridBagConstraint gc = new GridBagConstraint();
// set different properties of GridBagConstraint as per your need
this.panel.add(panels, gc);
Read more How to Use GridBagLayout read more about properties of GridBagConstraint.
Here is The Example to learn more about it.
EDIT
You can try with FlowLayout with right alignment:
JPanel titlePanel=new JPanel(new GridLayout(1,2));
titlePanel.setBorder(new LineBorder(Color.BLACK));
titlePanel.setBackground(Color.LIGHT_GRAY);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
panel.setBackground(Color.LIGHT_GRAY);
titlePanel.add(new JLabel("Title",JLabel.LEFT));
panel.add(new JButton("X"));
titlePanel.add(panel);
frame.add(titlePanel, BorderLayout.NORTH);
// add panel in the north section of the undecorated JFrame
// that by default uses BorderLayout
snapshot:
If you are using
this.panel.add(panels, BorderLayout.EAST);
then you should use BorderLayout not GridBagLayout.
this.panel.setLayout(new BorderLayout());
You can read more in the documentation How to Use BorderLayout.

Component on JPanel not showing in original size

I made a basic game on a JFrame and I'm currently trying to add a scoreboard on top my frame by using two separate JPanels. I tried to do it with WindowBuilder but the problem is that my GameFrame class component isn't shown fully in the game frame. It looks like this:
The code is as follows:
JFrame frame = new JFrame("Game");
frame.setSize(500, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 100, 500, 600);
panel.add(new GameFrame());
frame.getContentPane().add(panel);
frame.setVisible(true);
How can I solve the problem?
A JPanel uses FlowLayout by default which respects preferred sizes. As its unlikely that this is currently overridden for your GameFrame component class, you need to use a layout manager which uses the maximum area available
panel.setLayout(new BorderLayout());
I think setting the layout to null is this issue. Set a proper Layout as shown below
frame.setLayout(new GridLayout(1,1));)
Have you tried one of these: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html ?
If so, what's not working?
A BorderLayout could be useful if you want the scoreboard along the top, by using panel.add(new ScoreBoard(), BorderLayout.PAGE_START) for example.

Why is this JPanel not sticking to the specified size?

So, I'm trying to learn Java Swing and custom components. I've created a JFrame, given it a background color, and added a JPanel:
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 2000);
frame.setBackground(Color.WHITE);
JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setSize(40, 40);
frame.add(jp);
frame.setVisible(true);
The result is a 1000x2000 window colored blue (as opposed to a white window with a 40x40 blue box inside it). Why is the JPanel expanding beyond its specified size?
Using your code, simply add one line to change the LayoutManager of the JFrame. Then, when you add the component, it will keep it's preferred size.
Also, instead of calling jp.setSize(40,40), call jp.setPreferredSize(new Dimension(40,40)).
And, you need to call pack() on the JFrame to tell it to layout its components.
JFrame frame = new JFrame("Testing");
frame.setLayout(new FlowLayout()); // New line of code
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 2000)); // Modified line of code
frame.setBackground(Color.WHITE);
JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setPreferredSize(new Dimension(40, 40)); // Modified line of code
frame.add(jp);
frame.pack(); // added line of code
frame.setVisible(true);
Also, you should read up on all of the different LayoutManagers available to you. Here is a great tutorial.
The default layout manager for a JFrame is BorderLayout. When you add a component to the frame without constraints, it uses BorderLayout.CENTER as the default constraint. This means the component takes up all available space, regardless of its requested size.

Categories

Resources