Vous êtes sur la page 1sur 5

UiTM Melaka Kampus Jasin

CSC444 Java Programming (Lab 5-GUI)

Lab 5
Exercise 1
a. Read and analyze the program below
/**
Introduction to OOP with Java 4th Ed, McGraw-Hill by Wu/Otani
extended by Zulaile Mabni
A Sample frame to illustrate the placing of GUI objects and event
handling with Swing JFrame
**/
import
import
import
import

javax.swing.*;
java.awt.*;
java.awt.event.*;
java.io.*;

class MiniCalculator extends JFrame implements ActionListener{


//-----------------------------------// Data Members
//-----------------------------------// Default frame width
private static final int FRAME_WIDTH = 350;
// Default frame height
private static final int FRAME_HEIGHT = 200;
// x coordinate of the frame default original point
private static final int FRAME_X_ORIGIN = 150;
// y coordinate of the frame default original point
private static final int FRAME_Y_ORIGIN = 250;
/**
* The Swing button for Add and Subtract
*/
private JButton btnAdd;
private JButton btnSubtract;
private JButton btnClear;
private JButton btnExit;
/**
* The JTextField for the user to enter the numbers
*/
private JTextField txtNum1;
private JTextField txtNum2;
private JTextField txtResult;
/**
* The JLabel for prompting the user
*/
private JLabel lblNum1;

UiTM Melaka Kampus Jasin

CSC444 Java Programming (Lab 5-GUI)

private JLabel lblNum2;


private JLabel lblResult;
//-----------------------------------// Main Method
//-----------------------------------public static void main(String[] args) throws IOException {
MiniCalculator calculator = new MiniCalculator();
calculator.setVisible(true);
}
//-----------------------------------// Constructors
//-----------------------------------public MiniCalculator()
{
Container contentPane;
//set the frame properties
setSize
(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(true);
setTitle
("Mini Calculator");
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground( Color.GREEN);
// add the labels
lblNum1 = new JLabel();
lblNum1.setText("Please enter the 1st number:");
lblNum1.setSize(150, 25);
lblNum2 = new JLabel();
lblNum2.setText("Please enter the 2nd number:");
lblNum2.setSize(150, 25);
lblResult = new JLabel();
lblResult.setText("The Result:");
lblResult.setSize(150, 25);
// add the text fields
txtNum1 = new JTextField();
txtNum1.setColumns(15);
txtNum1.setFont(new Font("Courier", Font.PLAIN, 14));
txtNum2 = new JTextField();
txtNum2.setColumns(15);
txtNum2.setFont(new Font("Courier", Font.PLAIN, 14));
txtResult = new JTextField();
txtResult.setColumns(27);
txtResult.setFont(new Font("Courier", Font.PLAIN, 14));
//create and place buttons on the frame
btnAdd = new JButton("ADD");

UiTM Melaka Kampus Jasin

CSC444 Java Programming (Lab 5-GUI)

btnSubtract = new JButton("SUBTRACT");


btnClear = new JButton("CLEAR");
btnExit = new JButton("EXIT");
//add the objects to the content pane
contentPane.add(lblNum1);
contentPane.add(txtNum1);
contentPane.add(lblNum2);
contentPane.add(txtNum2);
contentPane.add(lblResult);
contentPane.add(txtResult);
//add the button objects to the content pane
contentPane.add(btnAdd);
contentPane.add(btnSubtract);
contentPane.add(btnClear);
contentPane.add(btnExit);
//register this frame as an action listener of the
buttons
txtNum1.addActionListener(this);
txtNum2.addActionListener(this);
btnAdd.addActionListener(this);
btnSubtract.addActionListener(this);
btnClear.addActionListener(this);
btnExit.addActionListener(this);
setDefaultCloseOperation( EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
if ( event.getSource() == btnAdd)
{
int num1 = Integer.parseInt(txtNum1.getText());
int num2 = Integer.parseInt(txtNum2.getText());
int add = num1 + num2;
txtResult.setText(" " + add);
}
else if ( event.getSource() == btnSubtract)
{
int num1 = Integer.parseInt(txtNum1.getText());
int num2 = Integer.parseInt(txtNum2.getText());
int sub = num1 - num2;
txtResult.setText(" " + sub);
}
else if ( event.getSource() == btnClear)
{
txtNum1.setText(" " + 0);
txtNum2.setText(" " + 0);
txtResult.setText(" " + 0);
}
else if ( event.getSource() == btnExit)

UiTM Melaka Kampus Jasin

CSC444 Java Programming (Lab 5-GUI)

{
}

System.exit(0);
}
// end of ActionPerformed method

}
Program 1.1
b. What is the output? (Draw the GUI in the box below)

UiTM Melaka Kampus Jasin

CSC444 Java Programming (Lab 5-GUI)

Exercise 2
Raya Biscuit Sdn. Bhd. Has requested from you to write a GUI program to calculate the
total price to be paid by the customer for the biscuits that they purchased. Write a JAVA
program to develop the GUI interface as shown below. Use GridLayout(6,2).

In order for us to calculate the total price to be paid by the customers for the biscuits that
they purchased, we need to do some calculation using the event handling mechanism.
Each customer can make order based on the following information:
Types
Almond London
Mazola Cookies

Price / box (1 box = 50 pieces)


RM20.00
RM15.00

Write the event handler for this system to calculate the total price based on the types
and quantity of biscuits ordered by the customers. Additional RM5.00 delivery charges
will be charged for customers who need the delivery services.

Vous aimerez peut-être aussi