JLabelJButton with color name on it and the corresponding background color. JLabel's color is changed to the button's color. 
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)

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)
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 4WhoAmI.java randomly gives a Simpson character name.
The user then asked to click the correct picture. ![]() Apu.gif |
![]() Bart.gif |
![]() Burns.gif |
![]() Homer.gif |
![]() Krusty.gif |
![]() Lisa.gif |
![]() Maggie.gif |
![]() Marge.gif |
![]() Moe.gif |
![]() Ned.gif |



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 5import 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)java ActionTest:buttonA (ii) click on buttonB (iii) click on buttonC?
display after you click on buttonC, then click on buttonB,
and then click on buttonA?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);
}
}