Commit | Line | Data |
---|---|---|
60971873 | 1 | import javax.swing.*; |
c281942a | 2 | import javax.swing.text.DefaultEditorKit; |
089fcbfc | 3 | import java.awt.*; |
416e35f7 JB |
4 | import java.awt.event.ActionEvent; |
5 | import java.awt.event.ActionListener; | |
c281942a | 6 | import java.awt.event.KeyEvent; |
089fcbfc | 7 | import java.util.ArrayList; |
60971873 JB |
8 | |
9 | public class MainWindowsView extends JFrame { | |
f1e9d6d2 | 10 | |
b974e749 | 11 | MainWindowsView(String title, JPanel panel) { |
60971873 JB |
12 | //Create and set up the window. |
13 | setTitle(title); | |
089fcbfc JB |
14 | setSize(300, 300); |
15 | setLocationRelativeTo(null); | |
60971873 JB |
16 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
17 | ||
416e35f7 | 18 | //Create menu |
c281942a | 19 | JMenuBar menuBar = createMenuBar(); |
416e35f7 | 20 | setJMenuBar(menuBar); |
416e35f7 | 21 | |
3d9fdaf4 | 22 | //Get all Swing/AWT JPanel in the views and add them to the main panel. |
089fcbfc JB |
23 | ArrayList<Component> components = new ArrayList<>(); |
24 | for (int i = 0; i < panel.getComponentCount(); i++) { | |
3d9fdaf4 | 25 | components.add(panel.getComponent(i)); |
089fcbfc JB |
26 | } |
27 | for (Component component : components) { | |
28 | panel.add(component); | |
29 | } | |
60971873 | 30 | |
089fcbfc | 31 | setContentPane(panel); |
60971873 JB |
32 | } |
33 | ||
c281942a JB |
34 | /** |
35 | * Create menu that support cut/copy/paste. | |
36 | */ | |
37 | public JMenuBar createMenuBar() { | |
38 | JMenuItem menuItem; | |
39 | JMenuBar menuBar = new JMenuBar(); | |
40 | ||
41 | JMenu fileMenu = new JMenu("File"); | |
42 | ||
43 | menuItem = new JMenuItem("Exit"); | |
44 | menuItem.addActionListener(new ActionListener() { | |
45 | public void actionPerformed(ActionEvent e) { | |
46 | System.exit(0); | |
47 | } | |
48 | }); | |
49 | fileMenu.add(menuItem); | |
50 | ||
51 | JMenu editMenu = new JMenu("Edit"); | |
52 | editMenu.setMnemonic(KeyEvent.VK_E); | |
53 | ||
54 | menuItem = new JMenuItem(new DefaultEditorKit.CutAction()); | |
55 | menuItem.setText("Cut"); | |
56 | menuItem.setMnemonic(KeyEvent.VK_T); | |
57 | editMenu.add(menuItem); | |
58 | ||
59 | menuItem = new JMenuItem(new DefaultEditorKit.CopyAction()); | |
60 | menuItem.setText("Copy"); | |
61 | menuItem.setMnemonic(KeyEvent.VK_C); | |
62 | editMenu.add(menuItem); | |
63 | ||
64 | menuItem = new JMenuItem(new DefaultEditorKit.PasteAction()); | |
65 | menuItem.setText("Paste"); | |
66 | menuItem.setMnemonic(KeyEvent.VK_P); | |
67 | editMenu.add(menuItem); | |
68 | ||
69 | menuBar.add(fileMenu); | |
70 | menuBar.add(editMenu); | |
71 | return menuBar; | |
72 | } | |
73 | ||
60971873 JB |
74 | /** |
75 | * Show the GUI. For thread safety, | |
76 | * this method should be invoked from the | |
77 | * event-dispatching thread. | |
78 | */ | |
79 | public void showGUI() { | |
80 | ||
81 | //Display the window. | |
089fcbfc | 82 | //this.pack(); |
60971873 JB |
83 | this.setVisible(true); |
84 | } | |
416e35f7 | 85 | } |