(1) SketchFrame.java<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

package com.service;

 

import javax.swing.ButtonGroup;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.KeyStroke;

import static java.awt.event.InputEvent.*;

 

public class SketchFrame extends JFrame {

    // 构造函数

    public SketchFrame(String title){

       setTitle(title);                      // 设置窗口标题

       setDefaultCloseOperation( EXIT_ON_CLOSE );

       setJMenuBar( menuBar );                 // 添加菜单栏到窗口

      

       JMenu fileMenu= new JMenu( "File" );    // 创建 File 菜单

       JMenu elementMenu = new JMenu( "Elements" ); // 创建 Elements 菜单

       fileMenu.setMnemonic( 'F' );            // 创建快捷键

       elementMenu.setMnemonic( 'E' );         // 创建快捷键

      

       // 构造 file 下拉菜单

       newItem = fileMenu.add( "new" );        // 添加 New 项目

       openItem = fileMenu.add( "Open" );      // 添加 Open 项目

       closeItem = fileMenu.add( "Close" );    // 添加 Close 项目

       fileMenu.addSeparator();          // 添加 Separator

       saveItem = fileMenu.add( "save" );      // 添加 Save 项目

       saveAsItem = fileMenu.add( "Save as" ); // 添加 Save As 项目

       fileMenu.addSeparator();            // 添加 Separator

        printItem = fileMenu.add( "print" );    // 添加 Print 项目

      

       // 添加 File 菜单加速键

       newItem .setAccelerator(KeyStroke.getKeyStroke( 'N' , CTRL_DOWN_MASK ));

       openItem .setAccelerator(KeyStroke.getKeyStroke( 'O' , CTRL_DOWN_MASK ));

       saveItem .setAccelerator(KeyStroke.getKeyStroke( 'S' , CTRL_DOWN_MASK ));

       printItem .setAccelerator(KeyStroke.getKeyStroke( 'P' , CTRL_DOWN_MASK ));

      

       // 构造 Element 下拉菜单

       elementMenu.add( lineItem = new JRadioButtonMenuItem( "Line" , true ));

       elementMenu.add( rectangleItem = new JRadioButtonMenuItem( "Rectangle" , false ));

       elementMenu.add( circleItem = new JRadioButtonMenuItem( "Circle" , false ));

       elementMenu.add( curveItem = new JRadioButtonMenuItem( "Curve" , false ));

       ButtonGroup types= new ButtonGroup();

       types.add( lineItem );

       types.add( rectangleItem );

       types.add( circleItem );

       types.add( curveItem );

      

       // 添加 element 类型加速键

       lineItem .setAccelerator(KeyStroke.getKeyStroke( 'L' , CTRL_DOWN_MASK ));

        rectangleItem .setAccelerator(KeyStroke.getKeyStroke( 'E' , CTRL_DOWN_MASK ));

        circleItem .setAccelerator(KeyStroke.getKeyStroke( 'I' , CTRL_DOWN_MASK ));

       curveItem .setAccelerator(KeyStroke.getKeyStroke( 'V' , CTRL_DOWN_MASK ));

      

       elementMenu.addSeparator();

      

       // 创建 color 子菜单

       JMenu colorMenu = new JMenu( "Color" );    //Color 子菜单

       elementMenu.add(colorMenu);              // 添加子菜单

       colorMenu.add( redItem = new JCheckBoxMenuItem( "Red" , false ));

       colorMenu.add( yellowItem = new JCheckBoxMenuItem( "Yellow" , false ));

       colorMenu.add( greenItem = new JCheckBoxMenuItem( "Green" , false ));

       colorMenu.add( blueItem = new JCheckBoxMenuItem( "Blue" , true ));

      

       // 添加 element color 加速键

       redItem .setAccelerator(KeyStroke.getKeyStroke( 'R' , CTRL_DOWN_MASK ));

        yellowItem .setAccelerator(KeyStroke.getKeyStroke( 'Y' , CTRL_DOWN_MASK ));

       greenItem .setAccelerator(KeyStroke.getKeyStroke( 'G' , CTRL_DOWN_MASK ));

       blueItem .setAccelerator(KeyStroke.getKeyStroke( 'B' , CTRL_DOWN_MASK ));

      

       menuBar .add(fileMenu);         // 添加 file 菜单

       menuBar .add(elementMenu);      // 添加 element 菜单

    }

   

    private JMenuBar menuBar = new JMenuBar();    // 窗口菜单栏

   

    //File 菜单项目

    private JMenuItem newItem , openItem , closeItem , saveItem , saveAsItem , printItem ;

   

    //Element 菜单项目

    private JRadioButtonMenuItem lineItem , rectangleItem , circleItem , curveItem , textItem ;

    private JCheckBoxMenuItem redItem , yellowItem , greenItem , blueItem ;

}

 

(2) Sketcher.java
package com.test;

 

import java.awt.Dimension;
import java.awt.Toolkit;

 

import javax.swing.SwingUtilities;

 

import com.service.SketchFrame;

 

public class Sketcher {

 

       public static void main(String[] args) {
              SwingUtilities.invokeLater(
                            new Runnable(){
                                   public void run(){
                                          creatGUI();
                                   }
                            }
              );

 

       }
      
       static void creatGUI(){
              window = new SketchFrame("Sketcher");
              Toolkit theKit = window.getToolkit();
              Dimension wndSize = theKit.getScreenSize();
             
              window.setBounds(wndSize.width/4, wndSize.height/4,   //位置
                                     wndSize.width/2, wndSize.height/2);  //大小
             
              window.setVisible(true);
                    
       }

 

       private static SketchFrame window;
}