import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String args[])
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300,200);
ButtonPanel panel=new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction redAction=new ColorAction(Color.red);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
} class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
     private Color backgroundColor;
}

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class ButtonTest
    {
    public static void main(String[] args)
    {
    ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class ButtonFrame extends JFrame
    {
    public ButtonFrame()
    {
    setTitle("ButtonTest");
    setSize(300, 200);
    ButtonPanel panel = new ButtonPanel();
    add(panel);
    }
    }class ButtonPanel extends JPanel implements ActionListener
    {
    public ButtonPanel()
    {
    JButton yellowButton = new JButton("yellow");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("red");
    add(yellowButton);
    add(blueButton);
    add(redButton);
    yellowButton.addActionListener(this);
    yellowButton.setActionCommand("yellow");
    blueButton.addActionListener(this);
    blueButton.setActionCommand("blue");
    redButton.addActionListener(this);
    redButton.setActionCommand("red");
    } public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("yellow"))
    setBackground(Color.yellow);
    else if(e.getActionCommand().equals("blue"))
    setBackground(Color.blue);
    else if(e.getActionCommand().equals("red"))
    setBackground(Color.red);
    }
    }
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ButtonTest
    {
    public static void main(String args[])
    {
    ButtonFrame frame=new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class ButtonFrame extends JFrame
    {
    public ButtonFrame()
    {
    setTitle("ButtonTest");
    setSize(300,200);
    ButtonPanel panel=new ButtonPanel();
    add(panel);
    }
    }
    class ButtonPanel extends JPanel implements ActionListener
    {
    public ButtonPanel()
    {
    JButton yellowButton=new JButton("yellow");
    JButton blueButton=new JButton("Blue");
    JButton redButton=new JButton("red");
    add(yellowButton);
    add(blueButton);
    add(redButton);
    yellowButton.addActionListener(this);
    blueButton.addActionListener(this);
    redButton.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event){
    //System.out.println("111");
    if(event.getActionCommand()=="yellow") {
    System.out.println("0000");
    setBackground(Color.yellow);
    }
    if(event.getActionCommand()=="Blue") {
    setBackground(Color.blue);
    }
    if(event.getActionCommand()=="red") {
    setBackground(Color.red);
    }
    }
    }