TCC102 Unit3 Tutorial3
TCC102 Unit3 Tutorial3
Tutorial 3
Content pane
Methods Provided by the
class Container
class JButton
Provided to create buttons in Java.
To create a button:
JButton btnOk = new JButton(“OK”);
Methods Provided by the class
JButton
Methods Provided by the class
JButton
Sample
Code
FrameWithButton.java
import javax.swing.*;
import java.awt.*;
public class FrameWithButton extends JFrame {
private JButton btnOK;
public FrameWithButton(){
super("Test Frame");
btnOK = new JButton("OK");
Container pane =getContentPane();
pane.add(btnOK);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
FrameWithButton frame = new FrameWithButton();
}
}
class JLabel
Labels: Objects of a particular class type.
class JLabel: Used to create labels.
Label attributes:
Title
Width
Height
To create a label:
Instantiate object of type JLabel.
Modify attributes to control display of labels.
Methods Provided by the class
JLabel
Methods Provided by the class
JLabel
class JTextField
Text fields: Objects belonging to class
JTextField.
To create a text field:
Declare reference variable of type JTextField.
Instantiate object.
Methods Provided by the class JTextField
Layout Managers
FlowLayout
Default layout manager.
Places components from left to right until no
more items can be placed.
Can align each line left, center, or right.
Default alignment: LEFT.
GridLayout
Similar to FlowLayout.
All rows (columns) have same number of
components.
All components have the same size.
Layout Managers
BorderLayout
Items placed into one of five specific regions:
NORTH/SOUTH
EAST/WEST
CENTER
NORTH and SOUTH components extend
horizontally (completely span one edge to the
other).
EAST and WEST components extend vertically
between components in NORTH and SOUTH
regions.
CENTER component expands to occupy any
unused regions.
Sample
Code FlowLayoutDemo.java
import javax.swing.*;
import java.awt.*;
public class FlowLayoutDemo extends JFrame {
private JButton btnCalculate;
private JLabel lengthL;
public FlowLayoutDemo(){
super("Test Frame");
btnCalculate = new JButton("Calculate");
lengthL = new JLabel("Enter the length:");
Container pane =getContentPane();
pane.setLayout(new FlowLayout());
pane.add(lengthL);
pane.add(btnCalculate);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
FlowLayoutDemo frame = new FlowLayoutDemo();
}
}
Sample
Code BorderLayoutDemo.java
import javax.swing.*;
import java.awt.*;
public class BorderLayoutDemo extends JFrame {
private JButton btnCalculate, btnExit;
private JLabel lengthL;
public BorderLayoutDemo(){
super("Test Frame");
btnCalculate = new JButton("Calculate");
btnExit = new JButton("Exit");
lengthL = new JLabel("Enter the length:");
Container pane =getContentPane();
pane.setLayout(new BorderLayout());
pane.add(lengthL, BorderLayout.EAST);
pane.add(btnCalculate, BorderLayout.SOUTH);
pane.add(btnExit, BorderLayout.WEST);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
BorderLayoutDemo frame = new BorderLayoutDemo();
Sample GridLayoutDemo.java
Code
import javax.swing.*;
import java.awt.*;
public class GridLayoutDemo extends JFrame {
private JButton btnCalculate, btnExit;
private JLabel lengthL;
private JTextField lengthTF;
public GridLayoutDemo(){
super("Test Frame");
btnCalculate = new JButton("Calculate");
btnExit = new JButton("Exit");
lengthL = new JLabel("Enter the length:");
lengthTF = new JTextField(10);
Container pane =getContentPane();
pane.setLayout(new GridLayout(2,2));
pane.add(lengthL);
pane.add(lengthTF);
pane.add(btnCalculate);
pane.add(btnExit);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Handling an Event
}
Sample
Code EventDemo.java
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double length, width, area;
length = Double.parseDouble(lengthTF.getText());
width = Double.parseDouble(widthTF.getText());
area = length * width;
areaTF.setText(String.format("%.2f",area));
}
}
private class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public static void main(String[] args) {
EventDemo frame = new EventDemo();
}
}
Pseudocode for
EventDem.java
//Use the import statement to access the content pane
//Use the import statement to create GUI components
//Use the import statement to create event handling objects
//Define class EventDemo that extends JFrame
//Declare variables for each label, button, textfield and event handler of the content pane
//Write a default constructor which contain:
//Create each label fields
//Create each text fields
//Create a calculate button
//Register calculate button with event listener
//Create an exit button
//Register exit button with event listener
//Get the container object
//Set the pane layout
//Add the components in the pane
//Set the title for the window
//Set the size for the window
//Display the window
//Set the close operation for the window
//End constructor
//Define the main method which contain:
//declare a EventDemo variable and create an instance of EventDemo object
//End method main
//End class
TMA 3 (20%)
Individual Assignment
Submit soft copy (virus free) and hard copy of the TMA3 in word
processed (1.5 spacing).
You may send your soft copy to me at [email protected]
Submission date 21/10/07
Late submission
Over 7 days
Formatting output
Malik textbook , page 142
LayoutManager
Malik textbook, page 888
Handling an Event
Malik textbook, page 322
Complete study chapter 6, Malik textbook
Applets
A Java program that is embedded within a
Web page and executed by a Web browser.
To develop an applet:
Override any/all of the methods above.
Applet Methods
init method:
Initializes variables.
paint method:
Performs output.
Skeleton of a Java Applet
import java.awt.Graphics;
import javax.swing.JApplet;
}
Applet Displaying Welcome Message
//Welcome Applet
import java.awt.Graphics;
import javax.swing.JApplet;