帮帮小弟,一个简单问题(10分)

  • 主题发起人 主题发起人 feiren
  • 开始时间 开始时间
F

feiren

Unregistered / Unconfirmed
GUEST, unregistred user!
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");

//add buttons to panel

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

//create button actions

ColorAction yellowAction = new ColorAction(Color.yellow);

ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);



//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}



private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}

public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}

private Color backgroundColor;

}
}
错误提示:ActionEventDemo.java:62: ButtonPanel.ColorAction is not abstract anddo
es not override abstract m
ethod actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
private class ColorAction implements ActionListener
1 error
 
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}
这个可能不对,参数吧。
说你没有实现actionPerformed()。
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");
//add buttons to panel
add(yellowButton);
add(blueButton);
add(redButton);
//create button actions
ColorAction yellowAction = new ColorAction(Color.yellow);
ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);

//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}

private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}
private Color backgroundColor;
}
}
 
to ZRWeng
包我已经引入了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
你能不能指点一下错在哪里。谢谢
 
以上我给出的是已经编译通过的,你的错误应该不是在你所给出的代码
 
to ZRWeng
完整程序如下,忙烦帮我看一下,应该是很简单的。(我初学java)谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionEventDemo
{
public static void main(String[] args)
{
Frame1 fram=new Frame1();
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.show();
}
}

class Frame1 extends JFrame
{
public Frame1()
{
setTitle("buttontest");
setSize(width,height);

ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int width=300;
public static final int height=200;

}

class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");

//add buttons to panel

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

//create button actions

ColorAction yellowAction = new ColorAction(Color.yellow);

ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);



//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}



private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}

public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}

private Color backgroundColor;

}
}
 
你的代码没错呀,测试过了,可以运行的,环境JDK1.4.1
你给出的错误有些奇怪,不能覆盖接口ActionListener的抽象方法actionPerformed?
 
真的是奇怪了,我的环境是j2sdk1.4.2_03。
非常感谢ZRWeng兄的帮助,
 
接受答案了.
 
后退
顶部