import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
    public ButtonTest() {
        setTitle("wo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTest();
        setVisible(true);
    }
    public void setTest() {
        setSize(300, 200);
        makeB("Y",Color.YELLOW);
    }
    public void makeB(String n, final Color c) {
        JButton a = new JButton(n);
        add(a);
        a.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setBackground(c);
            }
        });
      
    }
    public static void main(String[] args) {
        new ButtonTest();
    }
}请问我这个设置出来的背景怎么颜色不对呢? 

解决方案 »

  1.   

    楼主你好:
        我现在也学到这了这个地方比较迷惑!你加我的qq群咱们交流!                  群号:74552467;           衷心的欢迎各位学习java的朋友加入!
      

  2.   

    你试试看先调用一下 setOpaque(true);
      

  3.   


    setBackground(c);
    改为
    ((JPanel)getContentPane()).setOpaque(false);
    getRootPane().setBackground(c);
      

  4.   

    直接设置JFrame的背景颜色不好整,还是弄一个JLabe了吧
      

  5.   

    JButton添加的BorderLayout.CENTER。背景被遮住了。你改变大小时,会看到一闪而过的黄色。add(a,BorderLayout.NORTH);
    要么
    getContentPane().setBackground(c);
    要么
    ((JPanel)getContentPane()).setOpaque(false);
    getRootPane().setBackground(c);
      

  6.   


    支持 
    改下代码,就可以看到了: public void makeB(String n, final Color c) { 
            JButton a = new JButton(n); 
            add(a,BorderLayout.NORTH);
            a.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) { 
                 getContentPane().setBackground(c);
                } 
            }); 
          
        } 
      

  7.   

    谢谢 改变按钮位置可以  
    但是那个设置JPanel为opaque的方法还是没用 
    这个是什么原因呢?
      

  8.   

    不知道你到底是想改变背景颜色还是按钮的颜色,按钮的话就直接setBackgroundJFrame就添加JPanel
      

  9.   

    想要JButton半透明,需要自己控制绘制过程。
    class TranslucentButton extends JButton {
        public TranslucentButton(){super()};
        public TranslucentButton(String label) {super(label);}
        public TranslucentButton(Icon icon){super(icon);}
        public TranslucentButton(Action action){super(action);}
        public TranslucentButton(String label,Icon icon){super(label,icon);}    @Override
        public void paintComponent(Graphics g) {
            BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);        Graphics gButton = image.getGraphics();
            gButton.setClip(g.getClip());        // Have the superclass render the button for us
            super.paintComponent(gButton);        Graphics2D g2d = (Graphics2D) g;
            AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f);
            g2d.setComposite(newComposite);        // Copy the button's image to the destination graphics, translucently
            g2d.drawImage(image, 0, 0, null);
        }    private static final long serialVersionUID = 23239L;
    }
      

  10.   

    想要JButton半透明,需要自己控制绘制过程。
    class TranslucentButton extends JButton 

    public TranslucentButton(){super();}
    public TranslucentButton(String label) {super(label);} 
    public TranslucentButton(Icon icon) {super(icon);} 
    public TranslucentButton(Action action){super(action);} 
    public TranslucentButton(String label,Icon icon){super(label,icon);}  @Override 
    public void paintComponent(Graphics g) 

    BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); 
    Graphics gButton = image.getGraphics(); 
    gButton.setClip(g.getClip()); 

    // Have the superclass render the button for us  
    super.paintComponent(gButton); 
    Graphics2D g2d = (Graphics2D) g; 
    AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f); 
    g2d.setComposite(newComposite);
    // Copy the button's image to the destination graphics, translucently  
    g2d.drawImage(image, 0, 0, null); 

    private static final long serialVersionUID = 23239L; 
    }