Java
Slip14
Q.1) Write a Java program which will create a frame if we try to close it, it should change it’s color and it remains visible on the screen(Use swing). [Marks 30]
Solution
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class Ccolor extends JFrame
{
JPanel p = new JPanel();
Ccolor()
{
setVisible(true);
setSize(400,400);
setLocation(200,300);
setTitle("Swing Background");
add(p);
p.setBackground(Color.green);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(p.getBackground() == Color.green)
p.setBackground(Color.blue);
else
p.setBackground(Color.green);
setDefaultCloseOperation(1);
}});
}
public static void main(String args[])
{
new Ccolor();
}
}
Tags:
java