Jbutton Click Event example Advance Java
Change frame Color On Button click Advance Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class FrameDemo extends JFrame implements ActionListener
{
JButton btn;
Container c;
FrameDemo()
{
c=this.getContentPane();
JButton btn= new JButton("Click");
btn.setBounds(100,50,100,50);
c.add(btn);
c.setLayout(null);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
c.setBackground(Color.RED);
}
}
// creating a class ActionDemo (step 2 )
class ActionDemo
{
public static void main( String args[])
{
// Creating object of FrameDemo class ( Step 5 )
FrameDemo f=new FrameDemo();
f.setTitle("click Event ");
f.setVisible(true);
f.setBounds(100,50,700,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}