Explanation
This Java code demonstrates a fundamental approach to creating a Graphical User Interface (GUI) using Java's Abstract Window Toolkit (AWT). The program creates a simple window (referred to as a Frame) and places three interactive buttons inside it: "OK", "SUBMIT", and "CANCEL".
Step-by-step logic:
import java.awt.*;: This line brings in all the necessary classes from the AWT package, such asFrame(for the window) andButton(for the buttons).class gui1 { ... }: This defines a class namedgui1, which will encapsulate our GUI logic.gui1() { ... }: This is the constructor of thegui1class. When an object ofgui1is created, the code inside this constructor is automatically executed. This is where the GUI components are initialized and arranged.Frame f = new Frame("Button Example");: Inside the constructor, a newFrameobject is created. AFrameis a top-level window with a title bar and borders. The title of this window is set to "Button Example".Button b1 = new Button("OK");: A newButtonobject is created with the label "OK".b1.setBounds(100, 50, 80, 30);: This method is used to set the position and size ofb1manually. The arguments are(x, y, width, height). So, the "OK" button will start at x-coordinate 100, y-coordinate 50, and will be 80 pixels wide and 30 pixels tall.f.add(b1);: Theb1button is added to thefframe. Without this, the button wouldn't appear in the window.Button b2 = new Button("SUBMIT");,b2.setBounds(100, 100, 80, 30);,f.add(b2);: These lines repeat the process for a second button labeled "SUBMIT", placing it below the "OK" button.Button b3 = new Button("CANCEL");,b3.setBounds(100, 150, 80, 30);,f.add(b3);: These lines do the same for a third button labeled "CANCEL", placing it below the "SUBMIT" button.f.setSize(400, 300);: This sets the overall size of theFrameto 400 pixels wide and 300 pixels tall.f.setLayout(null);: This is a crucial line. By setting the layout manager tonull, we tell the frame that we will manually position and size all components usingsetBounds(). This is known as absolute positioning. If we didn't do this, the buttons might not appear where we intended or might stack differently based on the default layout manager.f.setVisible(true);: By default, frames are invisible. This line makes the frame (and all its added components) appear on the screen.public static void main(String[] args) { ... }: This is the entry point of any Java application.new gui1();: Inside themainmethod, a new object of thegui1class is created. This action automatically calls thegui1()constructor, which then builds and displays the GUI window with the buttons.
In essence, the code sets up a basic window and populates it with three buttons, manually arranging them in a vertical column.