snowbird2005
米兰大脑
发贴: 72
积分: 10
|
于 2005-06-12 21:03
试试以下代码: import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class GetColorFromButton extends Applet implements ActionListener { private Button button1,button2,button3; public void init() { button1=new Button("red"); button1.setBackground(Color.red); button1.addActionListener(this); button2=new Button("yellow"); button2.setBackground(Color.yellow); button2.addActionListener(this); button3=new Button("blue"); button3.setBackground(Color.blue); button3.addActionListener(this); add(button1); add(button2); add(button3); }
public void actionPerformed(ActionEvent e) { Color color=((Button)e.getSource()).getBackground(); this.setForeground(color); repaint(); } public void paint(Graphics g) { g.drawLine(100,100,200,200); }
}
|