Click here to Skip to main content
15,851,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// Import Core Java packages
import java.awt.*;
import java.awt.event.*;

public class Welcome extends Frame {
 
	// Constants and variables used in the program
	static final int WIDTH = 500;                // frame width
	static final int HEIGHT = 450;               // frame height
	static final int SMALL_SIZE = 16;            // small font size
	static final int MEDIUM_SIZE = 20;           // medium font size
	static final int LARGE_SIZE = 24;            // large font size
	static final String COLOR_NAMES[] = {"Red", "Blue", "Green"};
	static final Color COLORS[] = {Color.RED, Color.BLUE, Color.GREEN};
	
	static final String INITIAL_FACE = "TimesRoman";    // initial typeface
	static final int INITIAL_STYLE = Font.PLAIN;         // initial type style
	static final int INITIAL_SIZE = LARGE_SIZE;          // initial type size
	
	String typeFace = INITIAL_FACE;     // current typeface
	int typeStyle = INITIAL_STYLE;      // current type style
	int typeSize = INITIAL_SIZE;        // current type size
	
	Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
	Font Courier = new Font ("Courier", typeStyle, typeSize);
	Font Dialog = new Font ("Dialog", typeStyle, typeSize);
	
	Label text;                            // display text
	Checkbox small;                        // checkbox for small font
	Checkbox medium;                       // checkbox for medium font
	Checkbox large;                        // checkbox for large font
	Checkbox plain;
	Checkbox bold;
	Checkbox italic;
	Choice colorChoice;                    // choice box for text color
	Choice faceFont;
	Button resetButton;                    // button to reset font and color
	Button exitButton;
    /**
     * Constructor
     */
	public Welcome() {
	    setTitle("Welcome Java Program");             // set frame title

        // create text
	    text = new Label("Welcome to the World of Java");
        text.setAlignment(Label.CENTER);
	    text.setFont(new Font(typeFace, typeStyle, typeSize));
	    text.setForeground(COLORS[0]);
	    add(text, BorderLayout.CENTER);

	    Panel controlPanel = new Panel();
	    controlPanel.setLayout(new GridLayout(0, 1));
	    add(controlPanel, BorderLayout.SOUTH);

        // create choice box
        Panel panel = new Panel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        controlPanel.add(panel);
        Label label = new Label("Select a text color");
        panel.add(label);
        colorChoice = new Choice();
        colorChoice.add(COLOR_NAMES[0]);
        colorChoice.add(COLOR_NAMES[1]);
        colorChoice.add(COLOR_NAMES[2]);
        panel.add(colorChoice);
        colorChoice.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
	            text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
            }
        });
        
        Label label1 = new Label("Select a Font style");
        panel.add(label1);
        faceFont = new Choice();
        faceFont.add(COLOR_NAMES[0]);
        faceFont.add(COLOR_NAMES[1]);
        faceFont.add(TimesRoman);
        panel.add(faceFont);
        faceFont.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
	            text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
            }
        });

        // create radio buttons
	    panel = new Panel();
	    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
	    controlPanel.add(panel);
	    label1 = new Label("Select a type size");
	    panel.add(label1);
	    CheckboxGroup sizeGroup = new CheckboxGroup();
	    small = new Checkbox("Small", sizeGroup, false);
	    panel.add(small);
	    small.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(small.getState()) {
	                typeSize = SMALL_SIZE;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });
	    medium = new Checkbox("Medium", sizeGroup, true);
	    panel.add(medium);
	    medium.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(medium.getState()) {
	                typeSize = MEDIUM_SIZE;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });
	    large = new Checkbox("Large", sizeGroup, false);
	    panel.add(large);
	    large.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(large.getState()) {
	                typeSize = LARGE_SIZE;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });
	   
	    
	    panel = new Panel();
	    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
	    controlPanel.add(panel);
	    label1 = new Label("Select a type style");
	    panel.add(label1);
	    CheckboxGroup styleGroup = new CheckboxGroup();
	    italic = new Checkbox("Italic", styleGroup, false);
	    panel.add(italic);
	    italic.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(italic.getState()) {
	                typeStyle = Font.ITALIC ;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });
	    bold = new Checkbox("Bold", styleGroup, false);
	    panel.add(bold);
	    bold.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(bold.getState()) {
	                typeStyle = Font.BOLD ;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });
	    plain = new Checkbox("Plain", styleGroup, true);
	    panel.add(plain);
	    plain.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent event) {
	            if(plain.getState()) {
	                typeStyle = Font.PLAIN ;
	                fontChange(typeFace, typeStyle, typeSize);
	            }
	        }
	    });


        // create reset button
        panel = new Panel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        controlPanel.add(panel);
		resetButton = new Button("Reset");
		panel.add(resetButton);
		resetButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        large.setState(true);          // reset radio button
		        colorChoice.select(0);         // reset color choice box
	            text.setForeground(COLORS[0]);  // reset text color
	            typeStyle = INITIAL_STYLE;
	            typeSize = INITIAL_SIZE;       // reset type size
	            fontChange(typeFace, typeStyle, typeSize);      // reset text font
	        }
	    });
		
		
		exitButton = new Button("Exit");
		panel.add(exitButton);
		exitButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        System.exit(0);
	        }
	    });
		
	} // end of constructor

    /**
     *  Method to change the text font
     */
    private void fontChange(String typeFace, int typeStyle, int typeSize) {
        Font font = new Font(typeFace, typeStyle, typeSize);
	    text.setFont(font);
    }

    /**
     * the main method
     */
    public static void main(String[] argv) {
        // Create frame
        Welcome welcome = new Welcome();
	    welcome.setBackground(Color.WHITE);
        welcome.setSize(WIDTH, HEIGHT);
        welcome.setLocation(150, 100);

        // add window closing listener
        welcome.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                System.exit(0);
            }
        });

        // Show the frame
        welcome.setVisible(true);
    }
}


There is my code.

What I need to do is make 3 types of Font ( TimesRoman, Courier, and Dialog)
Choices in the Choice Boxes.

Not sure how i go about doing that, something has to be done here:

Label label1 = new Label("Select a Font style");
       panel.add(label1);
       faceFont = new Choice();
       faceFont.add(COLOR_NAMES[0]);
       faceFont.add(COLOR_NAMES[1]);
       faceFont.add(TimesRoman);
       panel.add(faceFont);
       faceFont.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent event) {
               text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
           }
       });


Any help is appreciated.
Posted

1 solution

You probably need to use a JList[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900