/** * This is the hook through which all menu items are created. It registers the result with the * menuitem hashtable so that it can be fetched with getMenuItem(). * * @see #getMenuItem */ protected JMenuItem createMenuItem(String cmd) { JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix)); URL url = getResource(cmd + imageSuffix); if (url != null) { mi.setHorizontalTextPosition(JButton.RIGHT); mi.setIcon(new ImageIcon(url)); } String astr = getResourceString(cmd + actionSuffix); if (astr == null) { astr = cmd; } mi.setActionCommand(astr); Action myaction = getAction(astr); // if this is a known action if (myaction != null) { mi.addActionListener(myaction); myaction.addPropertyChangeListener(createActionChangeListener(mi)); // System.out.println("myaction not null: astr:"+astr+" enabled:"+myaction.isEnabled()); mi.setEnabled(myaction.isEnabled()); } else { System.err.println("Error:TextViewer: createMenuItem: myaction is null: astr:" + astr); // causes the item to be greyed out mi.setEnabled(false); } menuItems.put(cmd, mi); return mi; }
/** This is the hook through which all menu items are created. */ protected JMenuItem createMenuItem(String cmd) { JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix)); URL url = getResource(cmd + imageSuffix); if (url != null) { mi.setHorizontalTextPosition(JButton.RIGHT); mi.setIcon(new ImageIcon(url)); } String astr = getProperty(cmd + actionSuffix); if (astr == null) { astr = cmd; } mi.setActionCommand(astr); Action a = getAction(astr); if (a != null) { mi.addActionListener(a); a.addPropertyChangeListener(createActionChangeListener(mi)); mi.setEnabled(a.isEnabled()); } else { mi.setEnabled(false); } return mi; }
/** 程式的GUI畫面配置,並登入按鈕、選項監聽。 */ private void GUI() { JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); JMenuItem file_newMenuItem = new JMenuItem("New"); file_newMenuItem.setIcon( new ImageIcon( DocumentEditGUI.class.getResource("/javax/swing/plaf/metal/icons/ocean/file.gif"))); file_newMenuItem.addActionListener(new SystemFileNew()); fileMenu.add(file_newMenuItem); JMenuItem file_openMenuItem = new JMenuItem("Open File..."); file_openMenuItem.setIcon( new ImageIcon( DocumentEditGUI.class.getResource( "/com/sun/java/swing/plaf/windows/icons/TreeOpen.gif"))); file_openMenuItem.addActionListener(new SystemFileOpen()); fileMenu.add(file_openMenuItem); JMenu file_saveMenu = new JMenu("Save"); fileMenu.add(file_saveMenu); JMenuItem file_save_saveMenuItem = new JMenuItem("Save"); file_save_saveMenuItem.setIcon( new ImageIcon( DocumentEditGUI.class.getResource( "/com/sun/java/swing/plaf/windows/icons/FloppyDrive.gif"))); file_save_saveMenuItem.addActionListener(new SystemFileSave()); file_save_saveMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); file_saveMenu.add(file_save_saveMenuItem); JMenuItem file_save_save_asMenuItem = new JMenuItem("Save as"); file_save_save_asMenuItem.addActionListener(new SystemFileSaveAS()); file_saveMenu.add(file_save_save_asMenuItem); JMenuItem file_save_save_allMenuItem = new JMenuItem("Save all"); file_save_save_allMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)); file_save_save_allMenuItem.addActionListener(new SystemFileSaveAll()); file_saveMenu.add(file_save_save_allMenuItem); JMenu file_closeMenu = new JMenu("Close"); fileMenu.add(file_closeMenu); JMenuItem file_close_closeMenuItem = new JMenuItem("Close File"); file_close_closeMenuItem.addActionListener(new SystemFileClose()); file_closeMenu.add(file_close_closeMenuItem); JMenuItem file_close_close_all_fileMenuItem = new JMenuItem("Close all File"); file_close_close_all_fileMenuItem.addActionListener(new SystemFileCloseAll()); file_closeMenu.add(file_close_close_all_fileMenuItem); JMenuItem file_exitMenuItem = new JMenuItem("Exit"); file_exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK)); file_exitMenuItem.addActionListener(new SystemExit()); fileMenu.add(file_exitMenuItem); JMenu editMenu = new JMenu("Edit"); menuBar.add(editMenu); replace_searchMenuItem.addActionListener(replace_search); replace_searchMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); editMenu.add(replace_searchMenuItem); SystemEditCut_Copy_Paste cut_copy_paste = new SystemEditCut_Copy_Paste(); cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); cutMenuItem.addActionListener(cut_copy_paste); editMenu.add(cutMenuItem); copyMenutem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); copyMenutem.addActionListener(cut_copy_paste); editMenu.add(copyMenutem); pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); pasteMenuItem.addActionListener(cut_copy_paste); editMenu.add(pasteMenuItem); JMenuItem pathMenuItem = new JMenuItem("Default AutoSave-Path"); pathMenuItem.addActionListener(new SystemEditSetPath()); editMenu.add(pathMenuItem); JMenu helpMenu = new JMenu("Help"); menuBar.add(helpMenu); JMenuItem HelpMenuItem = new JMenuItem("Help"); HelpMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, help, "Help!", JOptionPane.INFORMATION_MESSAGE); } }); helpMenu.add(HelpMenuItem); JMenuItem AboutNewMenuItem = new JMenuItem("About"); AboutNewMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, about, "About...", JOptionPane.INFORMATION_MESSAGE); } }); helpMenu.add(AboutNewMenuItem); JToolBar toolBar = new JToolBar(); getContentPane().add(toolBar, BorderLayout.NORTH); scrollPane.setViewportView(td); JButton newFileButton = new JButton("New"); newFileButton.setIcon( new ImageIcon( DocumentEditGUI.class.getResource("/javax/swing/plaf/metal/icons/ocean/file.gif"))); newFileButton.setToolTipText("New File."); newFileButton.addActionListener(new SystemFileNew()); toolBar.add(newFileButton); JButton openFileButton = new JButton("Open"); openFileButton.setIcon( new ImageIcon( DocumentEditGUI.class.getResource( "/javax/swing/plaf/metal/icons/ocean/directory.gif"))); openFileButton.setToolTipText("Open File."); openFileButton.addActionListener(new SystemFileOpen()); toolBar.add(openFileButton); JButton save_asFileButton = new JButton("Save as"); save_asFileButton.setToolTipText("Save as File."); save_asFileButton.addActionListener(new SystemFileSaveAS()); toolBar.add(save_asFileButton); JButton saveFileButton = new JButton("Save"); saveFileButton.setIcon( new ImageIcon( DocumentEditGUI.class.getResource("/javax/swing/plaf/metal/icons/ocean/floppy.gif"))); saveFileButton.setToolTipText("Savet his File."); saveFileButton.addActionListener(new SystemFileSave()); toolBar.add(saveFileButton); JButton save_allFileButton = new JButton("Save all"); save_allFileButton.setToolTipText("Save all File."); save_allFileButton.addActionListener(new SystemFileSaveAll()); toolBar.add(save_allFileButton); JButton closeButton = new JButton("Close"); closeButton.setToolTipText("Close this file."); closeButton.addActionListener(new SystemFileClose()); toolBar.add(closeButton); JButton close_allButton = new JButton("Close all"); close_allButton.setToolTipText("Close al File."); close_allButton.addActionListener(new SystemFileCloseAll()); toolBar.add(close_allButton); }