creating a button
import java.awt.color.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame
{
public static void main(String args[])
{
MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
JPanel p=new JPanel();
JButton a=new JButton("YELLOW");
JButton b=new JButton("BLUE");
JButton c=new JButton("RED");
JButton d=new JButton("GREEN");
MyFrame()
{
p.add(a);
p.add(b);
p.add(c);
p.add(d);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==a)
{
p.setBackground(Color.yellow);
}
if(e.getSource()==b)
{
p.setBackground(Color.blue);
}
if(e.getSource()==c)
{
p.setBackground(Color.red);
}
if(e.getSource()==d)
{
p.setBackground(Color.green);
}
}
}
output:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame
{
public static void main(String args[])
{
MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
JPanel p=new JPanel();
JButton a=new JButton("YELLOW");
JButton b=new JButton("BLUE");
JButton c=new JButton("RED");
JButton d=new JButton("GREEN");
MyFrame()
{
p.add(a);
p.add(b);
p.add(c);
p.add(d);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==a)
{
p.setBackground(Color.yellow);
}
if(e.getSource()==b)
{
p.setBackground(Color.blue);
}
if(e.getSource()==c)
{
p.setBackground(Color.red);
}
if(e.getSource()==d)
{
p.setBackground(Color.green);
}
}
}
output:
if u click the yellow button below colour will be display.
No comments:
Post a Comment
Leave the comments