zoukankan      html  css  js  c++  java
  • 事件处理程序

    动作监听按钮,随机改变面板背景。

    package wjs;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    class Assembly{
        
    }
    public class RandomColors implements ActionListener{
        JFrame f;
        JPanel p;
        JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9;
        RandomColors() {
            JButton b=new JButton("随机变色");
            f=new JFrame();
            p=new JPanel(new GridLayout(3,3,10,10));
            p1=new JPanel();
            p2=new JPanel();
            p3=new JPanel();
            p4=new JPanel();
            p5=new JPanel(new GridLayout());
            p6=new JPanel();
            p7=new JPanel();
            p8=new JPanel();
            p9=new JPanel();
            f.add(p);
            p.add(p1);
            p.add(p2);
            p.add(p3);
            p.add(p4);
            p.add(p5);
            p.add(p6);
            p.add(p7);
            p.add(p8);
            p.add(p9);
            p5.add(b);
            p.setBackground(new Color(255,255,255));
            p1.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p2.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p3.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p4.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p5.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p6.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p7.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p8.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p9.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            f.setVisible(true);
            f.setSize(500, 500);
            b.addActionListener(this);
        }
    
        public static void main(String[] args) {
            new RandomColors();
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            p1.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p2.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p3.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p4.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p5.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p6.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p7.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p8.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            p9.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
            
        }
    
    }

  • 相关阅读:
    锐浪报表(Grid++Report)问题-子报表获取不到父报表参数问题解决
    C#PictureBox实现一直显示提示信息
    C#.Net ComboBox控件设置DropDownList之后背景颜色问题,以及发现的微软的一个BUG
    Visual Studio 2019移除/禁用Live Share按钮
    robotframework导入selenium2library变红
    robotframework selenium2library中关键字缺失的问题
    Jmeter之JSON提取器的使用
    Jmeter中unicode转码问题处理
    Jmeter之关联接口处理
    jmeter之用户自定义的变量
  • 原文地址:https://www.cnblogs.com/wjs0403/p/10844663.html
Copyright © 2011-2022 走看看