Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Exercise 1

package tutorial;

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

public class exe1 extends JFrame implements ActionListener


{
JButton bred;

exe1()
{
super ("SimpleSwing");
Container container=getContentPane();
container.setLayout(new FlowLayout());

bred=new JButton("RED");
container.add(bred);

bred.addActionListener(this);
setSize(350,200);
setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
String arg = e.getActionCommand();
if(e.getSource()instanceof JButton)
{
if("RED".equals(arg))
getContentPane().setBackground(Color.red);
else
setBackground(Color.black);
}
repaint();
}
public static void main(String[] args) {
exe1 test = new exe1();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output

Before : After :
Exercise 2

package tutorial;

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

public class exe2 extends JFrame {

private JLabel lb1, lb2, lb3, lb4, lb5, lb6;


private JTextField tf1, tf2, tf3;
private JButton b1;
private Container ct;
private FlowLayout layout;

public exe2() {
super("Markah Pelajar");
layout = new FlowLayout();

Container ct = getContentPane();
ct.setLayout(new FlowLayout());

lb1 = new JLabel("MARKAH F1001");


layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(ct);
ct.add(lb1);

lb2 = new JLabel("Markah PB:");


layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(ct);
ct.add(lb2);

tf1 = new JTextField(10);


ct.add(tf1);

lb3 = new JLabel("Markah PA:");


layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(ct);
ct.add(lb3);

tf2 = new JTextField(10);


ct.add(tf2);

lb4 = new JLabel("Purata Markah:");


layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(ct);
ct.add(lb4);
tf3 = new JTextField(10);
ct.add(tf3);

lb5 = new JLabel("Gred:");


layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(ct);
ct.add(lb5);

lb6 = new JLabel(" ");


layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(ct);
ct.add(lb6);

b1 = new JButton("Keputusan");
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(ct);
ct.add(b1);

ButtonHandler handler = new ButtonHandler();


b1.addActionListener(handler);

setSize(400, 300);
setVisible(true);
}

public static void main(String args[]) {


exe2 application = new exe2();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {


Object b2 = event.getSource();

if (b2 == b1) {
int pa, pb, purata;
pa = Integer.parseInt(tf1.getText());
pb = Integer.parseInt(tf2.getText());
purata = (pa + pb) / 2;
tf3.setText(" " + purata);
lb5.setText("Gred:");

if (purata >= 85) {


lb6.setText(" " + purata);
lb6.setText("Excellent");
} else if (purata >= 50) {
lb6.setText(" " + purata);
lb6.setText("OK!");
} else if (purata >= 0) {
lb6.setText(" " + purata);
lb6.setText("STUDY HARD!");
}
}
}
}
}

Output
Exercise 3(a)

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

public class exe3a extends JFrame {


private JButton semak;
private JLabel tl,ts,papar,umur;
private JTextField txt1,txt2;

private int x, y, z;
JOptionPane popup;

exe3a() {
super ("Kira umur melalui tarikh lahir");
Container container=getContentPane();
container.setLayout(new FlowLayout());

tl=new JLabel("Tahun lahir : "); //label


ts=new JLabel("Tahun sekarang : ");
umur=new JLabel("Umur anda : ");
papar=new JLabel("");

txt1=new JTextField(5);
txt2=new JTextField(5);

semak=new JButton("Semak"); //button semak

container.add(tl);
container.add(txt1);
container.add(ts);
container.add(txt2);
container.add(semak);
container.add(umur);
container.add(papar);

exe3aHandler handler=new exe3aHandler();


semak.addActionListener(handler);

setSize(350,150);
setVisible(true);

public static void main(String[] args) {


exe3a apps = new exe3a();
apps.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class exe3aHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

Object button2 = event.getSource();

int x,y,z;

if (button2 == semak) {

x = Integer.parseInt(txt1.getText());
y = Integer.parseInt(txt2.getText());

z = y - x;

papar.setText(""+z);
}
}
}
}

Output
Exercise 3(b)

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

public class exe3b extends JFrame {

private JTextField text;


private JButton button;
private JLabel result,status,arahan;
private GridLayout layout;

public exe3b() {
super("Jantina anda");
layout = new GridLayout(2,2);

Container container = getContentPane();


container.setLayout(layout);

text = new JTextField("IC number here",15);


container.add(text);

button = new JButton("Send");


container.add(button);

status = new JLabel("Status :");


container.add(status);

result = new JLabel("");


container.add(result);

exe3Bhandler handler = new exe3Bhandler();


button.addActionListener(handler);

setSize(300, 100);
setVisible(true);

public static void main(String[] args) {


exe3b application = new exe3b();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class exe3Bhandler implements ActionListener {


public void actionPerformed(ActionEvent event) {
Object button2 = event.getSource();

long ic;
long total;
if (button2 == button) {

ic = Long.parseLong(text.getText());

total = ic % 2;

if (total == 0) {
result.setText("Perempuan");
} else if (total == 1) {
result.setText("Lelaki");
}

}
}
}
}

Output

You might also like