This is just a beginner's code in Java for building a GUI (Graphical User Interface). It features just a button, which when clicked, changes text from "Not Clicked" to "I'm Clicked". There are various technicalities and theoretical aspects of Java that are involved in this code, but, we'll not look at them just now, but what we'll do is keep brief comments with important parts of the code that need to be explained, and their definitions? We'll learn then in Insights into Programming, as we go along. And for those of you, who have a little knowledge of Java, for you guys, I hope that just comments will do.
The Code follows:
package gui;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*Here we have imported three packages from the Java Library. First of the three, is java.awt. AWT stands for Abstract Windows Toolkit. It provides classes and tools to call on API to create a window, that is when function call occurs and manages the output on the window produced.
The next is javax.swing, which is basically derived from awt, but it is specific to java and provides an interface of GUI that remains the same with every Operating Systems API (Application Program Interface), and has the same display in every O/S, unlike awt.
And the third import here is the awt.event.*. We have imported this specifically to lend on the ActionListener tool. For now, all you need to know, that a function, called actionPerformed( ) in this pacakge, detects when a button is clicked or another similar action is performed and define the corresponding change that must be invoked after the button has been clicked*/
public class GUI
{
JFrame frame;
/*JFrame is a class defined in javax.swing that lets us create a new window, for our GUI app at run time*/
JButton button;
/*JButton is class defined in the Swing package and well, as the name suggests, creates a button in our app. It basically returns false when not clicked and true when it is. It's different from traditional console based elements in the sense that it returns those values at the invocation of a mouse*/
JLabel Label;
/*JLabel is another class defined in the Swing package. It's simply, a label. You can place some text on it, and it'll display it in the app*/
class buttonListener implements ActionListener
{
/*buttonListener is an inner class, or a nested class in the main class GUI, that implements the methods of the class ActionListener defined in java.awt.event, as suggested by the keyword implements. ActionListener is an abstract class, and it contains a function called actionPerformed( ) that accepts a parameter of the type ActionEvent, which basically tells whether some invocation or, click has been performed or a button etc. i.e it senses an action on the elements of the GUI application. By using this method, we can detect any action on the individual application elements and define the changes that must be made following the click of the button*/
public void actionPerformed(ActionEvent event)
{
Label.setText("I'm Clicked");
/*setText(char *) is a member of JLable and it sets the text on the Label to the String passed. Here, what the code will do is, it will change the text on the lable to "I'm clicked", when the button is clicked"*/
}
}
buttonListener b = new buttonListener( );
/*An object of buttonListener is declared so that it can be linked to a button, whose clicks we are to detect*/
public static void main(String[] args)
{
GUI gui = new GUI( );
gui.go( );
}
public void go( )
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*setDefaultCloseOperation( ) is a meber of JFrame, that defines the operation to be performed when the close (x) button on the frame is clicked. Here we have passed EXIT_ON_CLOSE, that implicates that when closed, the run of the code is also terminated*/
button = new JButton("Button");
/*Here we have initialized button, with it's name that is - Button*/
Label = new JLabel("Not Clicked");
/*We have initialized Label with the String "Not Clicked", to indicate it's status before the button is clicked*/
button.addActionListener(b);
/*In this step, we have linked the button with buttonListener, to, you know, detect when it's clicked*/
frame.getContentPane( ).add(BorderLayout.WEST,button);
/*getContentPane( ).add( ), is used to add elements defined in the application's window. It accepts two arguments, that is the elemets orientation, given through BorderLayout.direction, which can be EAST, WEST, SOUTH, NORTH, CENTER and the element to be added*/
frame.getContentPane( ).add(BorderLayout.EAST,Label);
frame.setSize(200,300);
frame.setVisible(true);
/*setSize(pixels,pixels) is defined in JFrame and accepts two parameters which represent width and height of our applcation window in pixels. And setVisible(Boolean) accepts a Boolean argument - true or false. If it's set to false, you'll not see the run of your application. Your application window will not be visible, if you don't set it to true*/
}
}
Here's the output the code will produce:
Well that was a beginner's code to build a GUI application in Java. We'll dig more into GUI development later. To understand how we got here, please follow our series "Baby Steps into Programming". Also to keep in touch with our latest articles and programs, like our Facebook page
Code for School .
The Code follows:
package gui;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*Here we have imported three packages from the Java Library. First of the three, is java.awt. AWT stands for Abstract Windows Toolkit. It provides classes and tools to call on API to create a window, that is when function call occurs and manages the output on the window produced.
The next is javax.swing, which is basically derived from awt, but it is specific to java and provides an interface of GUI that remains the same with every Operating Systems API (Application Program Interface), and has the same display in every O/S, unlike awt.
And the third import here is the awt.event.*. We have imported this specifically to lend on the ActionListener tool. For now, all you need to know, that a function, called actionPerformed( ) in this pacakge, detects when a button is clicked or another similar action is performed and define the corresponding change that must be invoked after the button has been clicked*/
public class GUI
{
JFrame frame;
/*JFrame is a class defined in javax.swing that lets us create a new window, for our GUI app at run time*/
JButton button;
/*JButton is class defined in the Swing package and well, as the name suggests, creates a button in our app. It basically returns false when not clicked and true when it is. It's different from traditional console based elements in the sense that it returns those values at the invocation of a mouse*/
JLabel Label;
/*JLabel is another class defined in the Swing package. It's simply, a label. You can place some text on it, and it'll display it in the app*/
class buttonListener implements ActionListener
{
/*buttonListener is an inner class, or a nested class in the main class GUI, that implements the methods of the class ActionListener defined in java.awt.event, as suggested by the keyword implements. ActionListener is an abstract class, and it contains a function called actionPerformed( ) that accepts a parameter of the type ActionEvent, which basically tells whether some invocation or, click has been performed or a button etc. i.e it senses an action on the elements of the GUI application. By using this method, we can detect any action on the individual application elements and define the changes that must be made following the click of the button*/
public void actionPerformed(ActionEvent event)
{
Label.setText("I'm Clicked");
/*setText(char *) is a member of JLable and it sets the text on the Label to the String passed. Here, what the code will do is, it will change the text on the lable to "I'm clicked", when the button is clicked"*/
}
}
buttonListener b = new buttonListener( );
/*An object of buttonListener is declared so that it can be linked to a button, whose clicks we are to detect*/
public static void main(String[] args)
{
GUI gui = new GUI( );
gui.go( );
}
public void go( )
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*setDefaultCloseOperation( ) is a meber of JFrame, that defines the operation to be performed when the close (x) button on the frame is clicked. Here we have passed EXIT_ON_CLOSE, that implicates that when closed, the run of the code is also terminated*/
button = new JButton("Button");
/*Here we have initialized button, with it's name that is - Button*/
Label = new JLabel("Not Clicked");
/*We have initialized Label with the String "Not Clicked", to indicate it's status before the button is clicked*/
button.addActionListener(b);
/*In this step, we have linked the button with buttonListener, to, you know, detect when it's clicked*/
frame.getContentPane( ).add(BorderLayout.WEST,button);
/*getContentPane( ).add( ), is used to add elements defined in the application's window. It accepts two arguments, that is the elemets orientation, given through BorderLayout.direction, which can be EAST, WEST, SOUTH, NORTH, CENTER and the element to be added*/
frame.getContentPane( ).add(BorderLayout.EAST,Label);
frame.setSize(200,300);
frame.setVisible(true);
/*setSize(pixels,pixels) is defined in JFrame and accepts two parameters which represent width and height of our applcation window in pixels. And setVisible(Boolean) accepts a Boolean argument - true or false. If it's set to false, you'll not see the run of your application. Your application window will not be visible, if you don't set it to true*/
}
}
Here's the output the code will produce:
![]() |
| fig. Output |
Well that was a beginner's code to build a GUI application in Java. We'll dig more into GUI development later. To understand how we got here, please follow our series "Baby Steps into Programming". Also to keep in touch with our latest articles and programs, like our Facebook page
Code for School .

No comments:
Post a Comment