1

i would like to create a program that has menus. in those menus there are the font styles,the size and the color to use. my problem is what code should use to make it happen. i did not put the string because im having problems of which one to use. either string or JLabel.

here is my code so far:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.Font;

public class menuFonts extends JFrame {

    public menuFonts() {

        setTitle("Menu Fonts");
        setSize(300, 300);


        JMenuBar fonts = new JMenuBar();
        setJMenuBar(fonts);

       JMenu menu_Face = new JMenu("Face");
        JMenu  menu_Size = new JMenu("Size");
        JMenu  menu_Color = new JMenu("Color");
       fonts.add( menu_Face);
       fonts.add(menu_Size);
        fonts.add(menu_Color);


        JMenuItem menu_Face_arial = new JMenuItem("Arial", 'A');
        JMenuItem menu_Face_tahoma = new JMenuItem("Tahoma", 'T');
        JMenuItem menu_Face_verdana = new JMenuItem("Verdana", 'V');

        JMenuItem menu_Size_12 = new JMenuItem("12");
        JMenuItem menu_Size_14= new JMenuItem("14");
        JMenuItem menu_Size_16 = new JMenuItem("16");

        JMenuItem menu_Color_red = new JMenuItem("Red", 'R');
        JMenuItem menu_Color_blue = new JMenuItem("Blue", 'B');
        JMenuItem menu_Color_green = new JMenuItem("Green", 'G');


        menu_Face.add(menu_Face_arial);
        menu_Face.add(menu_Face_tahoma);
        menu_Face.add(menu_Face_verdana);

            fonts.add(menu_Size);
            menu_Size.add(menu_Size_12);
        menu_Size.add(menu_Size_14);
        menu_Size.add(menu_Size_16);

            fonts.add(menu_Color);
            menu_Color.add(menu_Color_red);
        menu_Color.add(menu_Color_blue);
        menu_Color.add(menu_Color_green);



    }
    public static void main(String[] args) {
        menuFonts me = new menuFonts();
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.setVisible(true);
    }
} 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"which one to use. either string or JLabel."* Use a `Font` in a `JList` or `JComboBox` with a suitable renderer. E.G. [combo box](http://stackoverflow.com/a/6965149/418556), [list](http://stackoverflow.com/a/13718134/418556) (though the list uses no renderer, it is the same principle as the combo to create one). – Andrew Thompson May 16 '15 at 06:59
  • 1
    BTW - Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. – Andrew Thompson May 16 '15 at 07:00
  • Oh.. & don't hard code font names like `JMenuItem menu_Face_arial = new JMenuItem("Arial", 'A');` the constant `Font.SANS_SERIF` might point to `Arial` on a Windows machine but `Helvetica` for OS X. – Andrew Thompson May 16 '15 at 07:03
  • JMenuItem menu_Face_arial = new JMenuItem("Arial", 'A'); are just menus. i think they dont affect the fonts unless i put an event to it – Aizen Minemki May 16 '15 at 07:19
  • Well.. duh! But it is the *intent* of the menu indicated by the text that is the problem. That menu specifies a font (`Arial`) that **will probably not be installed** on a non-Windows machine. – Andrew Thompson May 16 '15 at 07:22
  • What are you trying to achieve? – user1803551 May 16 '15 at 13:30

0 Answers0