我用setBackground()可以设置JButton类型的背景颜色,但是用getBackground()得不到JButton的背景颜色,为什么?JButton的按钮的背景颜色怎么得到?谢谢!

解决方案 »

  1.   

    setBackground(new Color(150,150,150))
    参数你设了几位?
      

  2.   

    能否解释一下各个参数的意思?
    以下是我的程序代码
    import java.awt.*;import javax.swing.*;import java.applet.*;
    public class b extends Applet
    {
    JButton b1,b2,b3;
    JFrame f;
    public void init()
    {
     f=new JFrame("按钮背景测试");
     f.setLayout(null);
     b1=new JButton("yes");b2=new JButton("no");b3=new JButton("KK");
     b1.setBounds(20,20,80,50);b2.setBounds(120,20,80,50);b3.setBounds(220,20,80,50);
     b1.setBackground(Color.red);b2.setBackground(Color.yellow);  b3.setBackground(getBackground(b1));  f.setSize(500,500);
     f.add(b1);f.add(b2);f.add(b3);
     f.setVisible(true);
    }
    }
      

  3.   

    解决了
    把b3.setBackground(getBackground(b1)); 改为b3.setBackground(b1.getBackground()); 就OK了
    呵呵,初学~~