⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sketchframe.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
// Frame for the Sketcher application
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class SketchFrame extends JFrame
                         implements Constants
{

  // Constructor
  public SketchFrame(String title)
  {
    super(title);                                // Call the base constructor

    setJMenuBar(menuBar);                        // Add the menu bar to the window

    JMenu fileMenu = new JMenu("File");                    // Create File menu
    JMenu elementMenu = new JMenu("Elements");             // Create Elements menu
    fileMenu.setMnemonic('F');                             // Create shortcut
    elementMenu.setMnemonic('E');                          // Create shortcut

   // We will construct the file pull down menu here using actions...
    addMenuItem(fileMenu, newAction = new FileAction("New", "Create new sketch"),
                                    KeyStroke.getKeyStroke('N',Event.CTRL_MASK ));
    addMenuItem(fileMenu, openAction = new FileAction("Open", "Open an old sketch"),
                                     KeyStroke.getKeyStroke('O',Event.CTRL_MASK ));
    addMenuItem(fileMenu, closeAction = new FileAction("Close", "Close this sketch"),null);
    fileMenu.addSeparator();                                    // Add separator
    addMenuItem(fileMenu, saveAction = new FileAction("Save", "If you want it, save it"),
                                           KeyStroke.getKeyStroke('S',Event.CTRL_MASK ));
    addMenuItem(fileMenu, saveAsAction = new FileAction("Save As...", "Save as new file"),null);
    fileMenu.addSeparator();                                     // Add separator
    addMenuItem(fileMenu, printAction = new FileAction("Print", "Masterpiece hardcopy"),
                                     KeyStroke.getKeyStroke('P',Event.CTRL_MASK ));

   // We will add the types menu items here using actions...
    addMenuItem(elementMenu, lineAction = new TypeAction("Line", LINE, "Draw lines!"),null);
    addMenuItem(elementMenu, rectangleAction =
                                       new TypeAction("Rectangle",RECTANGLE, "Draw boxes!"),null);
    addMenuItem(elementMenu, circleAction = new TypeAction("Circle", CIRCLE, "Draw circles!"),null);
    addMenuItem(elementMenu, curveAction = new TypeAction("Curve", CURVE, "...and curves wahay!"),null);

    elementMenu.addSeparator();

    JMenu colorMenu = new JMenu("Color");                  // Color sub-menu
    elementMenu.add(colorMenu);                            // Add the sub-menu

    // We will add the color menu items here using actions...
    addMenuItem(colorMenu, redAction = new ColorAction("Red", Color.red, "Wrox red"),null);
    addMenuItem(colorMenu, yellowAction = new ColorAction("Yellow", Color.yellow, "Jaundice yellow"),null);
    addMenuItem(colorMenu, greenAction = new ColorAction("Green", Color.green, "Green as paint"),null);
    addMenuItem(colorMenu, blueAction = new ColorAction("Blue", Color.blue, "Blue, blue, my heart is..."),null);

    // Handles color menu items

    menuBar.add(fileMenu);                                 // Add the file menu
    menuBar.add(elementMenu);                              // Add the element menu

    // Add file buttons
    toolBar.addSeparator();                                 // Space at the start
    addToolBarButton(newAction);
    addToolBarButton(openAction);
    addToolBarButton(saveAction);
    addToolBarButton(printAction);
   
    // Add element type buttons
    toolBar.addSeparator();
    addToolBarButton(lineAction);
    addToolBarButton(rectangleAction);
    addToolBarButton(circleAction);
    addToolBarButton(curveAction);

    // Add element color buttons
    toolBar.addSeparator();
    addToolBarButton(redAction);
    addToolBarButton(yellowAction);
    addToolBarButton(greenAction);
    addToolBarButton(blueAction);
    toolBar.addSeparator();                                // Space at the end

    toolBar.setBorder(BorderFactory.createCompoundBorder(       // Toolbar border
               BorderFactory.createLineBorder(Color.darkGray),
               BorderFactory.createEmptyBorder(2,2,4,2)));   

    toolBar.setFloatable(false);                           // Inhibit toolbar floating
    getContentPane().add(toolBar, BorderLayout.NORTH);     // Add toolbar  
 }

  // We will add inner classes defining action objects here...
 
  //  FILE ACTION INNER CLASS
   class FileAction extends AbstractAction
   {    
    // Constructor
    FileAction(String name, String tooltip)
    {  
      super(name);  
      String iconFileName = "Images/"+ name + ".gif";
      if(new File(iconFileName).exists())
         putValue(SMALL_ICON, new ImageIcon(iconFileName));
      if(tooltip != null)                               // If there is tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it away

    }

    public void actionPerformed(ActionEvent e)
    {
     // We will add action code here eventually...
    }
   }

   // TYPE ACTION INNER CLASS
    class TypeAction extends AbstractAction
    {    
     TypeAction(String name, int typeID, String tooltip)
     {
       super(name);
       this.typeID = typeID;
       String iconFileName = "Images/"+ name + ".gif";
       if(new File(iconFileName).exists())
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
       if(tooltip != null)                                  // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);              // ...squirrel it away

     }
    
     public void actionPerformed(ActionEvent e)
     {
       elementType = typeID;  
     }

       private int typeID;
    }

  // COLOR ACTION INNER CLASS
    class ColorAction extends AbstractAction
    {
      public ColorAction(String name, Color color, String tooltip)
      {
         super(name);
         this.color = color;
         String iconFileName = "Images/"+ name + ".gif";
         if(new File(iconFileName).exists())
            putValue(SMALL_ICON, new ImageIcon(iconFileName));
         if(tooltip != null)                                  // If there is a tooltip
            putValue(SHORT_DESCRIPTION, tooltip);              // ...squirrel it away

      }

      public void actionPerformed(ActionEvent e)
      {
         elementColor = color;

         // This is temporary - just to show it works
         getContentPane().setBackground(color);
      }

      private Color color;
    }

    //Helper for adding toolbar buttons 
    private JButton addToolBarButton(Action action)
    {
      JButton button = toolBar.add(action);                     // Add toolbar button
      button.setToolTipText((String)action.getValue(action.SHORT_DESCRIPTION)); //for tooltips
      button.setBorder(BorderFactory.createRaisedBevelBorder()); // Add button border
      button.setText(null);                                      // No button text
      return button;
    }

    //Helper for removing menuitem icons
    private JMenuItem addMenuItem(JMenu menu, Action action, KeyStroke keystroke)
    {
      JMenuItem item = menu.add(action);                  // Add the menu item
      item.setIcon(null);                                 // Remove the icon
      if(keystroke != null)                               // If there is an accelerator
      item.setAccelerator(keystroke);                   // then add it...
      return item;                                        // Return the menu item
    }


  // We will add action objects as members here...

  FileAction newAction,  openAction,   closeAction,
             saveAction, saveAsAction, printAction;
  // Element type actions
  private TypeAction lineAction, rectangleAction, 
                     circleAction, curveAction;
  // Element color actions
  private ColorAction redAction,   yellowAction,     
                      greenAction, blueAction ;

  private JMenuBar menuBar = new JMenuBar();               // Window menu bar
  private JToolBar toolBar = new JToolBar();		   // Toolbar 
  private Color elementColor = DEFAULT_ELEMENT_COLOR;      // Current element color
  private int elementType = DEFAULT_ELEMENT_TYPE;          // Current element type
}

⌨️ 快捷键说明

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