コード例 #1
0
  /*
   * The following method creates the font menu item to choose a font
   *     style.
   * postcondition: returns the JMenuItem for the font menu item for
   *     the font style.
   */
  public JMenuItem createFontStyleItem(String style) {
    JMenuItem item = new JMenuItem(style);

    // This class is used to create a special ActionListener for this menu item
    class MenuItemListener implements ActionListener {
      private String style;

      public MenuItemListener(String s) {
        style = s;
      }

      public void actionPerformed(ActionEvent event) {
        // Set the size of the font
        if (style.equals("Plain")) fontStyle = Font.PLAIN;
        else if (style.equals("Italic")) fontStyle = Font.ITALIC;
        else if (style.equals("Bold")) fontStyle = Font.BOLD;
        else if (style.equals("Bold Italic")) fontStyle = Font.ITALIC | Font.BOLD;

        sampleField.setFont(new Font(fontName, fontStyle, fontSize));
        sampleField.repaint();

        pack();
      }
    }
    ActionListener listener = new MenuItemListener(style);
    item.addActionListener(listener);
    return item;
  } // end createFontStyleItem method
コード例 #2
0
  /*
   * The following method creates the font menu item to choose a font
   *     size.
   * postcondition: returns the JMenuItem for the font menu item for
   *     the font size.
   */
  public JMenuItem createFontSizeItem(String size) {
    JMenuItem item = new JMenuItem(size);

    // This class is used to create a special ActionListener for this menu item
    class MenuItemListener implements ActionListener {
      private String size;

      public MenuItemListener(String s) {
        size = s;
      }

      public void actionPerformed(ActionEvent event) {
        // Set the size of the font
        //                        if(size.equals("Small"))
        //                              fontSize = SMALL;
        //                        else if(size.equals("Medium"))
        //                              fontSize = MEDIUM;
        //                        else if(size.equals("Large"))
        //                              fontSize = LARGE;

        fontSize = Integer.parseInt(size);

        sampleField.setFont(new Font(fontName, fontStyle, fontSize));
        sampleField.repaint();

        pack();
      }
    }
    ActionListener listener = new MenuItemListener(size);
    item.addActionListener(listener);
    return item;
  } // end createFontSizeItem method
コード例 #3
0
 /*
  * The following method creates the file menu item to quit the program
  * postcondition: returns the JMenuItem for the file menu item "Exit".
  */
 public JMenuItem createFileExitItem() {
   JMenuItem item = new JMenuItem(new String("Exit"));
   class MenuItemListener implements ActionListener {
     public void actionPerformed(ActionEvent event) {
       System.exit(0);
     }
   }
   ActionListener listener = new MenuItemListener();
   item.addActionListener(listener);
   return item;
 } // end createFileExitItem method
コード例 #4
0
  /*
   * The following method creates the font menu item to choose a font
   *     name.
   * postcondition: returns the JMenuItem for the font menu item for
   *     the font name.
   */
  public JMenuItem createFontNameItem(String name) {
    JMenuItem item = new JMenuItem(name);

    // This class is used to create a special ActionListener for this menu item
    class MenuItemListener implements ActionListener {
      private String name;

      public MenuItemListener(String n) {
        name = n;
      }

      public void actionPerformed(ActionEvent event) {
        // Set the font of the text field
        fontName = name;
        sampleField.setFont(new Font(fontName, fontStyle, fontSize));
        sampleField.repaint();

        pack();
      }
    }
    ActionListener listener = new MenuItemListener(name);
    item.addActionListener(listener);
    return item;
  } // end createFontNameItem method