Java Swing සමඟින් චිත්රක අතුරුමුහුණත්
GUI යනු පරිශීලකයාට බොත්තම් (buttons), පෙළ ක්ෂේත්ර (text fields), මෙනු (menus) වැනි චිත්රක අංග (visual elements) භාවිතයෙන් පරිගණක වැඩසටහනක් සමඟ අන්තර් ක්රියා කිරීමට ඉඩ සලසන අතුරු මුහුණතකි. Command-Line Interface (CLI) එකකට වඩා GUI එකක් භාවිතා කිරීමට පහසු සහ ආකර්ෂණීය වේ.
Java හි GUI applications නිර්මාණය කිරීමට ප්රධාන frameworks දෙකක් ඇත: Swing සහ JavaFX. මෙම පාඩමේදී, අපි ආරම්භකයින්ට වඩාත් පහසු වන Swing පිළිබඳව අවධානය යොමු කරමු.
අපි නිර්මාණය කිරීමට යන යෙදුම පෙනෙන ආකාරය පිළිබඳව මෙය ආදර්ශනයකි.
මෙම ව්යාපෘතිය සඳහා අපට එක් Java ගොනුවක් පමණක් අවශ්ය වේ. පහත කේතය BMICalculator.java ලෙස සුරකින්න. අපි දැන් එහි එක් එක් කොටස වෙන වෙනම අධ්යයනය කරමු.
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;
අපගේ 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
}
අපගේ යෙදුමට අවශ්ය සියලුම 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;
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);
}
"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.");
}
}
});
අවසාන වශයෙන්, අපගේ යෙදුම ආරම්භ කිරීමට `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
BMICalculator.java ලෙස ඔබගේ පරිගණකයේ එක ගොනුවක සුරකින්න (Save).javac BMICalculator.java ලෙස ටයිප් කර Enter ඔබන්න.java BMICalculator ලෙස ටයිප් කර Enter ඔබන්න.