Explanation
This Java code creates a simple Graphical User Interface (GUI) application using the Swing toolkit. The application is designed to display a window where a user can select known languages using checkboxes.
Here's a step-by-step breakdown:
- Import Swing Library:
import javax.swing.*;brings in all the necessary classes from the Swing library, which provides components for building GUIs (like windows, buttons, labels, etc.). - Define the GUI Class: The
gui3class serves as the container for our GUI logic. - Constructor
gui3(): This special method runs automatically when an object ofgui3is created. All the GUI setup happens inside this constructor.- Create Main Window (JFrame):
JFrame f = new JFrame("Language Selector");creates the main window of our application. It's titled "Language Selector" and will be the top-level container for all other GUI elements. - Create Instruction Label (JLabel):
JLabel l1 = new JLabel("Select known Languages:");creates a static text label that displays the instruction "Select known Languages:". - Position Label:
l1.setBounds(100, 50, 200, 30);manually sets the position and size of the label.(100, 50)are the x and y coordinates (from the top-left corner of the frame), and(200, 30)are the width and height in pixels. - Add Label to Frame:
f.add(l1);places the label inside our main window (f). - Create Checkboxes (JCheckBox):
JCheckBox c1 = new JCheckBox("English");creates a checkbox labeled "English".JCheckBox c2 = new JCheckBox("Hindi");creates a checkbox labeled "Hindi".JCheckBox c3 = new JCheckBox("Konkani");creates a checkbox labeled "Konkani".
- Position Checkboxes: Similar to the label,
setBounds()is used for each checkbox (c1,c2,c3) to place them manually below the label, stacking them vertically. For example,c1.setBounds(100, 100, 100, 30);places the "English" checkbox at (100, 100) with a size of 100x30 pixels. - Add Checkboxes to Frame:
f.add(c1);,f.add(c2);,f.add(c3);add each checkbox to the main window. - Set Frame Size:
f.setSize(400, 400);sets the initial dimensions of the main window to 400 pixels wide and 400 pixels high. - Disable Layout Manager:
f.setLayout(null);is a crucial line. It tells theJFramenot to use any automatic layout manager. This means we are entirely responsible for positioning and sizing every component usingsetBounds(). - Set Default Close Operation:
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);configures the frame so that when the user clicks the close button (usually 'X' in the window's title bar), the Java application will terminate completely. - Make Frame Visible:
f.setVisible(true);makes theJFrameand all its contained components appear on the screen. Until this line is executed, the window is created but remains hidden.
- Create Main Window (JFrame):
mainMethod: This is the entry point of any Java application.public static void main(String args[])callsnew gui3();, which creates an instance of ourgui3class. This action, in turn, executes thegui3()constructor, setting up and displaying the GUI.
In essence, this program constructs a simple window with a title, an instructional message, and three checkboxes for language selection, all positioned manually.