Skip to content

Instantly share code, notes, and snippets.

@roshanadh
Created April 10, 2019 06:56
Show Gist options
  • Save roshanadh/c1273f756bdc9c5ba5e5b44a98de56ec to your computer and use it in GitHub Desktop.
Save roshanadh/c1273f756bdc9c5ba5e5b44a98de56ec to your computer and use it in GitHub Desktop.
Multicast events in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MulticastDemo implements ActionListener
{
JFrame frame;
static int count=0;
JButton newWindow;
JButton closeAll;
public static void main(String[] args)
{
new MulticastDemo();
}
MulticastDemo()
{
frame=new JFrame("Main Window");
newWindow=new JButton("New Window");
closeAll=new JButton("Close All Windows");
frame.setVisible(true);
frame.setSize(300,300);
frame.setLayout(new FlowLayout());
frame.add(newWindow);
frame.add(closeAll);
newWindow.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
CloseFrame f=new CloseFrame();
count++;
f.setTitle("Window "+count);
f.setVisible(true);
f.setSize(250,150);
f.setLocation(40*count,40*count);
closeAll.addActionListener(f);
}
}
class CloseFrame extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment