import javax.swing.*;
+import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class MainWindowsView extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create menu
- JMenuBar menuBar = new JMenuBar();
+ JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
- JMenu fileMenu = new JMenu("File");
- menuBar.add(fileMenu);
- JMenuItem exit = new JMenuItem("Exit");
- exit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- fileMenu.add(exit);
//Get all Swing/AWT JPanel in the views and add them to the main panel.
ArrayList<Component> components = new ArrayList<>();
setContentPane(panel);
}
+ /**
+ * Create menu that support cut/copy/paste.
+ */
+ public JMenuBar createMenuBar() {
+ JMenuItem menuItem;
+ JMenuBar menuBar = new JMenuBar();
+
+ JMenu fileMenu = new JMenu("File");
+
+ menuItem = new JMenuItem("Exit");
+ menuItem.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ System.exit(0);
+ }
+ });
+ fileMenu.add(menuItem);
+
+ JMenu editMenu = new JMenu("Edit");
+ editMenu.setMnemonic(KeyEvent.VK_E);
+
+ menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
+ menuItem.setText("Cut");
+ menuItem.setMnemonic(KeyEvent.VK_T);
+ editMenu.add(menuItem);
+
+ menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
+ menuItem.setText("Copy");
+ menuItem.setMnemonic(KeyEvent.VK_C);
+ editMenu.add(menuItem);
+
+ menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
+ menuItem.setText("Paste");
+ menuItem.setMnemonic(KeyEvent.VK_P);
+ editMenu.add(menuItem);
+
+ menuBar.add(fileMenu);
+ menuBar.add(editMenu);
+ return menuBar;
+ }
+
/**
* Show the GUI. For thread safety,
* this method should be invoked from the