3. The design of a graphical user interface with a menu program, using cascading menus to control the font and color of text, the results of the operation as shown in the following figure
Import Javax.swing.*;import Java.awt.event.*;import java.awt.*;
public class Menutest extends JFrame implements actionlistener{JMenuBar = new JMB ();
JMenu fontmenu = new JMenu ("Font (F)");
JMenu Helpmenu = new JMenu ("help");
JMenu stylemenu = new JMenu ("style");
JMenu colormenu = new JMenu ("color");
JMenuItem exitmenu = new JMenuItem ("Exit");
JMenuItem aboutmenu = new JMenuItem ("about");
JCheckBoxMenuItem Boldmenu = new JCheckBoxMenuItem ("Bold (B)");
JCheckBoxMenuItem Italicmenu = new JCheckBoxMenuItem ("Italic");
JMenuItem redmenu = new JMenuItem ("Red");
JMenuItem greenmenu = new JMenuItem ("green");
JMenuItem bluemenu = new JMenuItem ("Blue");
JTextArea Txtdemo = new JTextArea ("Sample text");
int bold,italic;
Public Menutest () {Setjmenubar (JMB);
Jmb.add (Fontmenu);
Jmb.add (Helpmenu);
Fontmenu.setmnemonic (Keyevent.vk_f);
Boldmenu.setmnemonic (Keyevent.vk_b);
Fontmenu.add (Stylemenu);
Fontmenu.add (Colormenu);
Fontmenu.addseparator ();
Fontmenu.add (Exitmenu); FontmeNu.add (Aboutmenu);
Stylemenu.add (Boldmenu);
Stylemenu.add (Italicmenu);
Colormenu.add (Redmenu);
Colormenu.add (Greenmenu);
Colormenu.add (Bluemenu);
Exitmenu.addactionlistener (this);
Aboutmenu.addactionlistener (this);
Boldmenu.addactionlistener (this);
Italicmenu.addactionlistener (this);
Redmenu.addactionlistener (this);
Greenmenu.addactionlistener (this);
Bluemenu.addactionlistener (this);
Getcontentpane (). Add (Txtdemo);
Settitle ("Menu control font and color");
SetSize (350,250);
SetVisible (TRUE);
Setdefaultcloseoperation (Jframe.exit_on_close);
public void actionperformed (ActionEvent e) {String cmd = E.getactioncommand ();
if (Cmd.equals ("Red")) Txtdemo.setforeground (color.red);
else if (cmd.equals ("green")) Txtdemo.setforeground (Color.green);
else if (cmd.equals ("Blue")) Txtdemo.setforeground (Color.Blue); else if (Cmd.equals ("bold")) Bold = Boldmenu.isselected ()?
Font.BOLD:Font.PLAIN; else if (cmd.equals ("italic")) Italic = Italicmenu. isselected ()?
Font.ITALIC:Font.PLAIN;
else if (Cmd.equals ("Exit")) System.exit (0);
Txtdemo.setfont (New Font ("Serif", Bold + italic,24));
public static void Main (string[] args) {menutest TM = new Menutest ();
}
}