// // another example of the Java AWT, showing use of Choice and Font // classes. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; // // a ChooseFont object is a CloseableFrame containing three Choice // objects used to select a font name (from those provided by // the default toolkit), a font style (plain, bold, or italic), // and a font size, plus two Label objects. when a selection is // made, the three Choice objects are used to generate a Font, // which is then used to set the Font for the two Label objects. // class ChooseFont extends CloseableFrame implements ItemListener { Choice fontName ; Choice fontStyle ; Choice fontSize ; Label display1 ; Label display2 ; ChooseFont(String text) { super("ChooseFont") ; setLayout(new BorderLayout()) ; // make two Label objects and group vertically. Panel dPanel = new Panel() ; dPanel.setLayout(new GridLayout(0, 1)) ; add(dPanel, "Center") ; display1 = new Label(text.toUpperCase(), Label.CENTER) ; display2 = new Label(text.toLowerCase(), Label.CENTER) ; dPanel.add(display1) ; dPanel.add(display2) ; // group three Choice objects horizontally. Panel cPanel = new Panel() ; cPanel.setLayout(new FlowLayout()) ; add(cPanel, "North") ; // make Choice object for font name -- get names // provided by this frame's toolkit. fontName = new Choice() ; String[] names = getToolkit().getFontList() ; for (int i = 0 ; i < names.length ; i++) fontName.add(names[i]) ; fontName.addItemListener(this) ; cPanel.add(fontName) ; // make Choice object for font style. fontStyle = new Choice() ; fontStyle.add("PLAIN") ; fontStyle.add("BOLD") ; fontStyle.add("ITALIC") ; fontStyle.addItemListener(this) ; cPanel.add(fontStyle) ; // make Choice object for font size. fontSize = new Choice() ; for (int i = 6 ; i < 20 ; i+=2) fontSize.add("" + i) ; fontSize.addItemListener(this) ; cPanel.add(fontSize) ; pack() ; show() ; } // invoked when user makes a selection. public void itemStateChanged(ItemEvent e) { // get font name. String name = fontName.getSelectedItem() ; // get font style and convert to one of Font class's // constants. String styleTxt = fontStyle.getSelectedItem() ; int style = 0 ; if (styleTxt.equals("PLAIN")) style = Font.PLAIN ; else if (styleTxt.equals("BOLD")) style = Font.BOLD ; else if (styleTxt.equals("ITALIC")) style = Font.ITALIC ; // get font size and convert to int. int size = Integer.parseInt( fontSize.getSelectedItem()) ; // set font for Label objects. Font f = new Font(name, style, size) ; display1.setFont(f) ; display2.setFont(f) ; } // // ---- main --------------------------------------------------- // // command-line argument (optional) is string to display. // public static void main(String[] args) { String text = "Hello!" ; if (args.length > 0) text = args[0] ; ChooseFont c = new ChooseFont(text) ; } }