/** @version 1.31 2002-07-10 @author Cay Horstmann */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxTest { public static void main(String[] args) { ComboBoxFrame frame = new ComboBoxFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } /** A frame with a sample text label and a combo box for selecting font faces. */ class ComboBoxFrame extends JFrame { public ComboBoxFrame() { setTitle("ComboBoxTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); Container contentPane = getContentPane(); // add the sample text label label = new JLabel( "The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); contentPane.add(label, BorderLayout.CENTER); // make a combo box and add face names faceCombo = new JComboBox(); faceCombo.setEditable(true); faceCombo.addItem("Serif"); faceCombo.addItem("SansSerif"); faceCombo.addItem("Monospaced"); faceCombo.addItem("Dialog"); faceCombo.addItem("DialogInput"); // the combo box listener changes the label font to the // selected face name faceCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { label.setFont(new Font( (String)faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE)); } }); // add combo box to a panel at the frame's southern border JPanel comboPanel = new JPanel(); comboPanel.add(faceCombo); contentPane.add(comboPanel, BorderLayout.SOUTH); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; private JComboBox faceCombo; private JLabel label; private static final int DEFAULT_SIZE = 12; }