3 import java
.awt
.event
.ActionEvent
;
4 import java
.awt
.event
.ActionListener
;
5 import java
.util
.ArrayList
;
7 public class MainWindowsView
extends JFrame
{
9 MainWindowsView(String title
, JPanel panel
) {
10 //Create and set up the window.
13 setLocationRelativeTo(null);
14 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
17 JMenuBar menuBar
= new JMenuBar();
19 JMenu fileMenu
= new JMenu("File");
20 menuBar
.add(fileMenu
);
21 JMenuItem exit
= new JMenuItem("Exit");
22 exit
.addActionListener(new ActionListener() {
23 public void actionPerformed(ActionEvent e
) {
29 //Get all Swing/AWT primitive components in the views and add them to the panel.
30 ArrayList
<Component
> components
= new ArrayList
<>();
31 for (int i
= 0; i
< panel
.getComponentCount(); i
++) {
32 if ((panel
.getComponent(i
) instanceof Container
)) {
33 Container subContainer
= (Container
) panel
.getComponent(i
);
34 for (int j
= 0; j
< subContainer
.getComponentCount(); j
++) {
35 components
.add(subContainer
.getComponent(j
));
39 for (Component component
: components
) {
43 setContentPane(panel
);
47 * Show the GUI. For thread safety,
48 * this method should be invoked from the
49 * event-dispatching thread.
51 public void showGUI() {
55 this.setVisible(true);