SwingWindow.java

package window;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingWindow {

  private MenuFrame mf;
  private MainPanel mp;

  public SwingWindow(String str) {
    // the standard view (Java look and feel)
    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) { }

    // make sure we have nice windows decorations
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);

    // create a main frame and add components to it
    mf = new MenuFrame(str);
    mf.getContentPane().add(createComponents(), BorderLayout.CENTER);

    // finish setting up the frame and show it
    mf.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        Object[] options = {"Yes", "No"};
        int answer = JOptionPane.showOptionDialog(null, "Would you like to close this program?", "Question",
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
        if (answer == 0) {
          System.exit(0);
        } else {
          mf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
      }
    });
    mf.pack();
    mf.setSize(400, 460);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = mf.getSize();
    mf.setLocation(Math.max(0, (screenSize.width - windowSize.width) / 2), Math.max(0, (screenSize.height - windowSize.height) / 2));
    mf.setResizable(false);
    mf.setVisible(true);
  }

  public String getTextField() {
    return mp.getTextField();
  }

  public void setTextField(String str) {
    mp.setTextField(str);
  }

  public void setMessage(String str) {
    mp.messageIn(str);
  }

  private Component createComponents() {
    // create a main panel and put a border around it
    mp = new MainPanel();
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // top, left, bottom, right
    panel.setLayout(new GridLayout(0, 1));
    panel.add(mp);

    return panel;
  }
}