Output:
Code:
/**
* File: arrayJButton.java
* Tiltle: Construct a JButton using Array with Event Listener
* Author: http://java-code-complete.blogspot.com
*/
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;
public class arrayJButton extends JFrame {
//Initializing JButton and JPanel
private JButton button[];
private JPanel panel;
//Setting up GUI
public arrayJButton() {
//Setting up the Title of the Window
super("Construct a JButton using Array");
//Set Size of the Window (WIDTH, LENGTH)
setSize(275,200);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing JButton with an array of 5
button = new JButton[5];
//Constructing JPanel with a GirdLayout of 5 rows and 1 column (Vertical Position)
panel = new JPanel();
panel.setLayout(new GridLayout(button.length,1));
//Constructing all 5 JButtons using "for loop"
for(int count=0; count<button.length; count++) {
button[count] = new JButton("Button "+(count+1));
panel.add(button[count]);
}
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Implemeting Even-Listener on JButton's reference "button[0]" using ActionListener
button[0].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 1 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[1]" using ActionListener
button[1].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 2 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[2]" using ActionListener
button[2].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 3 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[3]" using ActionListener
button[3].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 4 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[4]" using ActionListener
button[4].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 5 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Adding the JPanel to the container
pane.add(panel);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}
//Main Method
public static void main (String[] args) {
arrayJButton arjb = new arrayJButton();
}
}
Important Part of the Program:
//Constructing JButton with an array of 5
button = new JButton[5];
//Constructing JPanel with a GirdLayout of 5 rows and 1 column (Vertical Position)
panel = new JPanel();
panel.setLayout(new GridLayout(button.length,1));
//Constructing all 5 JButtons using "for loop"
for(int count=0; count<button.length; count++) {
button[count] = new JButton("Button "+(count+1));
panel.add(button[count]);
}
//Implemeting Even-Listener on JButton's reference "button[0]" using ActionListener
button[0].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 1 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[1]" using ActionListener
button[1].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 2 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[2]" using ActionListener
button[2].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 3 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[3]" using ActionListener
button[3].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 4 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
//Implemeting Even-Listener on JButton's reference "button[4]" using ActionListener
button[4].addActionListener(
new ActionListener() {
//Handle JButton event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Message to be displayed after the button is clicked.
String message = "Button 5 Event Listener Successfully Implemented";
//Display the message using JOptionPane if the Event is successfully implemented.
JOptionPane.showMessageDialog(null, message,"Event",JOptionPane.INFORMATION_MESSAGE);
}
}
);
No comments:
Post a Comment