Java Swing GUI හැදීම: පියවරෙන් පියවර Simple Guide එකක්

Java Swing: GUI Programming වලට පාර කියන පියවරෙන් පියවර අත්පොත
ආයුබෝවන් යාළුවනේ! අද කාලේ ගොඩක් අය කියනවා desktop applications හදන එක පරණ දෙයක් කියලා. මොකද දැන් හැමෝම web apps, mobile apps පස්සේ දුවන නිසා. ඒත් ඇත්තටම එහෙමද? බලන්න, තාමත් ගොඩක් business applications, utilities, specialized tools හදන්නේ desktop එකටම තමයි. ඒ වගේම programming ලෝකයට අලුතින් එන අයට GUI (Graphical User Interface) concept එක හරියට තේරුම් ගන්න Java Swing කියන්නේ නියම තෝතැන්නක්. මොකද මේකෙන් අපිට graphical elements පාවිච්චි කරලා users ලාට interact කරන්න පුළුවන් applications හදන්න පුළුවන්. ඒ නිසා අද අපි බලමු Java Swing පාවිච්චි කරලා කොහොමද ලස්සන, user-friendly GUI applications හදන්නේ කියලා. කිසිම කලබලයක් නැතුව, මුල ඉඳන්ම අපි පියවරෙන් පියවර ඉගෙන ගමු.
Swing කියන්නේ මොකක්ද? (What is Swing?)
සරලවම කිව්වොත්, Swing කියන්නේ Java Foundation Classes (JFC) වල කොටසක්. මේකෙන් අපිට Java වල GUI applications හදන්න පුළුවන් components set එකක් ලැබෙනවා. Button, Text Field, Label වගේ දේවල් තමයි මේ components. Swing වල තියෙන විශේෂත්වය තමයි මේවා platform independent වීම. ඒ කියන්නේ ඔයා Windows වල code කරන application එකක් කිසිම වෙනසක් නැතුව Mac OS වලත්, Linux වලත් run කරන්න පුළුවන්. ඒ වගේම Swing components lightweight. ඒ කියන්නේ මේවා operating system එකේ native components පාවිච්චි කරන්නේ නැතුව, Java code වලින්ම draw කරන නිසා වැඩි customization options තියෙනවා, ඒ වගේම performace අතින් ගොඩක් හොඳයි. AWT (Abstract Window Toolkit) කියන්නේ Swing වලට කලින් තිබුණු framework එක. ඒක heavyweight, ඒ කියන්නේ OS components පාවිච්චි කරනවා. Swing ආවේ AWT වලට තිබුණු අඩුපාඩු පිරිමහන්න. ඉතින් අපි දැන් මේ වටිනාකම තේරුම් ගත්තා නේද?
පළමු Swing Window එක (Your First Swing Window)
හරි, දැන් අපි අපේ පළමු Swing application එක හදමු. හැම GUI application එකකටම වගේ මුලින්ම ඕනේ window එකක්. Java Swing වල මේකට අපි JFrame
කියන class එක පාවිච්චි කරනවා. මේක තමයි අපේ application එකේ ප්රධාන window එක.
import javax.swing.JFrame;
public class MyFirstSwingApp {
public static void main(String[] args) {
// 1. Create a JFrame object
JFrame frame = new JFrame("My First Swing Window");
// 2. Set the size of the frame
frame.setSize(400, 300); // Width, Height in pixels
// 3. Set what happens when the user closes the window
// EXIT_ON_CLOSE means the application will terminate when the window is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 4. Make the frame visible
frame.setVisible(true);
System.out.println("Window Created!"); // Just to confirm in console
}
}
මේ code එක run කරලා බලන්න. ඔයාට “My First Swing Window” කියලා title එකක් එක්ක පුංචි window එකක් දකින්න ලැබෙයි. මෙතන අපි කරන්නේ:
JFrame
class එකෙන් object එකක් හදාගන්නවා. Constructor එකට title එකක් දෙන්න පුළුවන්.setSize()
method එකෙන් window එකේ පළලයි, උසයි (width, height) set කරනවා.setDefaultCloseOperation()
කියන්නේ user කෙනෙක් window එක close කරද්දි මොකද වෙන්න ඕනේ කියලා.JFrame.EXIT_ON_CLOSE
කියන්නේ application එකම terminate වෙන්න කියලා.- අන්තිමට
setVisible(true)
කියන්නේ window එක screen එකේ පෙන්වන්න කියලා. මේක නැත්නම් window එක හැදුනත් පේන්නේ නැහැ.
Components එකතු කරමු (Adding Components)
දැන් අපි window එකක් හදාගත්තා. ඒත් මේක හිස් window එකක්. අපිට මේකට Buttons, Labels, Text Fields වගේ දේවල් එකතු කරන්න ඕනේ. Swing වලදී, components frame එකට add කරන්නේ Container
එකක් හරහා. සාමාන්යයෙන් මේකට අපි JPanel
එකක් පාවිච්චි කරනවා. JPanel
කියන්නේ components grouping කරන්න, ඒවාට layout එකක් දෙන්න පාවිච්චි කරන light-weight container එකක්. අපි JPanel
එකක් හදලා ඒක frame එකේ content pane එකට add කරලා, පස්සේ අපේ components ඒ JPanel
එකට add කරනවා. මෙතනදී Layout Managers ගැනත් පොඩි අවධානයක් දෙන්න වෙනවා. Layout Managers තමයි අපේ components window එක ඇතුලේ කොහොමද arrange වෙන්නේ කියලා තීරණය කරන්නේ. FlowLayout
, BorderLayout
, GridLayout
වගේ ඒවා ප්රධාන Layout Managers කිහිපයක්. මේ example එකේ අපි FlowLayout
පාවිච්චි කරමු. මේකෙන් components ටික වම් පැත්තෙ ඉඳන් දකුණු පැත්තට එකින් එක පෙළ ගැසෙනවා.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.FlowLayout; // For FlowLayout
public class MySwingComponentsApp {
public static void main(String[] args) {
JFrame frame = new JFrame("My Components App");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JPanel
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout()); // Set FlowLayout for the panel
// Create a JLabel
JLabel label = new JLabel("Enter your name:");
// Create a JTextField
JTextField textField = new JTextField(20); // 20 columns wide
// Create a JButton
JButton button = new JButton("Submit");
// Add components to the panel
panel.add(label);
panel.add(textField);
panel.add(button);
// Add the panel to the frame's content pane
frame.add(panel);
frame.setVisible(true);
}
}
මේ code එක run කරලා බලන්න. දැන් ඔයාට window එක ඇතුලේ “Enter your name:” කියලා Label එකකුයි, text type කරන්න පුළුවන් Text Field එකකුයි, “Submit” කියලා Button එකකුයි පේයි. අපි මෙතන JPanel
එකක් අරගෙන ඒකට FlowLayout
එකක් දුන්නා. ඊට පස්සේ JLabel
, JTextField
, JButton
කියන components තුන හදලා ඒක panel.add()
method එකෙන් panel එකට add කළා. අන්තිමට frame.add(panel)
කියලා panel එක frame එකට add කළා.
Events වලට සවන් දෙමු (Listening to Events)
දැන් අපිට components තියෙනවා, ඒත් ඒවා වැඩ කරන්නේ නැහැ. Button එකක් click කරද්දි, text field එකක text එක වෙනස් කරද්දි වගේ දේවල් වලදී අපි events වලට සවන් දෙන්න ඕනේ. මේකට අපි Event Listeners පාවිච්චි කරනවා. උදාහරණයක් විදියට, Button click එකක් handle කරන්න ActionListener
එකක් ඕනේ. මේකෙන් වෙන්නේ Button එක click කරද්දි, අපි දීලා තියෙන code block එක run වෙන එක.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyInteractiveSwingApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Interactive Swing App");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel promptLabel = new JLabel("Enter your name:");
JTextField nameTextField = new JTextField(20);
JButton submitButton = new JButton("Say Hello");
JLabel resultLabel = new JLabel("Hello there!"); // New label to display result
panel.add(promptLabel);
panel.add(nameTextField);
panel.add(submitButton);
panel.add(resultLabel); // Add the new label
// Add an ActionListener to the button
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userName = nameTextField.getText(); // Get text from JTextField
resultLabel.setText("Hello, " + userName + "!"); // Set text to JLabel
}
});
// Or using a Lambda Expression (more modern Java approach)
// submitButton.addActionListener(e -> {
// String userName = nameTextField.getText();
// resultLabel.setText("Hello, " + userName + "!");
// });
frame.add(panel);
frame.setVisible(true);
}
}
දැන් මේ code එක run කරලා බලන්න. Text Field එකේ නමක් type කරලා “Say Hello” Button එක click කරන්න. එතකොට අලුතින් add කරපු “resultLabel” එකේ “Hello, [ඔයාගේ නම]!” කියලා පෙන්නයි. අපි මෙතනදී submitButton.addActionListener()
කියන method එක පාවිච්චි කරලා ActionListener
එකක් Button එකට add කළා. මේ ActionListener
එකට actionPerformed()
කියන method එකක් තියෙනවා. මේ method එක තමයි Button එක click කරද්දි execute වෙන්නේ. ඒ ඇතුලේදී අපි nameTextField.getText()
කියන method එකෙන් Text Field එකේ තියෙන text එක අරගෙන, resultLabel.setText()
කියන method එකෙන් ඒ text එක Label එකට දානවා.
Comments වල තියෙන Lambda Expression එක බලන්න. Java 8 වලින් පස්සේ ActionListener
වගේ Single Abstract Method (SAM) interfaces implement කරන්න මේක ගොඩක් පහසු ක්රමයක්. එකම දේ කරන්න පුළුවන් කෙටි ක්රමයක්. Software Engineer කෙනෙක් විදියට ඔයාට මේ දෙකම තේරුම් ගන්න පුළුවන් වෙන්න ඕනේ.
වැඩිපුර Components ටිකක් (A Few More Components)
අපි අද ඉගෙන ගත්තේ JFrame
, JPanel
, JButton
, JLabel
, JTextField
කියන මූලිකම components ටික. ඒත් Swing වල තව ගොඩක් Components තියෙනවා ඔයාගේ application එකට ලස්සනක් ගේන්න. උදාහරණයක් විදියට:
- JCheckBox: ඔප්ෂන් එකක් තෝරන්න නැත්නම් අයින් කරන්න (tick/untick)
- JRadioButton: ඔප්ෂන් කීපයකින් එකක් විතරක් තෝරන්න
- JComboBox: drop-down list එකකින් value එකක් තෝරන්න
- JTextArea: දිග text input ගන්න
- JTable, JTree, JList: සංකීර්ණ data display කරන්න
මේ හැම එකක්ම පාවිච්චි කරන්න පුළුවන් විදිය සාමාන්යයෙන් අපි අද කතා කරපු විදියටමයි. component එක instantiate කරනවා, properties set කරනවා, panel එකට add කරනවා, අවශ්ය නම් event listeners add කරනවා.
සාරාංශය සහ ඉදිරි ගමන (Summary and Next Steps)
නියමයි නේද! අද අපි සාර්ථකව Java Swing වල මූලිකම දේවල් ඉගෙන ගත්තා. අපි window එකක් හදාගත්තා, ඒකට components එකතු කළා, ඒ වගේම events handle කරන හැටිත් බැලුවා. මේක ඔයාගේ Java GUI programming ගමනේ ආරම්භය විතරයි. Swing කියන්නේ ගොඩක් පුළුල් library එකක්. Layout Managers ගැන තව දුරටත් ඉගෙන ගන්න, Custom painting කරන්න, Menus, Toolbars එකතු කරන්න, Dialogs පාවිච්චි කරන්න වගේ තව ගොඩක් දේවල් තියෙනවා explore කරන්න.
මතක තියාගන්න, coding කියන්නේ කරලාම ඉගෙන ගන්න ඕන දෙයක්. මේ code snippets modify කරලා, අලුත් components දාලා, අලුත් features add කරලා practice කරන්න. එතකොට තමයි ඔයාට මේ concepts හරියටම තේරෙන්නේ.
ඔයාලගේ අත්දැකීම්, ප්රශ්න, අදහස් පහළ comment section එකේ දාන්න අමතක කරන්න එපා. අපි හැමෝටම එකතු වෙලා ඉගෙන ගමු. ආයෙත් මේ වගේ article එකකින් හම්බෙමු!