| 1 | import javax.swing.*; |
| 2 | import java.awt.*; |
| 3 | import java.awt.event.ActionEvent; |
| 4 | import java.awt.event.ActionListener; |
| 5 | import java.util.ArrayList; |
| 6 | |
| 7 | public class MainWindowsView extends JFrame { |
| 8 | MainWindowsView(String title) { |
| 9 | //Create and set up the window. |
| 10 | setTitle(title); |
| 11 | setSize(300, 300); |
| 12 | setLocationRelativeTo(null); |
| 13 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 14 | |
| 15 | //Create menu |
| 16 | JMenuBar menuBar = new JMenuBar(); |
| 17 | setJMenuBar(menuBar); |
| 18 | JMenu fileMenu = new JMenu("File"); |
| 19 | menuBar.add(fileMenu); |
| 20 | JMenuItem exit = new JMenuItem("Exit"); |
| 21 | exit.addActionListener(new ActionListener() { |
| 22 | public void actionPerformed(ActionEvent e) { |
| 23 | System.exit(0); |
| 24 | } |
| 25 | }); |
| 26 | fileMenu.add(exit); |
| 27 | |
| 28 | //Main pane |
| 29 | JPanel panel = new PersonView(); |
| 30 | |
| 31 | //Get all Swing/AWT primitive components in the views and add them to the panel. |
| 32 | ArrayList<Component> components = new ArrayList<>(); |
| 33 | for (int i = 0; i < panel.getComponentCount(); i++) { |
| 34 | if ((panel.getComponent(i) instanceof Container)) { |
| 35 | Container subContainer = (Container) panel.getComponent(i); |
| 36 | for (int j = 0; j < subContainer.getComponentCount(); j++) { |
| 37 | components.add(subContainer.getComponent(j)); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | for (Component component : components) { |
| 42 | panel.add(component); |
| 43 | } |
| 44 | |
| 45 | setContentPane(panel); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Show the GUI. For thread safety, |
| 50 | * this method should be invoked from the |
| 51 | * event-dispatching thread. |
| 52 | */ |
| 53 | public void showGUI() { |
| 54 | |
| 55 | //Display the window. |
| 56 | //this.pack(); |
| 57 | this.setVisible(true); |
| 58 | } |
| 59 | |
| 60 | } |