1,主窗体中有十四个Button,产生一个随数,根据随机数,改变Button的背景颜色
2,在主窗体中如可显示图片。

解决方案 »

  1.   

    还有,就是我在主窗体中加一个JLabel 可总是加不上背景图片
      

  2.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestComponent extends JComponent
    {
    public TestComponent()
    {
    this.setLayout(new FlowLayout(5));

    for (int i = 0; i < 14; i++)
    {
    JButton btn = new JButton("按钮" + (i + 1));
    btn.setBackground(new Color((float) Math.random(), (float) Math.random(), (float) Math.random()));
    add(btn);
    }
    }

    public void paintComponent(Graphics g)
    {
    ImageIcon icon = new ImageIcon("background.jpg");
    int width = this.getWidth();
    int height = this.getHeight();
    g.drawImage(icon.getImage(), 0, 0, width, height, null);
    }

    public static void main(String[] args)
    {
    JFrame frame = new JFrame();
    frame.add(new TestComponent());
    frame.setSize(400, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  3.   

    好谢谢了,不是那种效果。我是一个点名系统,当我点 点名 时,主窗体中的十四个lable 的颜色 ,随着随机数而变化,也就是随机哪个数,哪个数对应的 lable就变成红色。实分感谢。
      

  4.   


    我表示不是很明白,你的意思是说每个数对应一种颜色,随机到某个数时这14个button就显示这个数对应的颜色吗?也就是说button的颜色一直是在变吗?