当前位置:编程学习 > JAVA >>

java中三个按钮共享一个监听器,但我的程序编译不了,高手帮我看看吧

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
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");
setBounds(100,100,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);
}
Color backgroundColor;
}
//Color backgroundColor;
}
*************/
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)
{
JButton bt=(JButton) event.getSource(); 
if(bt==yellowButton)
setBackground(Color.YELLOW);
else if(bt==blueButton)
setBackground(Color.BLUE);
else if(bt==redButton)
setBackground(Color.RED);

}
}
  java,监听器, --------------------编程问答-------------------- 格式复制过来,就不好看了,大家见谅呀 --------------------编程问答-------------------- --------------------编程问答-------------------- 建议楼主调好格式,去javase板块里发帖子 --------------------编程问答--------------------
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");
        setBounds(100, 100, 300, 200);
        ButtonPanel panel = new ButtonPanel();
        add(panel);
    }
}

class ButtonPanel extends JPanel implements ActionListener
{
    private String CMD_YELLOW = "yellow";
    private String CMD_BLUE = "blue";
    private String CMD_RED = "red";
    public ButtonPanel()
    {

        JButton yellowButton = new JButton(CMD_YELLOW);
        JButton blueButton = new JButton(CMD_BLUE);
        JButton redButton = new JButton(CMD_RED);

        add(yellowButton);
        add(blueButton);
        add(redButton);

        yellowButton.addActionListener(this);
        blueButton.addActionListener(this);
        redButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (CMD_YELLOW.equals(e.getActionCommand()))
        {
            setBackground(Color.YELLOW);            
        }
        else if (CMD_BLUE.equals(e.getActionCommand()))
        {
            setBackground(Color.BLUE);            
        }   else if (CMD_RED.equals(e.getActionCommand()))
            setBackground(Color.RED);

    }
}
补充:Java ,  非技术区
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,