2

Below is the code, which i got from Creating two buttons at bottom left/right corner and i have included my attempt,(adding jpanel2)

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonsLeftAndRight {
    private JFrame frame;
    private JPanel pane;
    private JButton button1;
    private JButton button2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");

        pane.add(button1);
        pane.add(Box.createHorizontalGlue());
        pane.add(button2);

        frame.add(pane, BorderLayout.SOUTH);
        JButton b1 = new JButton ("A");
        JButton b2 = new JButton ("B");
        JButton b3 = new JButton ("C");
        JPanel panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel2.add(b1);
        panel2.add(Box.createVerticalGlue());
        panel2.add(b2);
        panel2.add(Box.createVerticalGlue());
        panel2.add(b3);
        frame.add(panel2, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The problem is that BoxLayout can't be shared but i need to somehow do it so that i can get the vertical boxes

I'm trying to create BoxLayout GUI which involves using buttons and JLabels. In the below ASCII B represents buttons an J represents JLabel. Also im only keeping a bit space between buttons and JLabels

__________________________
|                        | 
|      B       J         |
|      B       J         |
|      B       J         |
|          J             |
|                        |
|Button1         Button2 |
|________________________|
Charan
  • 29
  • 2
  • 8
  • 2
    What have you tried? You're throwing requirements to us, but have not shown effort into solving it yourself. Please [edit] your post with a [mcve] as I requested in the comments of my answer in your previous question – Frakcool Oct 11 '17 at 15:46
  • @Frakcool i updated it – Charan Oct 11 '17 at 15:56
  • I guess you haven't even read the links I posted... That isn't a MCVE... [Minimal, **Complete**, and Verifiable Example](https://stackoverflow.com/help/mcve) while it is minimal, it is not complete, it lacks imports, a `main` method and variable declarations... If you want help from us, show some effort into doing it yourself first please, otherwise your question might get down voted and / or closed as it lacks the MCVE – Frakcool Oct 11 '17 at 16:01
  • @Frakcool i just updated it, does it follow MCVE now – Charan Oct 11 '17 at 16:03
  • A MCVE is similar to the code I posted in your previous question, you can actually copy-paste it and see the same output as me – Frakcool Oct 11 '17 at 16:05
  • can i copy paste ur code and add my attempt to it – Charan Oct 11 '17 at 16:08
  • 1
    Use nested panels with different layout managers to achieve your desired layout. For example you create a panel with a horizontal BoxLayout for buttons 1 and 2. Then you can add to panel to another panel that uses a different layout manager. – camickr Oct 11 '17 at 16:21
  • 1
    *"can i copy paste ur code and add my attempt to it"* Of course you can, that's why I posted, but add your attempt to it. Also as already said by @camickr you can use other layout manager for another `JPanel` for the `JButtons` + `JLabel` and one for both buttons at the bottom... – Frakcool Oct 11 '17 at 16:25
  • @Frakcool im thinking of using gbc for the 3 labels and 3 buttons but the only problem is how do i create gridbaglayout using panel2. can i update the question with my attempt fro gridbaglayout and then u guys can tell me whats wrong – Charan Oct 11 '17 at 16:41
  • That's what you should be doing actually – Frakcool Oct 11 '17 at 16:47
  • got it thanks, it works :) – Charan Oct 11 '17 at 16:48

2 Answers2

1

You can have many JPanels, each with a different layout.

For this case I can imagine having GridBagLayout for the panel with buttons and labels which will be centered in the JFrame's BorderLayout.CENTER alignment and having the buttons at the bottom with BoxLayout orientation

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class ButtonsLeftAndRight {
    private JFrame frame;
    private JPanel bottomPane;
    private JPanel centerPane;
    private JButton button1;
    private JButton button2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        bottomPane = new JPanel();
        bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.LINE_AXIS));

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");

        bottomPane.add(button1);
        bottomPane.add(Box.createHorizontalGlue());
        bottomPane.add(button2);

        frame.add(bottomPane, BorderLayout.SOUTH);
        JButton b1 = new JButton("A");
        JButton b2 = new JButton("B");
        JButton b3 = new JButton("C");
        
        centerPane = new JPanel();
        centerPane.setLayout(new GridBagLayout());
        
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        centerPane.add(b1, gbc);
        
        gbc.gridx = 2;
        centerPane.add(new JLabel("Label 1"), gbc);
        
        gbc.gridx = 0;
        gbc.gridy++;
        centerPane.add(b2, gbc);
        
        gbc.gridx = 2;
        centerPane.add(new JLabel("Label 2"), gbc);
        
        gbc.gridx = 0;
        gbc.gridy++;
        centerPane.add(b3, gbc);
        
        gbc.gridx = 2;
        centerPane.add(new JLabel("Label 3"), gbc);
        
        gbc.gridx = 1;
        gbc.gridy++;
        centerPane.add(new JLabel("Centered Label"), gbc);
        
        frame.add(centerPane, BorderLayout.CENTER);
        frame.add(bottomPane, BorderLayout.SOUTH);
        
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

We have the labels positioned in the gridx = 2 so we can have the bottom label in the gridx = 1 position and this provides the extra space between the JLabels and the JButtons with the space equals to the length of the bottom label.

This gives us the following GUI before and after resizing:

enter image description here

enter image description here

Of course there might be some other ways of doing it, however this is the first one I thought


Note

  • You added setVisible(true) twice, once is enough
  • frame.setSize(500, 500); vs frame.pack(); use one or the other, not both.
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

Try to make this between adding jbutton and jlabel.

container.add(Box.createRigidArea(new Dimension(5,0)));
Jurgiele
  • 95
  • 1
  • 10