Java GUI (Graphical User Interface) programming
Up to this point we have focused only on Java code that runs in a command prompt. In this lesson, we go beyond text and enter the realm of graphical programming.
This lesson focues on:
- Importing the necessary package to create graphical programs
- Creating a frame
- GUI components
Importing the necessary package to create graphical programs
To create graphical programs, you have to import the java.awt package. This package contains classes for several graphical components such as frames, windows, textboxes, buttons, and more.
Example:
import java.awt.*;
class AClass{
}
Creating a frame
In Java, a frame is a standard window used for graphical applications. It has minimize, maximize, and close buttons in its top right corner and can be moved and resized (by default). Frames are created using the Frame class.
Frame class constructors:
-
Frame()
Creates a new instance of Frame that is initially invisible.
-
Frame(String title)
Creates a new instance of Frame that is initially invisible with the specified title.
Frame class methods:
-
boolean isResizable()
Returns a boolean value indicating whether a frame is resizable or not.
-
void setResizable(boolean resizable)
Sets whether or not a frame is resizable.
-
String getTitle()
Returns the title of a frame.
-
void setTitle(String title)
Sets the title of a frame.
-
void isVisible(boolean isVisible)
Sets whether or not a frame is visible.
-
void setSize(int width, int height)
Sets the size of a frame.
You can create a frame by extending the Frame class:
Example:
import java.awt.*;
class AFrame extends Frame{
public static void main(String[] args){
AFrame frame = new AFrame();
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Output:
In the above example, a class named AFrame that extends the Frame class is created. Within the main() method of this class, a new instance of AFrame is created. It's size is set as 200 by 200, and it is set to be initially visible.
You can also create a frame by creating an instance of the Frame class:
import java.awt.*;
class AFrame{
public static void main(String[] args){
Frame aFrame = new Frame();
aFrame.setSize(200, 200);
aFrame.setVisible(true);
}
}
Output:
In the above example, a class named AFrame is created. Within the main() method of this class, a new instance of Frame is created. It's size is set as 200 by 200, and it is set to be initially visible.
NOTE: Initially, a frame will not be visible. You have to set it to be visible using the setVisible() method.
GUI components
There are various graphical components you can add to frames. Some of these components include labels, buttons, textboxes, and textareas.
-
Labels
Labels are created using the Label class.
Label class constructors:
-
Label()
Creates a new instance of Label.
-
Label(String text)
Creates a new instance of Label with the specified text.
Label class methods:
-
String getText()
Returns a labels text.
-
void setText(String text)
Sets a labels text to the specified text.
You can create a label by creating an instance of the Label class:
Lebel labelOne = new Label(); Label labelTwo = new Label("This is a label");You can add a label (or any other graphical component) to a frame by using the add() method:
aFrame.add(labelOne);
-
Label()
-
Buttons
Buttons are created using the Button class.
Button class constructors:
-
Button()
Creates a new instance of Button.
-
Button(String label)
Creates a new instance of Button with the specified string as a label.
Button class methods:
-
String getLabel()
Returns a buttons label.
-
void setLabel(String label)
Sets a buttons label to the string specified by label.
You can create a button by creating an instance of the Button class:
Button buttonOne = new Button(); Button buttonOne = new Button("This is a button")You can add a button to a frame by using the add() method:
aFrame.add(buttonOne);
-
Button()
-
Textboxes
Textboxes are created using the TextField class.
TextField class constructors:
-
TextField()
Creates a new instance of TextField.
-
TextField(String text)
Creates a new instance of TextField with the specified text.
TextField class methods:
-
void setColumns(int columns)
Sets the number of columns in a textbox.
-
void setText(String text)
Sets a textboxes text to the specified text.
You can create a textbox by creating an instance of the TextField class:
TextField textOne = new TextField(); TextField textTwo = new TextField("This is a textbox");You can add a textbox to a frame by using the add() method:
aFrame.add(textOne);
-
TextField()
-
Textareas
TextAreas are created using the TextArea class.
TextArea class constructors:
-
TextArea()
Creates a new instance of TextArea.
-
TextArea(int rows, int columns)
Creats a new instance of TextArea with the specified number of rows and columns.
TextArea class methods:
-
void append(String appendedText)
Appends the specified string to the end of the textareas text.
-
int getColumns()
Returns the number of columns in a textarea.
You can create a textarea by creating an instance of the TextArea class:
TextArea textOne = new TextArea(); TextArea textTwo = new TextArea();
You can add a textarea to a frame by using the add() method:
aFrame.add(taAreaOne);
-
TextArea()
A Frame with several graphical components:
import java.awt.*;
class FrameWithComponents{
public static void main(String[] args){
//create a new frame whose
//title is "Frame with components"
Frame AFrame = new Frame("Frame with components");
//create a label, a button, a textbox, and a textarea
Label lblOne = new Label("This is a label");
Button btn1 = new Button("This is a button");
TextField tf1 = new TextField();
TextArea ta1 = new TextArea();
//set the text of the
//textfield to "This is a textbox
tf1.setText("This is a textbox");
//append to the textarea the text
//"Number of columns in this textarea: "
//+ the number of columns in the textarea
ta1.append("Number of columns in this textarea: "
+ ta1.getColumns());
//add the components to the frame
AFrame.add(lblOne);
AFrame.add(btn1);
AFrame.add(tf1);
AFrame.add(ta1);
//set the size of
//the frame to 450 by 300
AFrame.setSize(450, 300);
//set the layout of
//the frame to FlowLayout
AFrame.setLayout(new FlowLayout());
//set the frame to be initially visible
AFrame.setVisible(true);
}
}
Output:
In the above example, a class named FrameWithComponents is created. Within the main() method of this class, a frame is created, as well as a label, a button, a textbox, and a textarea. The text that will appear on the label and the button is set in their initialization. The text that will appear in the textbox is set through the setText() method. The text that will appear in the textarea is set through the append() method. Another method used with the textarea is getColumns, which is used to return the number of columns in the textarea.
The size of the frame is set to 450 by 300 with the setSize method. The layout of the frame is set to FlowLayout which means that the components in the frame will appear from left to right in the order in which they are added (GUI layout is covered in detail in the next lesson. The frame is set to be initially visible with the setVisible() method.




