Vedi la versione AWT, Applet e Swing.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CambiaColore1 extends JApplet implements ActionListener { private Color[] cCOLORI={ Color.black , Color.blue , Color.cyan , Color.darkGray , Color.gray , Color.green , Color.lightGray, Color.magenta, Color.orange, Color.pink , Color.red , Color.white , Color.yellow }; private String[] sCOLORI={ "Nero" , "Blu" , "Azzurro" , "Grigio scuro" , "Grigio" , "Verde" , "Grigio chiaro", "Magenta" , "Arancione" , "Rosa", "Rosso", "Bianco" , "Giallo" }; private JPanel pannello; private JComboBox colori; private JButton cambia, cambia2; private Color sfondo; private Canvas tela; public void init() { colori=new JComboBox(); colori.setEditable(false); for(int i=0; i < sCOLORI.length; i++) colori.addItem(sCOLORI[i]); tela=new Canvas(); sfondo=cCOLORI[0]; tela.setBackground(sfondo); cambia =new JButton("Cambia"); cambia.addActionListener(this); cambia2=new JButton("Bianco"); cambia2.addActionListener(this); pannello=new JPanel(); pannello.add(new JLabel("Colore sfondo", Label.RIGHT)); pannello.add(colori); pannello.add(cambia); pannello.add(cambia2); this.getContentPane().add(pannello, BorderLayout.SOUTH); this.getContentPane().add(tela, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { String nome = e.getActionCommand(); if(nome.equals("Cambia")) { int scelta=colori.getSelectedIndex(); sfondo=cCOLORI[scelta]; } else if(nome.equals("Bianco")) sfondo=Color.white; tela.setBackground(sfondo); } }