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

Source: Internet
Author: User

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 ();


 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.