FileDocCategorySizeDatePackage
FontSelection.javaAPI DocExample5475Tue Dec 12 19:00:08 GMT 2000None

FontSelection

public class FontSelection extends JApplet implements ItemListener

Fields Summary
JLabel
fontLabel
JLabel
sizeLabel
JLabel
styleLabel
FontPanel
fontC
JComboBox
fonts
JComboBox
sizes
JComboBox
styles
int
index
String
fontchoice
int
stChoice
String
siChoice
Constructors Summary
Methods Summary
public voidinit()


       

        getContentPane().setLayout( new BorderLayout() );

        JPanel topPanel = new JPanel();
        JPanel fontPanel = new JPanel();
        JPanel sizePanel = new JPanel();
        JPanel stylePanel = new JPanel();
        JPanel sizeAndStylePanel = new JPanel();

        topPanel.setLayout( new BorderLayout() );
        fontPanel.setLayout( new GridLayout( 2, 1 ) );
        sizePanel.setLayout( new GridLayout( 2, 1 ) );
        stylePanel.setLayout( new GridLayout( 2, 1 ) );
        sizeAndStylePanel.setLayout( new BorderLayout() );

        topPanel.add( BorderLayout.WEST, fontPanel );
        sizeAndStylePanel.add( BorderLayout.WEST, sizePanel );
        sizeAndStylePanel.add( BorderLayout.CENTER, stylePanel );
        topPanel.add( BorderLayout.CENTER, sizeAndStylePanel );

        getContentPane().add( BorderLayout.NORTH, topPanel );

        fontLabel = new JLabel();
        fontLabel.setText("Fonts");
        Font newFont = getFont().deriveFont(1);
        fontLabel.setFont(newFont);
        fontLabel.setHorizontalAlignment(JLabel.CENTER);
        fontPanel.add(fontLabel);

        sizeLabel = new JLabel();
        sizeLabel.setText("Sizes");
        sizeLabel.setFont(newFont);
        sizeLabel.setHorizontalAlignment(JLabel.CENTER);
        sizePanel.add(sizeLabel);

        styleLabel = new JLabel();
        styleLabel.setText("Styles");
        styleLabel.setFont(newFont);
        styleLabel.setHorizontalAlignment(JLabel.CENTER);
        stylePanel.add(styleLabel);

        GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String envfonts[] = gEnv.getAvailableFontFamilyNames();
        Vector vector = new Vector();
        for ( int i = 1; i < envfonts.length; i++ ) {
            vector.addElement(envfonts[i]);
        }
        fonts = new JComboBox( vector );
        fonts.setMaximumRowCount( 9 );
        fonts.addItemListener(this);
        fontchoice = envfonts[0];
        fontPanel.add(fonts);

        sizes = new JComboBox( new Object[]{ "10", "12", "14", "16", "18"} );
        sizes.setMaximumRowCount( 9 );
        sizes.addItemListener(this);
        sizePanel.add(sizes);

        styles = new JComboBox( new Object[]{
                                "PLAIN",
                                "BOLD",
                                "ITALIC",
                                "BOLD & ITALIC"} );
        styles.setMaximumRowCount( 9 );
        styles.addItemListener(this);
        sizes.setMaximumRowCount( 9 );
        stylePanel.add(styles);

        fontC = new FontPanel();
        fontC.setBackground(Color.white);
        getContentPane().add( BorderLayout.CENTER, fontC);
    
public voiditemStateChanged(java.awt.event.ItemEvent e)

        if ( e.getStateChange() != ItemEvent.SELECTED ) {
            return;
        }

        Object list = e.getSource();

        if ( list == fonts ) {
            fontchoice = (String)fonts.getSelectedItem();
        } else if ( list == styles ) {
            index = styles.getSelectedIndex();
            stChoice = index;
        } else {
            siChoice = (String)sizes.getSelectedItem();
        }
        fontC.changeFont(fontchoice, stChoice, siChoice);
    
public static voidmain(java.lang.String[] s)

        JFrame f = new JFrame("FontSelection");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet fontSelection = new FontSelection();
        f.getContentPane().add(fontSelection, BorderLayout.CENTER);
        fontSelection.init();
        f.setSize(new Dimension(550,250));
        f.setVisible(true);