//cd c:\JavaTry\MySwing //javac ColorPane2.java //set CLASSPATH=. //appletviewer ColorPane2.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class ColorPane2 extends JApplet { public void init(){ Container contentPane = getContentPane(); ColorPanel2 panel = new ColorPanel2(); contentPane.add(panel); } } class ColorPanel2 extends JPanel { JButton yellowB; JButton blueB; JButton redB; public ColorPanel2() { yellowB = new JButton("Yellow"); blueB = new JButton("Blue"); redB = new JButton("Red"); add(yellowB); add(blueB); add(redB); yellowB.addActionListener(new Response()); blueB.addActionListener(new Response()); redB.addActionListener(new Response()); } class Response implements ActionListener { public void actionPerformed(ActionEvent event) { Color c; Object source = event.getSource(); if (source == yellowB) { c=Color.yellow; } else if (source == blueB) { c=Color.blue; } else { c=Color.red; } setBackground(c); } } } /* yellowB.addActionListener(this) などを yellowB.addActionListener(new Response()) にお置き換える。 */