📄 mainmenu.java
字号:
// file: MainMenu.java
//
// import necessary java libraries
//
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.applet.*;
// class: MainMenu
//
// implement the menu driven system that drives the applet
//
// hierarchy: MainMenu
//
class MainMenu extends JPanel implements ActionListener, Constants {
// ---------------------------------------------------
//
// declare global variables
//
// ---------------------------------------------------
// creates a fram to manipulate the configuration options
//
public JFrame config = null;
// textfields and labels for the connector properties
//
public JLabel nameLabel = null;
public JTextField nameField = null;
// declare a reference to the DiGraph
//
public DiGraph digraph = null;
// menu that holds all functionality
//
public JMenu menu = null;
public JMenu subMenu = null;
public JMenu subSubMenu = null;
public JMenuBar menuBar = null;
public JMenuItem menuItem = null;
// declare a frame to display the help message
//
JFrame helpFrame = null;
// declare a text area to display documentation
//
public JTextArea textArea = null;
// declare a scrollable pane for the text area
//
public JScrollPane scrollPane = null;
// declare the layout manager
//
public GridBagLayout gridbag = new GridBagLayout();
// ---------------------------------------------------
//
// declare class constructors
//
// ---------------------------------------------------
// method: MainMenu
//
// arguments:
//
// returns : none
//
// constructor initializes objects and containers
//
MainMenu(DiGraph DiGraph) {
super();
// set the default layout
//
setLayout(gridbag);
// set the reference to vertex box
//
digraph = DiGraph;
// instantiate the frame
//
helpFrame = new JFrame("Help");
// instantiate the text area to display documentation
//
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
// instantiate the scrollable pane for the text area
//
scrollPane = new JScrollPane(textArea);
// set the scroll pane dismensions
//
scrollPane.setSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
scrollPane.setPreferredSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
scrollPane.setMinimumSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
scrollPane.setMaximumSize(new Dimension(HELP_WIDTH, HELP_HEIGHT));
// initialize the menu bar
//
menuBar = new JMenuBar();
// create the file menu
//
menu = new JMenu(fileStr);
menu.setFont(newFont);
menuBar.add(menu);
// add the menu items
//
menuItem = new JMenuItem(clearStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(clearStr);
menu.add(menuItem);
menuItem = new JMenuItem(openStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
ActionEvent.CTRL_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(openStr);
menu.add(menuItem);
menuItem = new JMenuItem(saveStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
ActionEvent.CTRL_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(saveStr);
menu.add(menuItem);
menuItem = new JMenuItem(saveAsStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(saveAsStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem(quitStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(quitStr);
menu.add(menuItem);
// create the edit menu
//
menu = new JMenu(editStr);
menu.setFont(newFont);
menuBar.add(menu);
menuItem = new JMenuItem(cutStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
ActionEvent.CTRL_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(cutStr);
menu.add(menuItem);
menuItem = new JMenuItem(copyStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
ActionEvent.CTRL_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(copyStr);
menu.add(menuItem);
menuItem = new JMenuItem(pasteStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
ActionEvent.CTRL_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(pasteStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem(insArcStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(insArcStr);
menu.add(menuItem);
menuItem = new JMenuItem(insSelfArcStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(insSelfArcStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem(delArcStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(delArcStr);
menu.add(menuItem);
menuItem = new JMenuItem(delBlkStr);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
ActionEvent.ALT_MASK));
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(delBlkStr);
menu.add(menuItem);
// create the vertex menu
//
menu = new JMenu(compStr);
menu.setFont(newFont);
menuBar.add(menu);
menuItem = new JMenuItem(startStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(startStr);
menu.add(menuItem);
menuItem = new JMenuItem(termStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(termStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("Node");
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(contStr);
menu.add(menuItem);
// create the utility menu
//
menu = new JMenu(utiStr);
menu.setFont(newFont);
menuBar.add(menu);
menuItem = new JMenuItem(digraphStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(digraphStr);
menu.add(menuItem);
menuItem = new JMenuItem(loadDigraphStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(loadDigraphStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem(lmTesterStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(lmTesterStr);
menu.add(menuItem);
// create the help menu
//
menu = new JMenu(helpStr);
menu.setFont(newFont);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(menu);
menuItem = new JMenuItem(overviewStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(overviewStr);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem(tutorialStr);
menuItem.setFont(newFont);
menuItem.addActionListener(this);
menuItem.setActionCommand(tutorialStr);
menu.add(menuItem);
// set the dimensions of the menu
//
this.setSize(new Dimension(digraph.MAX_WIDTH, 30));
this.setMinimumSize(new Dimension(digraph.MAX_WIDTH, 30));
this.setMaximumSize(new Dimension(digraph.MAX_WIDTH, 30));
this.setPreferredSize(new Dimension(digraph.MAX_WIDTH, 30));
}
// ---------------------------------------------------
//
// declare class methods
//
// ---------------------------------------------------
// method: add_components
//
// arguments: none
// return : none
//
// adds components to the control panel
//
public void add_components () {
constrain(this, menuBar, 0, 0,
GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER,
GridBagConstraints.BOTH, GridBagConstraints.CENTER,
1, 1, 0, 0, 0, 0);
}
// method: actionPerformed
//
// arguments:
// ActionEvent e: action command that is fired
//
// return : none
//
// method listens for actions taking place on text only menu items
//
public void actionPerformed(ActionEvent e) {
// get the source of the action that just occured
//
String source = e.getActionCommand();
// depending on the source of the event take the appropriate action
//
if(source.equals(openStr)) {
JFileChooser loadChooser=null;
if(digraph.paramFile != null) {
loadChooser = new JFileChooser(digraph.paramFile);
}
else {
String cwd = System.getProperty("user.dir");
loadChooser = new JFileChooser(cwd);
}
// set the font and button text for the load chooser
//
loadChooser.setFont(newFont);
String loadApprove = new String("Load");
loadChooser.setApproveButtonText(loadApprove);
// display the file chooser object
//
int returnVal = loadChooser.showOpenDialog(digraph);
// check to see if a file was selected by the user
//
if (returnVal == JFileChooser.APPROVE_OPTION) {
// retrieve the selected file
//
digraph.paramFile = loadChooser.getSelectedFile();
// read the parameters to file
//
digraph.read();
}
} else if(source.equals(saveStr)) {
boolean searchWord = digraph.fullNode();
if(!searchWord) {
// display a warning message indicating that you cannot have
// two vertices with the same name
//
JFrame frame = new JFrame("Warning");
JOptionPane.showMessageDialog(frame, "Every node should have word list!");
}
else {
// write the parameters to file
//
if (digraph.paramFile != null) {
digraph.write();
}
else {
// initialize the file chooser object
//
String cwd = System.getProperty("user.dir");
JFileChooser saveChooser = new JFileChooser(cwd);
// set the font and button text for the save chooser
//
saveChooser.setFont(newFont);
String saveApprove = new String("Save");
saveChooser.setApproveButtonText(saveApprove);
// display the file chooser object
//
int returnVal = saveChooser.showOpenDialog(digraph);
// check to see if a file was selected by the user
//
if (returnVal == JFileChooser.APPROVE_OPTION) {
// retrieve the selected file
//
digraph.paramFile = saveChooser.getSelectedFile();
// write the parameters to file
//
digraph.write();
}
}
}
} else if(source.equals(saveAsStr)) {
boolean searchWord = digraph.fullNode();
if(!searchWord) {
// display a warning message indicating that you cannot have
// two vertices with the same name
//
JFrame frame = new JFrame("Warning");
JOptionPane.showMessageDialog(frame, "Every node should have word list!");
}
else {
JFileChooser saveChooser = null;
// initialize the file chooser object
//
if(digraph.paramFile != null) {
saveChooser = new JFileChooser(digraph.paramFile);
}
else {
String cwd = System.getProperty("user.dir");
saveChooser = new JFileChooser(cwd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -