0% found this document useful (0 votes)
28 views5 pages

Lab 4 and Practical 4

This is for computer science, java

Uploaded by

Fezile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Lab 4 and Practical 4

This is for computer science, java

Uploaded by

Fezile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Fezile MS

Surname: Mazibuko

Student Number: 240114550

4CPS112

Lab 4

1.

import javax.swing.*; // Corrected import statement

Public class RectangleProgramOne extends JFrame {

Static private final int WIDTH = 400;

Static private final int HEIGHT = 300;

Public RectangleProgramOne() {

setTitle(“Welcome”);

setSize(WIDTH, HEIGHT);

setVisible(true); // Corrected method name to setVisible

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Added JFrame


before EXIT_ON_CLOSE

Public static void main(String args[]) {

RectangleProgramOne r1 = new RectangleProgramOne(); // Corrected


object creation

10.

a. Layout manager places GUI components in a container.


b. Clicking a button is an action event.

c. The method actionPerformed is invoked when a button is pressed and an


action listener is registered to handle the event.

d. New operator is needed to instantiate an object.

e. A class has two types of members: variables and methods.

f. To create a window, you extend the JFrame class.

g. Every GUI program is an event-driven program.

h. The method getText gets the string in the JTextField and the method
setText changes the string displayed in a JTextField.

i. If Student is a class and you create a new class GradStudent by extending


Student, then Student is a base class and GradStudent is a derived class.

j. Event and event listener classes are contained in the package


java.awt.event.

k. The unit of measure of length in a window is pixels.

Practical 4

Import java.awt.*;

Import javax.swing.*;

Import java.awt.event.*;

Public class DistanceConversion extends JFrame {

Private JLabel centimeterLabel;

Private JLabel inchLabel;

Private JTextField centimeterTF;

Private JTextField inchTF;

Private CentiHandler centimeterHandler;

Private InchHandler inchHandler;


Private static final int WIDTH = 550;

Private static final int HEIGHT = 50;

// Conversion constants

Private static final double CTOI = 0.393701; // Centimeters to inches

Private static final double ITOC = 2.54; // Inches to centimeters

Public DistanceConversion() {

setTitle(“Distance Conversion”);

Container c = getContentPane();

c.setLayout(new GridLayout(1, 4));

centimeterLabel = new JLabel(“Distance in centimeter: “,


SwingConstants.RIGHT);

inchLabel = new JLabel(“Distance in inches: “, SwingConstants.RIGHT);

centimeterTF = new JTextField(7);

inchTF = new JTextField(7);

c.add(centimeterLabel);

c.add(centimeterTF);

c.add(inchLabel);

c.add(inchTF);

// Create and register action listeners

centimeterHandler = new CentiHandler();

inchHandler = new InchHandler();


centimeterTF.addActionListener(centimeterHandler);

inchTF.addActionListener(inchHandler);

setSize(WIDTH, HEIGHT);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

Private class CentiHandler implements ActionListener {

Public void actionPerformed(ActionEvent e) {

Double centimeter, inch;

Centimeter = Double.parseDouble(centimeterTF.getText());

Inch = centimeter * CTOI;

inchTF.setText(“” + String.format(“%.2f”, inch));

Private class InchHandler implements ActionListener {

Public void actionPerformed(ActionEvent e) {

Double centimeter, inch;

Inch = Double.parseDouble(inchTF.getText());

Centimeter = inch * ITOC;

centimeterTF.setText(“” + String.format(“%.2f”, centimeter));

}
Public static void main(String[] args) {

DistanceConversion distConv = new DistanceConversion();

You might also like