Module 11 – Introduction to GUI Programming

Java Swing සමඟින් චිත්‍රක අතුරුමුහුණත්

GUI (Graphical User Interface) යනු කුමක්ද?

GUI යනු පරිශීලකයාට බොත්තම් (buttons), පෙළ ක්ෂේත්‍ර (text fields), මෙනු (menus) වැනි චිත්‍රක අංග (visual elements) භාවිතයෙන් පරිගණක වැඩසටහනක් සමඟ අන්තර් ක්‍රියා කිරීමට ඉඩ සලසන අතුරු මුහුණතකි. Command-Line Interface (CLI) එකකට වඩා GUI එකක් භාවිතා කිරීමට පහසු සහ ආකර්ෂණීය වේ.

Java හි GUI applications නිර්මාණය කිරීමට ප්‍රධාන frameworks දෙකක් ඇත: Swing සහ JavaFX. මෙම පාඩමේදී, අපි ආරම්භකයින්ට වඩාත් පහසු වන Swing පිළිබඳව අවධානය යොමු කරමු.


ව්‍යාපෘතිය: සරල BMI කැල්කියුලේටරය (HTML පෙරදසුන)

අපි නිර්මාණය කිරීමට යන යෙදුම පෙනෙන ආකාරය පිළිබඳව මෙය ආදර්ශනයකි.

BMI Calculator

කේතය පියවරෙන් පියවර පැහැදිලි කිරීම

මෙම ව්‍යාපෘතිය සඳහා අපට එක් Java ගොනුවක් පමණක් අවශ්‍ය වේ. පහත කේතය BMICalculator.java ලෙස සුරකින්න. අපි දැන් එහි එක් එක් කොටස වෙන වෙනම අධ්‍යයනය කරමු.

පියවර 1: අවශ්‍ය Libraries Import කිරීම

GUI අංග (components) සෑදීම සඳහා javax.swing.* ද, බොත්තම් click කිරීම වැනි සිදුවීම් (events) හැසිරවීමට java.awt.event.* ද අපි import කරගත යුතුය.

// Import necessary classes from the Swing and AWT libraries
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

පියවර 2: ප්‍රධාන Class එක නිර්මාණය කිරීම

අපගේ BMICalculator class එක, Swing හි ඇති JFrame class එකෙන් extend කරමු. JFrame යනු යෙදුමක ප්‍රධාන කවුළුව (window) නියෝජනය කරන class එකයි. මෙසේ extend කිරීමෙන්, අපගේ class එකට කවුළුවක සියලුම ගුණාංග උරුම වේ.

// Our main class inherits from JFrame to become a window
public class BMICalculator extends JFrame {
    // ... code will go here
}

පියවර 3: GUI Components ප්‍රකාශයට පත් කිරීම

අපගේ යෙදුමට අවශ්‍ය සියලුම GUI අංග (Labels, Text Fields, Button) class එක තුළ attributes ලෙස ප්‍රකාශයට පත් කරමු. private ලෙස යෙදීමෙන්, මෙම අංග වලට පිවිසිය හැක්කේ මෙම class එක තුළින් පමණක් බව සහතික කරයි.

    // Declare all the GUI components we will need
    private JLabel weightLabel, heightLabel, resultLabel;
    private JTextField weightField, heightField;
    private JButton calculateButton;

පියවර 4: Constructor එක තුළ GUI එක නිර්මාණය කිරීම

