(Solved) : Note Pad Application Running Great Except Go Edit Menu Try Change Font Font Style Nothing Q41399157 . . .

My note pad application is running great except for when i go tothe edit menu and try to change the font or font style nothingactually happens. what am i missing?

MyMenuFrame.java

import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.net.URL;import java.util.Scanner;import java.io.*;abstract class MyMenuFrame extends JFrame implements ActionListener { JRadioButtonMenuItem[] colorItems; // color menu items private JRadioButtonMenuItem[] fonts; // font menu items private JCheckBoxMenuItem[] styleItems; // font style menu items private JLabel displayLabel; // displays sample text private ButtonGroup fontButtonGroup; // manages font menu items ButtonGroup colorButtonGroup; // manages color menu items private final MenuItem close = null; private int style; // used to create style for font public static void openWebpage (String urlString) { try { Desktop.getDesktop().browse(new URL(urlString).toURI()); } catch (Exception e) {e.printStackTrace();} } // no-argument constructor set up GUI public MyMenuFrame() { super(“My Notepad”); TextArea textArea = new TextArea(“”, 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY); setSize(600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); textArea.setFont(new Font(“Times New Roman”, Font.PLAIN, 12)); getContentPane().setLayout(new BorderLayout()); getContentPane().add(textArea); JMenu fileMenu = new JMenu(“File”); // create file menu fileMenu.setMnemonic(‘F’); // set mnemonic to F // create open menu item JMenuItem openItem = new JMenuItem(“Open”); openItem.setMnemonic(‘o’); openItem.setAccelerator(KeyStroke.getKeyStroke(‘O’,InputEvent.CTRL_DOWN_MASK)); // set mnemonic to O fileMenu.add(openItem); // add about item to file menu openItem.addActionListener( new ActionListener() // anonymous inner class { // display message dialog when user selects About… @Override public void actionPerformed(ActionEvent event) { if (event.getSource() == openItem) { // open up a file chooser JFileChooser openItem = new JFileChooser(); // get the option that the user selected int option = openItem.showOpenDialog(openItem); // to open the file if (option == JFileChooser.APPROVE_OPTION) { // clear the TextArea before applying the file contents textArea.setText(“”); try { // create a scanner to read the file Scanner scan = new Scanner(new FileReader(openItem.getSelectedFile().getPath())); // while there’s still something to read while (scan.hasNext()) textArea.append(scan.nextLine() + “n”); // append the line to the TextArea } catch (Exception ex) { // catch any exceptions //write to the debug console System.out.println(ex.getMessage()); } } } } } ); // Save menu item JMenuItem saveItem = new JMenuItem(“Save”); saveItem.setMnemonic(‘s’); saveItem.setAccelerator(KeyStroke.getKeyStroke(‘S’,InputEvent.CTRL_DOWN_MASK)); fileMenu.add(saveItem); saveItem.addActionListener( new ActionListener() { @Override public void actionPerformed (ActionEvent event){ if (event.getSource() == saveItem) { //open a file chooser JFileChooser saveItem = new JFileChooser(); int option = saveItem.showSaveDialog(saveItem); // showSaveDialog // if the user clicked OK if (option == JFileChooser.APPROVE_OPTION) { try { // create a buffered writer to write to a file BufferedWriter out = new BufferedWriter(new FileWriter(saveItem.getSelectedFile().getPath())); // write the contents of the TextArea to the file out.write(textArea.getText()); // close the file stream out.close(); } catch (Exception ex) { //catch any exceptions //write to the debug console System.out.println(ex.getMessage()); } } } } } ); JMenuItem exitItem = new JMenuItem(“Exit”); // create exit item exitItem.setMnemonic(‘x’); // set mnemonic to x exitItem.setAccelerator(KeyStroke.getKeyStroke(‘X’,InputEvent.CTRL_DOWN_MASK)); // keyboard shortcut // add it to the “File” menu fileMenu.add(exitItem); // add exit item to file menu exitItem.addActionListener( new ActionListener() // anonymous inner class { // terminate application when user clicks exitItem @Override public void actionPerformed(ActionEvent e) { // if the source of the event was our “close” option if (e.getSource() == exitItem) dispose(); // dispose all resources and close the application } } ); JMenuBar bar = new JMenuBar(); // create menu bar setJMenuBar(bar); // add menu bar to application bar.add(fileMenu); // add file menu to menu bar JMenu editMenu = new JMenu(“Edit”); // create edit menu editMenu.setMnemonic(‘D’); // set mnemonic to r JMenu colorMenu = new JMenu(“Color”); // create color menu colorMenu.setMnemonic(‘C’); // set mnemonic to C editMenu.add(colorMenu); JMenuItem colorChange = new JMenuItem(“Change Color”); colorChange.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SelectColorActionPerformed(e); } }); colorMenu.add(colorChange); colorChange.setAccelerator(KeyStroke.getKeyStroke(‘C’,InputEvent.CTRL_DOWN_MASK)); ItemHandler itemHandler = new ItemHandler(); // create radio button menu items for color // array listing font names String[] fontName = {“Times New Roman”, “Arial”, “Serif”}; JMenu fontMenu = new JMenu(“Font”); // create font menu fontMenu.setMnemonic(‘F’); // set mnemonic to n // create radio button menu items for font names fonts = new JRadioButtonMenuItem[fontName.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(fontName[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); // select first Font menu item fontMenu.addSeparator(); // add separator bar to font menu String[] styleNames = {“Bold”, “Italic”}; // names of styles styleItems = new JCheckBoxMenuItem[styleNames.length]; StyleHandler styleHandler = new StyleHandler(); // style handler // create style checkbox menu items for (int count = 0; count < styleNames.length; count++) { styleItems[count] = new JCheckBoxMenuItem(styleNames[count]); // for style fontMenu.add(styleItems[count]); // add to font menu styleItems[count].addItemListener(styleHandler); // handler } editMenu.add(fontMenu); // add Font menu to Edit menu bar.add(editMenu); JMenu printMenu=new JMenu(“Print”); printMenu.setMnemonic(‘p’); bar.add(printMenu); JMenuItem printItem = new JMenuItem(“Send to Printer”); printMenu.add(printItem); printItem.setAccelerator(KeyStroke.getKeyStroke(‘P’,InputEvent.CTRL_DOWN_MASK)); printItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {// print window dialog JOptionPane.showMessageDialog(MyMenuFrame.this, “Your File has Successfully Printed”, “Print”, JOptionPane.PLAIN_MESSAGE); } }); JMenu helpMenu = new JMenu(“Help”); JMenuItem aboutItem = new JMenuItem(“About”); JMenuItem homeItem = new JMenuItem(“Visit Homepage”); helpMenu.add(aboutItem); helpMenu.add(homeItem); bar.add(helpMenu); aboutItem.setAccelerator(KeyStroke.getKeyStroke(‘A’,InputEvent.CTRL_DOWN_MASK)); homeItem.setAccelerator(KeyStroke.getKeyStroke(‘V’,InputEvent.CTRL_DOWN_MASK)); aboutItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {// About display message JOptionPane.showMessageDialog(MyMenuFrame.this, “This software was developed in 2019 Version is 1.0”, “About”, JOptionPane.PLAIN_MESSAGE); } }); homeItem.addActionListener(new ActionListener() {// URL for helpmenuitem @Override public void actionPerformed(ActionEvent e) { openWebpage(“http://www.microsoft.com”); } }); getContentPane().setBackground(Color.WHITE); // set background } // end MenuFrame constructor private void SelectColorActionPerformed(ActionEvent e){ Color color=JColorChooser.showDialog(this,”Select Color”,Color.RED); getContentPane().setForeground(color); } // inner class to handle action events from menu items private class ItemHandler extends Component implements ActionListener { // process color and font selections @Override public void actionPerformed(ActionEvent event) {displayLabel=new JLabel(); // process font selection for (int count = 0; count < fonts.length; count++) { if (event.getSource() == fonts[count]) { displayLabel.setFont(new Font(fonts[count].getText(), style, 12)); } } repaint(); // redraw application } } // end class ItemHandler // inner class to handle item events from checkbox menu items private class StyleHandler implements ItemListener { // process font style selections @Override public void itemStateChanged(ItemEvent e) { String name = displayLabel.getFont().getName(); // current Font Font font; // new font based on user selections // determine which CheckBoxes are checked and create Font if (styleItems[0].isSelected() && styleItems[1].isSelected()) font = new Font(name, Font.BOLD + Font.ITALIC, 12); else if (styleItems[0].isSelected()) font = new Font(name, Font.BOLD, 12); else if (styleItems[1].isSelected()) font = new Font(name, Font.ITALIC, 12); else font = new Font(name, Font.PLAIN, 12); displayLabel.setFont(font); repaint(); // redraw application } } // end class StyleHandler }// end class MyMenuFrame

MyMenuFrameTest.java

import javax.swing.*;import java.awt.event.ActionEvent;public class MyMenuFrameTest{ public static void main(String[] args) { MyMenuFrame menuFrame = new MyMenuFrame(){ @Override public void actionPerformed(ActionEvent e) { } }; menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); menuFrame.setSize(600, 400); menuFrame.setVisible(true); }}

Expert Answer


Answer to My note pad application is running great except for when i go to the edit menu and try to change the font or font style …