Background.java 代码
import java.awt.*;
import java.awt.event.*;
public class Background
{
Frame f;
Label t1;
public void display()
{
f=new Frame("Welcome");
t1=new Label("HE PENG");
f.setSize(800,600);
f.setLocation(200,150);
f.setLayout(new FlowLayout(FlowLayout.LEFT));
Button b1=new Button("change color");
f.add(b1);
f.add(t1);
b1.addActionListener(this);
f.setVisible(true);
}
public void actiomPerformed(ActionEvent e)
{
t1.setBackground(Color.lightGray);
}
public static void main(String[] args)
{
(new Background()).display();
}
}
报错:addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied to (Background)
                b1.addActionListener(this);
我本是想弄个按纽,按一下可以改变窗口颜色和文字颜色,要求两种色加起来等于255,255,255。但是先调试一下都不能编译啊,下面都不知道怎么写了。

解决方案 »

  1.   

    主要是不知道怎么给颜色ffffff赋值
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    public class Background implements ActionEvent
    {
    Frame f;
    Label t1;
    public void display()
    {
    f=new Frame("Welcome");
    t1=new Label("HE PENG");
    f.setSize(800,600);
    f.setLocation(200,150);
    f.setLayout(new FlowLayout(FlowLayout.LEFT));
    Button b1=new Button("change color");
    f.add(b1);
    f.add(t1);
    b1.addActionListener(this);
    f.setVisible(true);
    }
               public void actionPerformed(ActionEvent e)
    //public void actiomPerformed(ActionEvent e)
    {
    t1.setBackground(Color.lightGray);
    }
    public static void main(String[] args)
    {
    (new Background()).display();
    }
    }
      

  3.   

    Button cannot be applied to (Background)这个已经给出了错误了!你需要另外一个容器去装这个button。
      

  4.   

    public class Background implements ActionEvent
    这个有问题,应改成 public class Background implements ActionListener
    还有,为什么这个//public void actiomPerformed(ActionEvent e)
    要注释啊?
      

  5.   

    我是这样写的,出错了
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    public class Background implements ActionListener
    {
    Frame f;
    Label t1;
    public void display()
    {
    f=new Frame("Welcome");
    t1=new Label("HE PENG");
    f.setSize(400,400);
    f.setLocation(200,150);
    f.setLayout(new FlowLayout(FlowLayout.CENTER));
    Button b1=new Button("change color");
    f.add(b1);
    f.add(t1);
    b1.addActionListener(this);
    f.setVisible(true);
    }
               public void actionPerformed(ActionEvent e)
    {
    Random number=new Random();
    int i=number.nextInt(255);
    int j=number.nextInt(255);
    int k=number.nextInt(255);
        f.setBackground(i,j,k);
        i=255-i;
        j=255-j;
        k=255-k;
    t1.setBackground(i,j,k);
    }
    public static void main(String[] args)
    {
    (new Background()).display();
    }
    }
    出错: setBackground(java.awt.Color) in java.awt.Component cannot be applied to (int,int,int)
    很明显,不过我不知道怎么赋值
      

  6.   

    先用 Color c=new Color(i,j,k);
    在用 xx.setBackground(c);