请大家帮我把这个程序用另一种方式改写一下: 就是我想直接在public class MyButtonFrame extends Frame后接入implements ActionListener,改写后程序依然如改写前一样运行,请帮我改写一下,谢谢! import java.awt.*; import java.awt.event.*;
public class MyButtonFrame extends Frame { MyButtonPanel panel=new MyButtonPanel(); public MyButtonFrame(String s) { setTitle(s); add(panel); }
public static void main(String[] args) { MyButtonFrame frm=new MyButtonFrame("测试按钮事件"); frm.setSize(500,300); frm.setVisible(true); } }
class MyButtonPanel extends Panel { public MyButtonPanel() { Button b=new Button("蓝色"); Button g=new Button("绿色"); Button e=new Button("退出"); add(b); add(g); add(e);
MyListenerAction bAction=new MyListenerAction(Color.blue); MyListenerAction gAction=new MyListenerAction(Color.green); MyListenerAction eAction=new MyListenerAction(Color.red);
b.addActionListener(bAction); g.addActionListener(gAction); e.addActionListener(eAction); } private class MyListenerAction implements ActionListener { private Color bgColor;
public MyListenerAction(Color c) { bgColor=c; }
public void actionPerformed(ActionEvent event) { setBackground(bgColor); repaint(); if(event.getActionCommand()=="退出") System.exit(0); } } }