//cd c:\JavaTry\MySwing //javac ColorTest4.java //set CLASSPATH=. //appletviewer ColorTest4.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class ColorTest4 extends JApplet implements ActionListener { JPanel panel; public void init(){ Container contentPane = getContentPane(); panel = new JPanel(); JButton yellow = new JButton("Yellow"); JButton blue = new JButton("Blue"); JButton red = new JButton("Red"); panel.add(yellow); panel.add(blue); panel.add(red); contentPane.add(panel); yellow.addActionListener(this); blue.addActionListener(this); red.addActionListener(this); } public void actionPerformed(ActionEvent event) { Color c; String source = event.getActionCommand(); if (source == "Yellow") { c=Color.yellow; } else if (source == "Blue") { c=Color.blue; } else { c=Color.red; } panel.setBackground(c); } } /* 1 panel.add(yellow)がきいて、OK(core Java 449)。 2 setBackground(c);とすると、色が変わるのは一瞬。 3 contentPane.setBackground(c)もだめ。 */