import java.awt.event.*; import javax.swing.*; import java.awt.Font; import java.awt.Container; import java.awt.BorderLayout; public class AnonComboBoxApp extends JFrame { public static void main(String[] args) { JFrame cframe = new AnonComboBoxApp(); cframe.show(); } public AnonComboBoxApp() { setTitle("Ejemplo de ComboBox"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(XSIZE, YSIZE); AnonComboPanel panel= new AnonComboPanel(); Container contentPane= getContentPane(); contentPane.add(panel, "Center" ); } public static final int XSIZE= 600; public static final int YSIZE= 400; } class AnonComboPanel extends JPanel //implementa a ActionListener { public AnonComboPanel() { String[][] fontOptions= { {"Monospaced", "Serif", "SansSerif"}, {"NORMAL", "NEGRITA", "CURSIVA"}, {"10", "12", "14", "18", "24", "36"} }; setLayout( new BorderLayout() ); chFamily= new JComboBox(fontOptions[0]); chStyle= new JComboBox(fontOptions[1]); chSize= new JComboBox(fontOptions[2]); showFont= new JLabel(); showFont.setHorizontalAlignment( SwingConstants.CENTER ); showFont.setFont(new Font(curFamily, styleIndex(curStyle), curSize)); showFont.setText(curFamily + " " + curStyle + " " + curSize); JPanel comboPanel = new JPanel(); comboPanel.add(chFamily); comboPanel.add(chStyle); comboPanel.add(chSize); add( comboPanel, "North" ); add( showFont, "Center" ); //chFamily.addActionListener(this); chFamily.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { curFamily= (String) chFamily.getSelectedItem(); showFont.setFont( new Font( curFamily, styleIndex(curStyle), curSize) ); showFont.setText(curFamily + " " + curStyle + " " + curSize); } } ); //chStyle.addActionListener(this); chStyle.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { curStyle= (String) chStyle.getSelectedItem(); showFont.setFont( new Font( curFamily, styleIndex(curStyle), curSize) ); showFont.setText(curFamily + " " + curStyle + " " + curSize); } } ); //chSize.addActionListener(this); chSize.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { curSize= Integer.parseInt( (String) chSize.getSelectedItem()); showFont.setFont( new Font( curFamily, styleIndex(curStyle), curSize) ); showFont.setText(curFamily + " " + curStyle + " " + curSize); } } ); } private void update() { showFont.setFont( new Font( curFamily, styleIndex(curStyle), curSize) ); showFont.setText(curFamily + " " + curStyle + " " + curSize); } private String curFamily= "Monospaced"; private String curStyle= "NORMAL"; private int curSize= 10; private JLabel showFont; private JComboBox chFamily; private JComboBox chStyle; private JComboBox chSize; public int styleIndex(String s) { if (s.equals("NEGRITA")) return Font.BOLD; else if (s.equals("CURSIVA")) return Font.ITALIC; else return Font.PLAIN; } }