Java Program to place component in bottom-right corner with BorderLayout


We have created a button component here, which will places in bottom-right corner −

JButton button = new JButton("This is Demo Text!");
button.setBackground(Color.blue);
button.setForeground(Color.white);

Create panels now to arrange the above created button component in the bottom right −

JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(button, BorderLayout.LINE_END);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bottomPanel, BorderLayout.PAGE_END);

The following is an example to place a component in bottom-right corner with BorderLayout −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingDemo {
   public static void main(String[] args) {
      JButton button = new JButton("This is Demo Text!");
      button.setBackground(Color.blue);
      button.setForeground(Color.white);
      JPanel bottomPanel = new JPanel(new BorderLayout());
      bottomPanel.add(button, BorderLayout.LINE_END);
      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
      // mainPanel.setPreferredSize(new Dimension(550, 400));
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.setSize(550, 400);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

828 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements