Testing Menu Frame. - Java Swing

Java examples for Swing:Menu

Description

Testing Menu Frame.

Demo Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;

class MenuFrame extends JFrame {
  private final Color[] colorValues = { Color.BLACK, Color.BLUE, Color.RED,
      Color.GREEN };/*from w ww. j a  v a  2s  .co  m*/
  private final JRadioButtonMenuItem[] colorItems;
  private final JRadioButtonMenuItem[] fonts;
  private final JCheckBoxMenuItem[] styleItems;
  private final JLabel displayJLabel;
  private final ButtonGroup fontButtonGroup;
  private final ButtonGroup colorButtonGroup;
  private int style;

  public MenuFrame() {
    JMenu fileMenu = new JMenu("File"); // create file menu
    fileMenu.setMnemonic('F'); // set mnemonic to F

    JMenuItem aboutItem = new JMenuItem("About...");
    aboutItem.setMnemonic('A'); // set mnemonic to A
    fileMenu.add(aboutItem); // add about item to file menu
    aboutItem.addActionListener(e -> {
      JOptionPane.showMessageDialog(MenuFrame.this,
          "This is an example\nof using menus", "About",
          JOptionPane.PLAIN_MESSAGE);
    });

    JMenuItem exitItem = new JMenuItem("Exit"); // create exit item
    exitItem.setMnemonic('x');
    fileMenu.add(exitItem);
    exitItem.addActionListener(e -> System.exit(0));

    JMenuBar bar = new JMenuBar();
    setJMenuBar(bar);
    bar.add(fileMenu);

    JMenu formatMenu = new JMenu("Format"); // create format menu
    formatMenu.setMnemonic('r');
    String[] colors = { "Black", "Blue", "Red", "Green" };

    JMenu colorMenu = new JMenu("Color");
    colorMenu.setMnemonic('C');
    colorItems = new JRadioButtonMenuItem[colors.length];
    colorButtonGroup = new ButtonGroup();
    ItemHandler itemHandler = new ItemHandler();
    for (int count = 0; count < colors.length; count++) {
      colorItems[count] = new JRadioButtonMenuItem(colors[count]); // create
                                                                   // item
      colorMenu.add(colorItems[count]); // add item to color menu
      colorButtonGroup.add(colorItems[count]);
      colorItems[count].addActionListener(itemHandler);
    }

    colorItems[0].setSelected(true); // select first Color item

    formatMenu.add(colorMenu); // add color menu to format menu
    formatMenu.addSeparator(); // add separator in menu

    String[] fontNames = { "Serif", "Monospaced", "SansSerif" };
    JMenu fontMenu = new JMenu("Font"); // create font menu
    fontMenu.setMnemonic('n'); // set mnemonic to n

    fonts = new JRadioButtonMenuItem[fontNames.length];
    fontButtonGroup = new ButtonGroup(); // manages font names

    // create Font radio button menu items
    for (int count = 0; count < fonts.length; count++) {
      fonts[count] = new JRadioButtonMenuItem(fontNames[count]);
      fontMenu.add(fonts[count]); // add font to font menu
      fontButtonGroup.add(fonts[count]); // add to button group
      fonts[count].addActionListener(itemHandler); // add handler
    }

    fonts[0].setSelected(true);
    fontMenu.addSeparator();

    String[] styleNames = { "Bold", "Italic" };
    styleItems = new JCheckBoxMenuItem[styleNames.length];
    StyleHandler styleHandler = new StyleHandler();

    for (int count = 0; count < styleNames.length; count++) {
      styleItems[count] = new JCheckBoxMenuItem(styleNames[count]);
      fontMenu.add(styleItems[count]);
      styleItems[count].addItemListener(styleHandler);
    }

    formatMenu.add(fontMenu);
    bar.add(formatMenu);

    // set up label to display text
    displayJLabel = new JLabel("Sample Text", SwingConstants.CENTER);
    displayJLabel.setForeground(colorValues[0]);
    displayJLabel.setFont(new Font("Serif", Font.PLAIN, 72));

    getContentPane().setBackground(Color.CYAN);
    add(displayJLabel, BorderLayout.CENTER);
  }

  private class ItemHandler implements ActionListener {
    // process color and font selections
    @Override
    public void actionPerformed(ActionEvent event) {
      // process color selection
      for (int count = 0; count < colorItems.length; count++) {
        if (colorItems[count].isSelected()) {
          displayJLabel.setForeground(colorValues[count]);
          break;
        }
      }

      // process font selection
      for (int count = 0; count < fonts.length; count++) {
        if (event.getSource() == fonts[count]) {
          displayJLabel.setFont(new Font(fonts[count].getText(), style, 72));
        }
      }

      repaint();
    }
  }

  private class StyleHandler implements ItemListener {
    @Override
    public void itemStateChanged(ItemEvent e) {
      String name = displayJLabel.getFont().getName();
      Font font;

      if (styleItems[0].isSelected() && styleItems[1].isSelected())
        font = new Font(name, Font.BOLD + Font.ITALIC, 72);
      else if (styleItems[0].isSelected())
        font = new Font(name, Font.BOLD, 72);
      else if (styleItems[1].isSelected())
        font = new Font(name, Font.ITALIC, 72);
      else
        font = new Font(name, Font.PLAIN, 72);

      displayJLabel.setFont(font);
      repaint();
    }
  }
}

public class Main {
  public static void main(String[] args) {
    MenuFrame menuFrame = new MenuFrame();
    menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    menuFrame.setSize(500, 200);
    menuFrame.setVisible(true);
  }
}

Related Tutorials