Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Lab Internals 2. Show all posts
Showing posts with label Lab Internals 2. Show all posts

Saturday, 23 February 2019

Fifth Week Term Work Program 2

Question

Program:

//Displays a frame containing a three button. Pressing the
//button causes the background of the frame to change from
//white to define color.

import java.awt.*;
import java.awt.event.*;
import java.awt.Button;

public class threecolor {
public static void main(String[] args) {
Frame f = new ChangethreeColorFrame("Colors");
f.setSize(210, 200);
f.setVisible(true);
}
}
//Frame class
class ChangethreeColorFrame extends Frame {
public ChangethreeColorFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);

//Add  button to frame and attach listener
Button b1 = new Button("Red");
b1.setBounds(20, 150, 50, 30);
add(b1);
b1.addActionListener(new ButtonListener());
Button b2 = new Button("Blue");
b2.setBounds(80, 150, 50, 30);
add(b2);
b2.addActionListener(new ButtonListener());
Button b3 = new Button("Green");
b3.setBounds(140, 150, 50, 30);
add(b3);
b3.addActionListener(new ButtonListener());

//Attach window listener
addWindowListener(new WindowCloser());
}

//Listener for button
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {

  if (evt.getActionCommand().equals("Red"))
    setBackground(Color.RED);
  else if (evt.getActionCommand().equals("Blue"))
    setBackground(Color.BLUE);
  else if (evt.getActionCommand().equals("Green"))
    setBackground(Color.GREEN);
else
  setBackground(Color.white);
}
}

//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}