menustoolbar.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 102 行

JAVA
102
字号
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** An example class used to demonstrate the use of 
  * menus and toolbars
  */
public class MenusToolbar extends JFrame {
   private JLabel actionInfo
      = new JLabel( "Action information", JLabel.CENTER );

   /** Class constructor method
     * @param titleText Window's title bar text
     */
   public MenusToolbar( String titleText ) {
      super( titleText );
      addWindowListener( new WindowCloser() );

      JToolBar tb = new JToolBar();
      JMenu file = new JMenu( "File" );
      JMenu edit = new JMenu( "Edit" );
      JMenuBar mb = new JMenuBar();
      mb.add( file );
      mb.add( edit );

      NewAction na = new NewAction();
      file.add( na ).setMnemonic( 'N' );
      tb.add( na );
      SaveAction sa = new SaveAction();
      KeyStroke ks
         = KeyStroke.getKeyStroke( KeyEvent.VK_S,
                                   Event.CTRL_MASK );
      file.add( sa ).setAccelerator( ks );
      tb.add( sa );

      tb.addSeparator();
      CutAction cta = new CutAction();
      edit.add( cta );
      tb.add( cta );
      CopyAction cpa = new CopyAction();
      edit.add( cpa );
      tb.add( cpa );
      PasteAction pa = new PasteAction();
      edit.add( pa );
      tb.add( pa );
      
      setJMenuBar( mb );
      Container cp = getContentPane();
      cp.add( tb, BorderLayout.NORTH );
      cp.add( actionInfo, BorderLayout.CENTER );
      setSize( 350, 200 );
      setVisible( true );
   }

   class NewAction extends AbstractAction {
      public NewAction() {
         super( "new", new ImageIcon( "new.gif" ) );
      }
      public void actionPerformed( ActionEvent e ) {
         actionInfo.setText( "new selected" );
      }
   }
   class SaveAction extends AbstractAction {
      public SaveAction() {
         super( "save", new ImageIcon( "save.gif" ) );
      }
      public void actionPerformed( ActionEvent e ) {
         actionInfo.setText( "save selected" );
      }
   }
   class CutAction extends AbstractAction {
      public CutAction() {
         super( "cut", new ImageIcon( "cut.gif" ) );
      }
      public void actionPerformed( ActionEvent e ) {
         actionInfo.setText( "cut selected" );
      }
   }
   class CopyAction extends AbstractAction {
      public CopyAction() {
         super( "copy", new ImageIcon( "copy.gif" ) );
      }
      public void actionPerformed( ActionEvent e ) {
         actionInfo.setText( "copy selected" );
      }
   }
   class PasteAction extends AbstractAction {
      public PasteAction() {
         super( "paste", new ImageIcon( "paste.gif" ) );
      }
      public void actionPerformed( ActionEvent e ) {
         actionInfo.setText( "paste selected" );
      }
   }

   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      new MenusToolbar( "Example Menus and Toolbar" );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?