//cd c:\HPBasic\Toppage-sample3\MySwing //javac ColorLabel.java //set CLASSPATH=. //appletviewer ColorLabel.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class ColorLabel extends JApplet { public void init(){ Container contentPane = getContentPane(); ColorLbl label = new ColorLbl(); contentPane.add(label); } } class ColorLbl extends JLabel implements ActionListener { JButton yellowB; JButton blueB; JButton redB; public ColorLbl() { yellowB = new JButton("Yellow"); blueB = new JButton("Blue"); redB = new JButton("Red"); add(yellowB); add(blueB); add(redB); yellowB.addActionListener(this); blueB.addActionListener(this); redB.addActionListener(this); } public void actionPerformed(ActionEvent event) { Color c; Object source = event.getSource(); // String source = event.getActionCommand(); if (source == yellowB) { c=Color.yellow; } else if (source == blueB) { c=Color.blue; } else { c=Color.red; } setBackground(c); } } /* ColorPane.java ColorPanelにははりついたボタンだが、JLabelではだめ。 */