日期:2014-05-20  浏览次数:20793 次

JAVA基础题希望各位朋友帮个忙
package dome;
import javax.swing.*;
import java.awt.*;
import java.awt.Event;
public class Textpanel extends JPanel
{
public Textpanel()
{
JButton yellow=new JButton("yellow");
JButton blue=new JButton("blue");
JButton green=new JButton("green");
add(yellow);
add(blue);
add(green);
ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction greenAction=new ColorAction(Color.green);
yellow.addActionListener(yellowAction);
blue.addActionListener(blueAction);
green.addActionListener(greenAction);
}
private class ColorAction implements ActionListener
{
private Color temp;
public ColorAction(Color c)
{
temp=c;
}
public void actionPerformen(ActionEvent e)
{
setBackground(temp);
}
}

}
谁能告诉我 内部类的setBackground();是从哪里来的?
我实现ActionListener接口时候会报错这是为什么?

------解决方案--------------------
继承自Component的类一般都有setBackground()方法,你在查API文档时,有时候在当前类中没找到相应的方法,你就去它的基类中查找!
public void actionPerformen(ActionEvent e) //actionPerformen拼写错误。应该是actionPerformed

------解决方案--------------------
这是基本上所有的setBackguoud()方法:setBackground(Color) - 类 java.awt.Component.AccessibleAWTComponent 中的方法 
设置此对象的背景色。 
setBackground(Color) - 类 java.awt.Component 中的方法 
设置组件的背景色。 
setBackground(Color) - 类 java.awt.Graphics2D 中的方法 
设置 Graphics2D 上下文的背景色。 
setBackground(Color) - 类 java.awt.List.AccessibleAWTList.AccessibleAWTListChild 中的方法 
设置此对象的背景颜色。 
setBackground(Color) - 类 java.awt.MenuComponent.AccessibleAWTMenuComponent 中的方法 
设置此对象的背景色。 
setBackground(Color) - 类 java.awt.TextComponent 中的方法 
设置此文本组件的背景色。 
setBackground(Color) - 接口 javax.accessibility.AccessibleComponent 中的方法 
设置此对象的背景色。 
setBackground(Color) - 类 javax.swing.JComponent 中的方法 
设置此组件的背景色。 
setBackground(Color) - 类 javax.swing.JList.AccessibleJList.AccessibleJListChild 中的方法 
  
setBackground(Color) - 类 javax.swing.JTable.AccessibleJTable.AccessibleJTableCell 中的方法 
设置此对象的背景色。 
setBackground(Color) - 类 javax.swing.JTree.AccessibleJTree.AccessibleJTreeNode 中的方法 
设置此对象的背景色。 
setBackground(Color) - 类 javax.swing.table.DefaultTableCellRenderer 中的方法 
重写 JComponent.setBackground 将未选定单元格的背景色分配为指定颜色。 
setBackground(Color) - 类 javax.swing.table.JTableHeader.AccessibleJTableHeader.AccessibleJTableHeaderEntry 中的方法 
  
setBackground(Color) - 类 javax.swing.text.LabelView 中的方法 
设置视图的背景色。 
setBackground(MutableAttributeSet, Color) - 类 javax.swing.text.StyleConstants 中的静态方法 
设置背景色。 
setBackground(Color) - 类 javax.swing.tree.DefaultTreeCellRenderer 中的方法 
创建子类,以便将 ColorUIResource 映射为 null。


楼主写的setBackgroud()应该是component中的。
------解决方案--------------------
把import java.awt.Event; 改为import java.awt.event.*;

ActionListener这个类在java.awt.event这个包里。

调试技巧需要多多练习才能进步,lz加油!

------解决方案--------------------
没有把ActionListener 这个类import进来
------解决方案--------------------
没有把ActionListener 这个类import进来