0
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
button2.setLayout(new FlowLayout(FlowLayout.RIGHT));
button1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.getContentPane().add(button1,BorderLayout.SOUTH);
frame.getContentPane().add(button2,BorderLayout.SOUTH);
frame.setSize(500,500);
frame.setVisible(true);

I'm trying to make Button 1 on the bottom left corner and Button 2 on the bottom right corner

__________________________
|                        | 
|                        |
|                        |
|                        |
|                        |
|                        |
|Button1         Button2 |
|________________________|
Charan
  • 29
  • 2
  • 8

4 Answers4

1

You might want to consider using BoxLayout's horizontalGlue:

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);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

This might get you this, before and after resizing:

enter image description here

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Would this work if i add other Buttons with different layouts – Charan Oct 11 '17 at 14:57
  • What do you mean with "different layouts"? That's why I asked for the GUI you were trying to implement – Frakcool Oct 11 '17 at 14:59
  • sorry i meant like i will be making multiple buttons some might be in the middle left or right (3 buttons on the left side, 3 jlabels on the right side) and 1 jlabel in the middle – Charan Oct 11 '17 at 15:00
  • do u want me to update the ascii to show how my gui will look like – Charan Oct 11 '17 at 15:03
  • 1) You can add as many horizontal glues as you want... [Hera are some samples](http://www.java2s.com/Code/Java/Swing-JFC/BoxLayoutGlueSample.htm) 2) *"do u want me to update the ascii to show how my gui will look like"*, Please write "you" completely, this isn't a chat page, and no, it would be better to ask a new question, but be sure to take the [tour] and read [ask] so your question fits the guidelines... also provide a [mcve] (as I did in my answer) to show what you've tried in order to solve it – Frakcool Oct 11 '17 at 15:06
0

Create a container for both.

JPanel south = new JPanel(new AxisLayout(AxisLayout.HORIZONTAL));
south.add(button1);
south.add(button2);
frame.getContentPane().add(south, BorderLayout.SOUTH);

Obs: Sorry dont remember exactly Swing layout manangers, but you will find the AxisLayout to solve this

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

Another alternative is to use the GUI builder, and modify the code accordingly.

JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JFrame frame = new JFrame();
        GroupLayout layout = new GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(button1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                        .addComponent(button2)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(256, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(button1)
                                .addComponent(button2))
                        .addGap(25, 25, 25))
        );
        frame.setSize(500, 500);
        frame.setVisible(true);

enter image description here

achAmháin
  • 4,176
  • 4
  • 17
  • 40
-2

You can add a jPanel and then add the two buttons to it, and then call setBounds on the buttons and specify the position. Then add the jPanel to the jFrame.

JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JFrame frame = new JFrame();
        JPanel p = new JPanel();
        p.setLayout(null);
        button1.setBounds(10, 400, 100, 40);
        p.add(button1);
        button2.setBounds(375, 400, 100, 40);
        p.add(button2);
        frame.getContentPane().add(p);
        frame.setSize(500, 500);
        frame.setVisible(true);

The bounds are set as (x-coord, y-coord, width, height).

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • 1
    Dont use absolute(null) layout – Marcos Vasconcelos Oct 11 '17 at 14:26
  • See this [example](https://stackoverflow.com/questions/42520492/jtable-not-showing-up-on-jframe-java/42521097#42521097) of what happens when you use `null` layout in a GUI and run it in another computer with different screen sizes – Frakcool Oct 11 '17 at 14:48