Question 1
(a)
Write a swing program. The program consists of
Here is part of the codes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChooseColor extends JFrame implements ActionListener {
    // your codes
    
    
    
    public static void main(String[] args) {
        JFrame f = new ChooseColor();
	f.setSize(250,150);
	f.setVisible(true);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
(b)
Rewrite the above program by using anonymous class
Question 2
Write a swing program with 3 buttons. Each buttons has a number on it. When you click on the buttons, the number on it will be increased by 1.
At the beginning

After you click the first button twice, the second button 3 times, and the last button once

Fill in the codes
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Click extends JFrame implements ActionListener {
    // your codes
    
    public Click() {
       // your codes
	
    }
    
    public void actionPerformed(ActionEvent e) {
       // your codes
    }
    
   public static void main(String[] args) {
        JFrame f = new Click();
	f.pack();
	f.setVisible(true);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
	
}
Question 3 (a)
Sketch(draw) the output of the following program
import java.awt.*;
import javax.swing.*;

public class LayoutTest1 {
    public static void main(String[] args) {
        JFrame f = new JFrame("LayoutTest1");
        f.getContentPane().setLayout(new GridLayout(2,4));
       for(char ch = 'a'; ch <= 'g'; ch++) {
            f.getContentPane().add(new JButton("" + ch));
        }
        f.getContentPane().remove(3);
        f.getContentPane().add(new JButton(""+1), 5);
        f.pack();
        f.setVisible(true);
    }
}
(b)
import java.awt.*;
import javax.swing.*;

public class LayoutTest2 {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().setLayout(new BorderLayout());
        JPanel panel = new JPanel(new GridLayout(2,0));
        for(char ch = 'a'; ch <= 'f'; ch++) {
            panel.add(new JButton("" + ch));
       }
        f.getContentPane().add(new JButton("Hi"), BorderLayout.EAST);
        f.getContentPane().add(new JButton("Hey"));
        f.getContentPane().add(panel, BorderLayout.NORTH);
        f.setSize(300,300);
        f.setVisible(true);
    }
}
Question 4
The program WhoAmI.java randomly gives a Simpson character name. The user then asked to click the correct picture.
All the images belows are inside a folder name simpson

Apu.gif

Bart.gif

Burns.gif

Homer.gif

Krusty.gif

Lisa.gif

Maggie.gif

Marge.gif

Moe.gif

Ned.gif
At the beginning:
Answer the question on the top label.
Each time, a character name is randomly chosen among the 10 names given above.


Click on the button with Lisa.

Number of trials is increased by 1. Number of correct answers is increased by 1.
Another question appears.

Click on a wrong button.

Number of trials is increased by 1 but number of correct answers remains unchanged.
Another question appears.
The game goes on forever.


In below is part of the codes. Fill in the missing codes.
import javax.swing.*;
import java.awt.event.*;

public class WhoAmI extends JFrame implements ActionListener {
    private JLabel topLabel = new JLabel();
    private JLabel bottomLabel = new JLabel();
    private JPanel middlePanel;
    private JButton[] buttons;
    private Container contentPane = getContentPane();
    private String[] names = {"Apu", "Bart", "Burns", "Homer", "Krusty",
                                "Lisa", "Maggie", "Marge", "Moe", "Ned"};
    // other variables 

    public WhoAmI() {
         // your codes

         
    }
        
    public void actionPerformed(ActionEvent e) {
          // your codes 
          
    }
    
    // other codes
    
    
    public static void main(String[] args) {
        JFrame f = new WhoAmI();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(280,180);
        f.setVisible(true);
    }
}

Question 5
Consider the following program
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ActionTest extends JFrame {
    JButton buttonA = new JButton("A");
    JButton buttonB = new JButton("B");
    JButton buttonC = new JButton("C");
    JLabel display = new JLabel("0", JLabel.CENTER);
    Container contentPane = getContentPane();
    
    public ActionTest() {
        contentPane.add(buttonA, BorderLayout.CENTER);
        contentPane.add(buttonB, BorderLayout.EAST);
        contentPane.add(buttonC, BorderLayout.WEST);
        contentPane.add(display, BorderLayout.SOUTH);
        
        buttonA.addActionListener(new Action1(display, 3));
        buttonB.addActionListener(new Action1(display, 2));
        buttonC.addActionListener(new Action2(display));        
    }
    
    public static void main(String[] args) {
        ActionTest f = new ActionTest();
        f.setSize(200,100);
        f.setVisible(true);
    }
}

class Action1 implements ActionListener {
    JLabel label;
    int x;
    
    public Action1(JLabel label, int x) {
        this.label = label;
        this.x = x;
    }
    
    public void actionPerformed(ActionEvent e) {
        int num = Integer.parseInt(label.getText()) + x;
        label.setText("" + num);
    }
}

class Action2 implements ActionListener {
    JLabel label;
    
    public Action2(JLabel label) {
        this.label = label;
    }
    
    public void actionPerformed(ActionEvent e) {
        label.setText("" + 3);
    }
}
a)
Draw (sketch) the output of java ActionTest:
b)
Briefly describe what happens what you (i) click on buttonA (ii) click on buttonB (iii) click on buttonC?
c)
When will be the number on display after you click on buttonC, then click on buttonB, and then click on buttonA?
d)
Rewrite the above program using anonymous class only.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ActionTest extends JFrame {
    JButton buttonA = new JButton("A");
    JButton buttonB = new JButton("B");
    JButton buttonC = new JButton("C");
    JLabel display = new JLabel("0", JLabel.CENTER);
    Container contentPane = getContentPane();
    
    public ActionTest() {
        contentPane.add(buttonA, BorderLayout.CENTER);
        contentPane.add(buttonB, BorderLayout.EAST);
        contentPane.add(buttonC, BorderLayout.WEST);
        contentPane.add(display, BorderLayout.SOUTH);

         // your codes
         
         
    }
    
    public static void main(String[] args) {
        ActionTest f = new ActionTest();
        f.setSize(200,100);
        f.setVisible(true);
    }
}