Java Tutorial
In java programming, we can also use different layouts in JFrame to create a Graphical user interface (GUI) using JFrame class.
In Java JFrame, need to set the layout to add the components.
setLayout(new BorderLayout());
add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("Center"),BorderLayout.CENTER); add(new JButton("West"), BorderLayout.WEST); add(new JButton("East"), BorderLayout.EAST);
// importing swing package for JFrame, Jbutton controls import javax.swing.*; // importing to use Container class import java.awt.*; public class BorderLayoutTest extends JFrame { public BorderLayoutTest(){ // Setting Title to the JFrame GUI setTitle("Border Layout Test Java Code"); // Setting border layout to JFrame GUI setLayout(new BorderLayout()); // Adding buttons using BorderLayout add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("Center"), BorderLayout.CENTER); add(new JButton("West"), BorderLayout.WEST); add(new JButton("East"), BorderLayout.EAST); } public static void main(String[] args){ // Create JFrame for GUI frame and title as 'Add Button in JFrame Java Code' BorderLayoutTest frame = new BorderLayoutTest(); frame.setSize(500,200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }Output:
Once executing this java program, Frame with border layout buttons will be popped up as above and closes if we click close button in the frame title.
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page