/*By clicking on the button you can set your window background on your desired color*/
/*I recommend you this code if your eclipse doesn't support applet viewer
else refer to our applet code for window background which is more easer*/
package p1;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class Background extends Frame implements ActionListener {
Button b1,b2,b3,b4;
String s="Please click the button for new Background",s2;
Font f1,f2,f3;
Background() {
setLayout(new FlowLayout());
f1 = new Font("Arial",Font.BOLD,18);
f2 = new Font("Forte",Font.PLAIN,24);
f3 = new Font("Elephant",Font.ITALIC,28);
b1=new Button("Blue");
b2=new Button("Green");
b3=new Button("Cyan");
b4=new Button("Red");
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void paint(Graphics g) {
g.setFont(f1);
g.drawString(s,30,40);
g.setFont(f3);
g.drawString(s2,150,250);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String k=e.getActionCommand();
if(k.equals("Blue")) {
setBackground(Color.blue);
s2="This is Blue Color Background";
}
if(k.equals("Green")) {
setBackground(Color.green);
s2="This is Green Color Background";
}
if(k.equals("Cyan")) {
setBackground(Color.cyan);
s2="This is Cyan Color Background";
}
if(k.equals("Red")) {
setBackground(Color.red);
s2="This is Red Color Background";
}
}
public static void main(String args[]) {
Background b=new Background();
b.setSize(new Dimension(200,300));
b.setVisible(true);
}
}
Leave a comment