Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Al Imam Mohammad Ibn Saud Islamic University

College of Computer and Information Sciences


Computer Science Department
CS141 and CS151 – Quiz 2
(13/07/1443 - 15/02/2022, 8:25 AM, 30 minutes, 5 marks)

Student Name:

Student ID:
Course Code:
Section No.:

Instructions:
1. Write your answers directly on the question sheets. Use the ends of the question
pages for rough work or if you need extra space for your answer.
2. If information appears to be missing from a question, make a reasonable
assumption, state your assumption, and proceed.

Page 1 of 3

Imam University | CCIS | Doc. No. 006-02-20170316


Question 1: To be answered in (15) Minutes / 2.5 Marks

A user can control the Display light in the application by pressing on the buttons at the
top of the main Jframe (using FlowLayout). When a button is pressed, the label ‘activeLight’ on
the right of the label "Display Light" (both labels are inside a panel that uses Flowlayout)
changes according to the corresponding light. The default text of activeLight is "Green".

Complete the following code skeleton (next page) to reproduce the same interface and
interaction using Java Swing/Awt libraries and event handling components. Upload your java
file or compress and upload it.

Page 2 of 3

Imam University | CCIS | Doc. No. 006-02-20170316


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

public class GraphicTest {


public static void main(String[] args) {
// fill in code here
}
}
// Complete the missing parts in Tlight class for GUI
// Class declaration part (0.5 pt)

class ChangeLight { // you must correct the class declaration


// Components declaration and instantiation
JPanel lights = new JPanel();
JPanel display = new JPanel();
JLabel disLabel = new JLabel("Display Light:");
JLabel activeLight = new JLabel("Green");

JButton green = new JButton("Green");


JButton yellow = new JButton("Yellow");
JButton red = new JButton("Red");

public ChangeLight() {
//fill in the missing code under here,
//all components and panels have been
//declared previously.

// set other properties of JFrame


setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setTitle("GUI Example");
}

@Override
public void actionPerformed(ActionEvent ae) {
// complete handling the events and setting the text of the activeLight label to
// the corresponding light

}
}

Page 3 of 3

Imam University | CCIS | Doc. No. 006-02-20170316

You might also like