Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to add a Button to a Frame using Swing API in Java ?

Program to demonstrate adding of Button to a Frame

package com.hubberspot.awtSwing.example;

import java.awt.FlowLayout;
import javax.swing.*;

public class AddButtonToFrame
{
  JFrame frame;
  JButton button;
 
  AddButtonToFrame()
  {
 frame = new JFrame("Welcome to HubberSpot Frame");
 button = new JButton("Click Me!");
 frame.setSize(200,100);
 frame.setLayout(new FlowLayout());
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setVisible(true);
  }
  
  public static void main(String[] args)
 {
 new AddButtonToFrame();
 }
}
Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot