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

📄 sketchframe.java

📁 非常好的java事例以及带源码事例的java2教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                      "Confirm Save As",
                                      JOptionPane.YES_NO_OPTION,
                                      JOptionPane.WARNING_MESSAGE))
            return;                            // No selected file
      saveSketch(file);
    }
  }

  // Write a sketch to outFile
  private void saveSketch(File outFile)
  {
    try
    {
      ObjectOutputStream out =  new ObjectOutputStream(
                                new BufferedOutputStream(
                                new FileOutputStream(outFile)));
      out.writeObject(theApp.getModel());        // Write the sketch to the stream
      out.flush();                               // Flush the stream
      out.close();                               // And close it
    }
    catch(IOException e)
    {
      JOptionPane.showMessageDialog(SketchFrame.this,
                                          "Error writing a sketch file.",
                                              "File Output Error",
                                              JOptionPane.ERROR_MESSAGE);
      return;                                      // Serious error - return
    }
    if(outFile != modelFile)                      // If we are saving to a new file
    {                                              // we must update the window
      modelFile = outFile;                         // Save file reference
      filename = modelFile.getName();              // Update the file name
      setTitle(frameTitle + modelFile.getPath());  // Change the window title
    }
    sketchChanged = false;                         // Set as unchanged
  }

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

  private JMenuBar menuBar = new JMenuBar();               // Window menu bar

  private Color elementColor = DEFAULT_ELEMENT_COLOR;      // Current element color
  private int elementType = DEFAULT_ELEMENT_TYPE;          // Current element type
  private Font font = DEFAULT_FONT;                        // Current font

  private JToolBar toolBar = new JToolBar();                  // Window toolbar
  StatusBar statusBar = new StatusBar();                      // Window status bar
  private JPopupMenu popup = new JPopupMenu("General");       // Window pop-up

  // Sundry menu items
  private JMenuItem aboutItem, fontItem;
  private FontDialog fontDlg;                     // The font dialog

  private JMenuItem customColorItem;

  private String frameTitle;                         // Frame title
  private String filename = DEFAULT_FILENAME;        // Current model file name
  private File modelFile;                            // File for the current sketch
  private boolean sketchChanged = false;          // Model changed flag
  private JFileChooser files;                                // File dialogs

  Sketcher theApp;

  // We will add inner classes defining action objects here...
  class FileAction extends AbstractAction
  {    
    FileAction(String name)
    {
      super(name);
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists())
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
    }

    FileAction(String name, String tooltip)
    {
      this(name);                                     // Call the other constructor
      if(tooltip != null)                             // If there is tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);         // ...squirrel it away
    }

    public void actionPerformed(ActionEvent e)
    {
      String name = (String)getValue(NAME);
      if(name.equals(saveAction.getValue(NAME)))
      {
        saveOperation();
      }
      else if(name.equals(saveAsAction.getValue(NAME)))
      {
        // Code to handle file Save As operation...
      }
      else if(name.equals(openAction.getValue(NAME)))
      {
        // Code to handle file Open operation...
      }
      else if(name.equals(newAction.getValue(NAME)))
      {
        // File to handle file New operation...
      }
      if(name.equals(printAction.getValue(NAME)))
      {
        // Code to handle Print operation..
      }
    }
  }

  class TypeAction extends AbstractAction
  {    
    TypeAction(String name, int typeID)
    {
      super(name);
      this.typeID = typeID;
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists())
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
    }

    TypeAction(String name, int typeID, String tooltip)
    {
      this(name, typeID);
      if(tooltip != null)                               // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it away
    }
    
    public void actionPerformed(ActionEvent e)
    {
      elementType = typeID;
      statusBar.setTypePane(typeID);
    }

    private int typeID;
  }

  // Handles color menu items
  class ColorAction  extends AbstractAction
  {
    public ColorAction(String name, Color color)
    {
      super(name);
      this.color = color;
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists())
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
    }

    public ColorAction(String name, Color color, String tooltip)
    {
      this(name, color);
      if(tooltip != null)                               // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it away
    }

    public void actionPerformed(ActionEvent e)
    {
      elementColor = color;
      statusBar.setColorPane(color);
    }

    private Color color;
  }

  public Color getElementColor()
  { 
    return elementColor; 
  }

  public int getElementType()
  { 
    return elementType; 
  }

  public Font getCurrentFont()
  {
    return font;
  }

  // Retrieve the pop-up menu
  public JPopupMenu getPopup()
  {
    return popup;
  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() == aboutItem)
    {
      // Create about dialog with the menu item as parent
      JOptionPane.showMessageDialog((Component)e.getSource(),       // Parent
                             "Sketcher Copyright Ivor Horton 1999", // Message
                             "About Sketcher",                      // Title
                                 JOptionPane.INFORMATION_MESSAGE);  // Message type
    }
    else if(e.getSource() == fontItem)
    { // Set the dialog window position
      Rectangle bounds = getBounds();
      fontDlg.setLocation(bounds.x + bounds.width/3, bounds.y + bounds.height/3);

      fontDlg.setVisible(true);            // Show the dialog
    }
    else if(e.getSource() == customColorItem)
    {
      Color color = JColorChooser.showDialog(this, "Select Custom Color",
                                                           elementColor);
      if(color != null)
      {
        elementColor = color;
        statusBar.setColorPane(color);
      }
    }
  }

  public void setCurrentFont(Font font)
  {
    this.font = font;
  }
}

⌨️ 快捷键说明

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