How to Create Button on Frame

In this section, you will learn how to create Button on frame the topic of Java AWT package.

How to Create Button on Frame

In this section, you will learn how to create Button on frame the topic of Java AWT package.

How to Create Button on Frame

How to Create Button on Frame

     

In this section, you will learn how to create Button on  frame the topic of Java AWT package. In the section, from the generated output of the following program, complete window or frame is created in the ButtonText class that is the main class where the main() method lies. This part of the topic will show a simple command button labeled with the text "Submit" when the program will be executed.

Here, you will learn simply the procedure of creation a command button on the Java Awt Frame. There is a program for the best illustration the topic has been given ahead.

Description of Program

The following program has used simply the Button class for creating a command button on the frame object.

FlowLayout(): This is the constructor of FlowLayout class. This class is used for arranging the component from left to right. Here this is used for creating the object of the class to set the layout by passing the created object from the setLayout() method of the Frame class.

Here is the code of  this program:

import java.awt.*; 
import java.awt.event.*;
 

public class ButtonText 
  public static void main(String[] args) {
  Frame frame=new Frame("Button Frame");
  Button button = new Button("Submit")
  frame.add(button)
  frame.setLayout(new FlowLayout());
  frame.setSize(200,100);
  frame.setVisible(true);
  frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
  });
  }
}

Output this program:

  

Download this Example: