import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame extends JFrame implements WindowListener{ private Container contentPane; private ButtonPanel b1, b2; public ButtonFrame(){ setTitle("ButtonTest"); setSize(300, 200); addWindowListener(this); b1 = new ButtonPanel(); // Create two button panels b2 = new ButtonPanel(); b1.addListener(b1); // Make each panel a listener for b1.addListener(b2); // both sets of buttons b2.addListener(b1); b2.addListener(b2); contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout()); // Set layout to contentPane.add(b1,"North"); // ensure that contentPane.add(b2,"South"); // panels don't // overlap } public void windowClosing(WindowEvent e){ System.exit(0); } public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowOpened(WindowEvent e){} }
The main program that creates and displays a ButtonFrame is the same as before:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { JFrame frame = new ButtonFrame(); frame.show(); } }