Object එකක් සාදන විට ක්‍රියාත්මක වන constructor එක තුළ, අපි කවුළුව සකස් කර, සියලුම components සාදා, ඒවා කවුළුවට එකතු කරමු.

    // The constructor is where we build the GUI
    public BMICalculator() {
        // --- A. Set up the main window (JFrame) ---
        setTitle("BMI Calculator"); // කවුළුවේ தலைക്കെട്ട്
        setSize(350, 250); // කවුළුවේ ප්‍රමාණය (පළල, උස)
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // කවුළුව වැසූ විට යෙදුම නැවතීම
        setLayout(null); // අපි components වල පිහිටීම අතින් සකසන බව කියයි

        // --- B. Create and add the components ---
        // එක් එක් component එක සාදා, setBounds මගින් එහි පිහිටීම (x, y) සහ ප්‍රමාණය (width, height) ලබා දී,
        // add() මගින් එය කවුළුවට එකතු කරයි.
        weightLabel = new JLabel("Weight (kg):");
        weightLabel.setBounds(30, 30, 100, 30);
        add(weightLabel);

        weightField = new JTextField();
        weightField.setBounds(140, 30, 150, 30);
        add(weightField);

        heightLabel = new JLabel("Height (m):");
        heightLabel.setBounds(30, 70, 100, 30);
        add(heightLabel);
        
        heightField = new JTextField();
        heightField.setBounds(140, 70, 150, 30);
        add(heightField);

        calculateButton = new JButton("Calculate BMI");
        calculateButton.setBounds(100, 120, 150, 30);
        add(calculateButton);

        resultLabel = new JLabel("Your BMI will be shown here.");
        resultLabel.setBounds(30, 160, 300, 30);
        add(resultLabel);

        // Make the window visible at the end
        setVisible(true);
    }

පියවර 5: බොත්තම සඳහා ක්‍රියාකාරීත්වය එක් කිරීම (Event Handling)

"Calculate" බොත්තම click කළ විට කුමක් විය යුතුද යන්න අපි දැන් ලියමු. ඒ සඳහා ActionListener එකක් භාවිතා කරයි. මෙම listener එක බොත්තම click කරන තෙක් "බලා සිටින" අතර, click කළ විට එහි actionPerformed method එක ක්‍රියාත්මක කරයි.

        // This part goes inside the constructor, after adding components
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // This code runs ONLY when the button is clicked
                try {
                    // Get text from text fields and convert to a number (double)
                    double weight = Double.parseDouble(weightField.getText());
                    double height = Double.parseDouble(heightField.getText());

                    // Calculate BMI (weight / (height * height))
                    double bmi = weight / (height * height);

                    // Display the result in the label, formatted to two decimal places
                    resultLabel.setText(String.format("Your BMI is: %.2f", bmi));

                } catch (NumberFormatException ex) {
                    // If the user enters text instead of a number, this catch block runs
                    resultLabel.setText("Please enter valid numbers.");
                }
            }
        });

පියවර 6: `main` Method එක නිර්මාණය කිරීම

අවසාන වශයෙන්, අපගේ යෙදුම ආරම්භ කිරීමට `main` method එක අවශ්‍ය වේ. මෙහිදී, අපි සෑදූ BMICalculator class එකේ object එකක් නිර්මාණය කරයි. එවිට, constructor එක ක්‍රියාත්මක වී, අපගේ GUI කවුළුව තිරයේ දිස්වේ.

    // The main method to run our application
    public static void main(String[] args) {
        // This creates an instance of our BMICalculator, which in turn runs the constructor
        new BMICalculator();
    }
} // End of the BMICalculator class

කේතය ක්‍රියාත්මක කරන ආකාරය

  1. ඉහත සියලුම කේත කොටස් එකතු කර, BMICalculator.java ලෙස ඔබගේ පරිගණකයේ එක ගොනුවක සුරකින්න (Save).
  2. ඔබගේ Terminal (Command Prompt) එක විවෘත කර, ඔබ ගොනුව සුරැකූ ෆෝල්ඩරය වෙත යන්න.
  3. පළමුව, ගොනුව compile කරන්න. Terminal එකේ javac BMICalculator.java ලෙස ටයිප් කර Enter ඔබන්න.
  4. Compile වීම සාර්ථක නම්, වැඩසටහන ක්‍රියාත්මක කිරීමට Terminal එකේ java BMICalculator ලෙස ටයිප් කර Enter ඔබන්න.
  5. දැන් ඔබට BMI කැල්කියුලේටරයේ කවුළුව (window) දිස්වනු ඇත!