/** * Like Layout1, but modified to catch window resizings and tweak window layout */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Main extends JFrame implements ComponentListener, ActionListener { // Constants for window size limits private static final int LARGE = 800; //*1 Window size limits private static final int MEDIUM = 600; //*1 private static final int SMALL = 400; //*1 // Other sizes private static final int SMALL_URL = 8; private static final int LARGE_URL = 20; // Panels that we will rearrange private JPanel urlPanel; private JPanel canvasPanel; private JPanel controlsPanel; private JPanel settingsPanel; // Elements of the pop up dialoges private JButton settingsButton; private JButton controlsButton; private JDialog settingsPopup; private JDialog controlsPopup; private JTextField urlField; public static void main (String [] args) { java.awt.EventQueue.invokeLater (new Runnable() { public void run() { new Main (); } }); } public Main () { // Window setup setLocation (100, 100); setSize (LARGE, LARGE); setDefaultCloseOperation (EXIT_ON_CLOSE); setLayout (new BorderLayout()); //*2 Initial layout // Text field at bottom, never moves urlPanel = new JPanel (); //*2 urlPanel.setLayout (new FlowLayout()); //*2 add (urlPanel, BorderLayout.SOUTH); //*2 urlField = new JTextField ("http://www.cs.tufts.edu"); // Drawing canvas in middle, may be tweaked canvasPanel = new JPanel (); add (canvasPanel, BorderLayout.CENTER); //*2 // Control panel, but may be moved to/from popup controlsPanel = new JPanel (); //*2 controlsPanel.setLayout (new FlowLayout ()); //*2 // Contents of the control panel String[] comboStrings = { "Forward", "Back", "Home" }; JComboBox<String> combo = new JComboBox<String> (comboStrings); controlsPanel.add (combo); JButton reloadButton = new JButton ("Reload"); controlsPanel.add (reloadButton); JButton stopButton = new JButton ("Stop"); controlsPanel.add (stopButton); // Settings panel, but may be moved to/from popup settingsPanel = new JPanel (); //*2 settingsPanel.setLayout (new GridLayout (5, 1)); //*2 // Contents of the settings panel JLabel label = new JLabel ("Settings:"); settingsPanel.add (label); JCheckBox graphicsCB = new JCheckBox ("Graphics", true); settingsPanel.add (graphicsCB); JCheckBox animationCB = new JCheckBox ("Animation", true); settingsPanel.add (animationCB); JCheckBox javascriptCB = new JCheckBox ("Javascript", false); settingsPanel.add (javascriptCB); JCheckBox cookiesCB = new JCheckBox ("Cookies", false); settingsPanel.add (cookiesCB); /* * Set up for pop up dialogues */ addComponentListener (this); //*4 Callback for when we are resized settingsPopup = new JDialog (this, false); //*3 Set up popup dialogues ahead of time settingsPopup.setSize (200, 400); controlsPopup = new JDialog (this, false); //*3 controlsPopup.setSize (400, 200); /* * Buttons in URL panel for future use */ settingsButton = new JButton ("Settings"); settingsButton.addActionListener (this); urlPanel.add (settingsButton); controlsButton = new JButton ("Controls"); controlsButton.addActionListener (this); urlPanel.add (controlsButton); urlPanel.add (urlField); setVisible (true); } /* * Listener responds to resize of our whole JFrame * If resized, we modify or reassemble the components of our window */ public void componentResized (ComponentEvent e) { //*4 Dimension size = getSize(); //*4 // SMALL = Use pop up dialogues if (size.width < SMALL || size.height < SMALL) { //*5 Arrange for SMALL size // Borders and backgrounds borders (false); //*5 backgrounds (false); //*5 // Remove panels, use them in popups instead moveToPopups(); //*5 // Activate/deactivate buttons settingsButton.setVisible (true); //*5 controlsButton.setVisible (true); //*5 urlField.setColumns (SMALL_URL); } // MEDIUM = Get rid of borders, use background colors instead else if (size.width < MEDIUM || size.height < MEDIUM) { //*6 For MEDIUM size // Borders and backgrounds borders (false); //*6 backgrounds (true); //*6 // Replace panels moveToMain (); //*6 // Activate/deactivate buttons settingsButton.setVisible (false); //*6 controlsButton.setVisible (false); //*6 urlField.setColumns (LARGE_URL); } // LARGE = Full layout else { //*7 For LARGE size // Borders and backgrounds borders (true); //*7 backgrounds (false); //*7 // Replace panels moveToMain (); //*7 // Activate/deactivate buttons settingsButton.setVisible (false); //*7 controlsButton.setVisible (false); //*7 urlField.setColumns (LARGE_URL); } // May need to force recompute layout revalidate(); repaint(); } /* * Common code fragments from above */ private void borders (boolean which) { //*9 Turn borders on or off if (which) { //*9 urlPanel.setBorder (new LineBorder(Color.BLUE, 2)); //*9 canvasPanel.setBorder (new LineBorder(Color.RED, 2)); //*9 controlsPanel.setBorder (new LineBorder(Color.BLUE, 2)); //*9 settingsPanel.setBorder (new LineBorder(Color.BLUE, 2)); //*9 urlPanel.setBorder (new LineBorder(Color.BLUE, 2)); //*9 } else { //*9 urlPanel.setBorder (null); //*9 canvasPanel.setBorder (null); //*9 controlsPanel.setBorder (null); //*9 settingsPanel.setBorder (null); //*9 urlPanel.setBorder (null); //*9 } } private void backgrounds (boolean which) { //*10 Turn backgrounds on or off if (which) { //*10 controlsPanel.setBackground (Color.lightGray); //*10 settingsPanel.setBackground (Color.lightGray); //*10 urlPanel.setBackground (Color.lightGray); //*10 } else { //*10 controlsPanel.setBackground (null); //*10 settingsPanel.setBackground (null); //*10 urlPanel.setBackground (null); //*10 } } private void moveToPopups () { //*11 Move control panels to popups remove (controlsPanel); //*11 remove (settingsPanel); //*11 settingsPopup.add (settingsPanel); //*11 controlsPopup.add (controlsPanel); //*11 } private void moveToMain () { //*12 Move control panels to main window settingsPopup.remove (settingsPanel); //*12 controlsPopup.remove (controlsPanel); //*12 add (controlsPanel, BorderLayout.NORTH); //*12 add (settingsPanel, BorderLayout.WEST); //*12 } // The other abstract methods public void componentShown(ComponentEvent e) {} //*4 public void componentHidden(ComponentEvent e) {} //*4 public void componentMoved(ComponentEvent e) {} //*4 // From the 2 buttons public void actionPerformed (ActionEvent e) { //*8 Activate popups if necessary if (e.getSource()==controlsButton) { //*8 controlsPopup.setVisible (true); //*8 } else if (e.getSource()==settingsButton) { //*8 settingsPopup.setVisible (true); //*8 } } }