//cd c:\JavaTry\MySwing\Try //javac ColorTest3.java //set CLASSPATH=. //appletviewer ColorTest3.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class ColorTest3 extends JApplet implements ActionListener { JPanel panel; Container contentPane; public void init(){ contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JButton yellow = new JButton("Yellow"); JButton blue = new JButton("Blue"); JButton red = new JButton("Red"); contentPane.add(yellow); contentPane.add(blue); contentPane.add(red); yellow.addActionListener(this); blue.addActionListener(this); red.addActionListener(this); // 削除 panel = new JPanel(); // 削除 contentPane.add(panel); } 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; } contentPane.setBackground(c); //panel.setBackground(c) に変えて } } /* 1 ColorTest10.java から、パネルの部分を削除してすっきりしたもの。 変更点をコメントしてある。 2 contentPane.setLayout(new FlowLayout())は、contentPane全体を FlowLayoutするものではない? */