Java how to import BorderLayout

Java how to import BorderLayout

To import BorderLayout in Java, you will need to include the following import statement at the beginning of your code:

refer to:‮tual‬turi.com
import java.awt.BorderLayout;

Here is an example of how you might use BorderLayout in your code:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;

public class BorderLayoutExample {
  public static void main(String[] args) {
    JFrame frame = new JFrame("BorderLayout Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button1 = new JButton("Button 1 (PAGE_START)");
    JButton button2 = new JButton("Button 2 (PAGE_END)");
    JButton button3 = new JButton("Button 3 (LINE_START)");
    JButton button4 = new JButton("Button 4 (LINE_END)");
    JButton button5 = new JButton("Button 5 (CENTER)");

    frame.add(button1, BorderLayout.PAGE_START);
    frame.add(button2, BorderLayout.PAGE_END);
    frame.add(button3, BorderLayout.LINE_START);
    frame.add(button4, BorderLayout.LINE_END);
    frame.add(button5, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
  }
}

This will create a JFrame with a BorderLayout, and add five buttons to the frame using the PAGE_START, PAGE_END, LINE_START, LINE_END, and CENTER constants of the BorderLayout class. The buttons will be arranged in the frame according to the layout specified by the BorderLayout.

Created Time:2017-11-03 23:27:10  Author:lautturi