//FontFrame.java //The Font chooser as a frame //manipulates a component remotely //set up with gridbaglayout //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class FontFrame extends Frame{ GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); Label lblOutput = new Label("Sample text"); EventHandler evt = new EventHandler(this); private Component theComponent; //size stuff CheckboxGroup cbgSize = new CheckboxGroup(); Checkbox cb10 = new Checkbox("10", cbgSize, true); Checkbox cb20 = new Checkbox("20", cbgSize, false); Checkbox cb30 = new Checkbox("30", cbgSize, false); Checkbox cb40 = new Checkbox("40", cbgSize, false); //Color stuff List lstColor = new List(5); //Font stuff Choice chcFont = new Choice(); Checkbox chkBold = new Checkbox("bold"); Checkbox chkItalic = new Checkbox("italic"); Button btnClose = new Button("Close Window"); public FontFrame(Component component){ super("Font Chooser"); //copy over the reference theComponent = component; //main layout setLayout(gbl); // comp X Y ht wd x% y% fill addComp(lblOutput, 0, 0, 4, 3, 100,50, GridBagConstraints.BOTH); addComp(cb10, 0, 4, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(cb20, 0, 5, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(cb30, 0, 6, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(cb40, 0, 7, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(lstColor, 1, 4, 4, 1, 33, 50, GridBagConstraints.BOTH); addComp(chcFont, 2, 4, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(chkBold, 2, 5, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(chkItalic, 2, 6, 1, 1, 33, 6, GridBagConstraints.NONE); addComp(btnClose, 2, 7, 1, 1, 33, 6, GridBagConstraints.NONE); //set up color list lstColor.add("black"); lstColor.add("blue"); lstColor.add("cyan"); lstColor.add("gray"); lstColor.add("green"); lstColor.add("magenta"); lstColor.add("orange"); lstColor.add("pink"); //set up font choicebox chcFont.add("SansSerif"); chcFont.add("Serif"); chcFont.add("Monospaced"); lblOutput.setAlignment(Label.CENTER); //add all the listeners cb10.addItemListener(evt); cb20.addItemListener(evt); cb30.addItemListener(evt); cb40.addItemListener(evt); lstColor.addItemListener(evt); chcFont.addItemListener(evt); chkBold.addItemListener(evt); chkItalic.addItemListener(evt); btnClose.addActionListener(evt); this.addWindowListener(evt); pack(); } // end constructor public void addComp (Component comp, int x, int y, int ht, int wd, int xPerc, int yPerc, int fill){ c.gridx = x; c.gridy = y; c.gridheight = ht; c.gridwidth = wd; c.weightx = xPerc; c.weighty = yPerc; c.fill = fill; gbl.setConstraints(comp, c); add(comp); } // end addComp private class EventHandler extends WindowAdapter implements ItemListener, ActionListener{ Frame theFrame; public EventHandler(Frame f){ super(); theFrame = f; } // end constructor public void itemStateChanged(ItemEvent e){ //create variables for font Font oldFont = lblOutput.getFont(); Font newFont = lblOutput.getFont(); String fontName = oldFont.getName(); int fontStyle = oldFont.getStyle(); int fontSize = oldFont.getSize(); //System.out.println(e.getItemSelectable()); if (e.getItemSelectable() == lstColor){ String theColor = lstColor.getSelectedItem(); if (theColor.equals("black")){ lblOutput.setForeground(Color.black); } else if (theColor.equals("blue")){ lblOutput.setForeground(Color.blue); } else if (theColor.equals("cyan")){ lblOutput.setForeground(Color.cyan); } else if (theColor.equals("gray")){ lblOutput.setForeground(Color.gray); } else if (theColor.equals("green")){ lblOutput.setForeground(Color.green); } else if (theColor.equals("magenta")){ lblOutput.setForeground(Color.magenta); } else if (theColor.equals("orange")){ lblOutput.setForeground(Color.orange); } else if (theColor.equals("pink")){ lblOutput.setForeground(Color.pink); } // end if } else if (e.getItemSelectable() == chcFont){ //The user wants to change the font face fontName = chcFont.getSelectedItem(); } else if (e.getItemSelectable() == cb10 || e.getItemSelectable() == cb20 || e.getItemSelectable() == cb30 || e.getItemSelectable() == cb40 ){ //handle size comparison String fontString = String.valueOf(e.getItem()); //convert to int fontSize = Integer.parseInt(fontString); } else if (e.getItemSelectable() == chkBold || e.getItemSelectable() == chkItalic){ fontStyle = Font.PLAIN; if (chkBold.getState() == true){ fontStyle += Font.BOLD; } // end 'bold' if if (chkItalic.getState() == true){ fontStyle += Font.ITALIC; } // end 'italic' if } // end "what item clicked' if //apply the new details newFont = new Font(fontName, fontStyle, fontSize); lblOutput.setFont(newFont); theComponent.setFont(newFont); theComponent.setForeground(lblOutput.getForeground()); } // end itemStateChanged public void actionPerformed(ActionEvent e){ //closing window is only event. theFrame.setVisible(false); theFrame.dispose(); } // end actionPerformed public void windowClosing(WindowEvent e){ theFrame.setVisible(false); theFrame.dispose(); } // end windowClosing } // end EventHandler } // end class def