Friday, 14 June 2013
program in java swing - use button with swing and change background color while click on buttons
// wap in javat ot place 3 buttton red,green,blueto click red button to
//set the background of frameand so on.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class button extends JFrame implements ActionListener
{
JButton b1,b2,b3;
Container c=getContentPane();
button()
{
c.setLayout(new FlowLayout());
b1=new JButton("Red");
b1.addActionListener(this);
c.add(b1);
b2=new JButton("Green");
b2.addActionListener(this);
c.add(b2);
b3=new JButton("Blue");
b3.addActionListener(this);
c.add(b3);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str=="Red")
{
c.setBackground(Color.red);
}
if(str=="Green")
{
c.setBackground(Color.green);
}
if(str=="Blue")
{
c.setBackground(Color.blue);
}
}
public static void main(String args[])
{
button b=new button();
b.setTitle("Button");
b.setSize(500,500);
b.setVisible(true);
b.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment