您处于正确的轨道上,但您需要将事件绑定到按钮组件而不是鼠标的中断信号.
而不是MouseListener,改为theHandler
:
public class TheActionHandler implements ActionListener { @Override public void actionPerformed(final ActionEvent e) { if(e.getSource()==bred) { mousepanel.setBackground(Color.red); } else if(e.getSource()==bblue) { mousepanel.setBackground(Color.blue); } else if(e.getSource()==bgreen) { mousepanel.setBackground(Color.green); } } }