import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;
public class Test0 extends Frame implements ActionListener{
static Test0 frm=new Test0();
public static void main(String[] args) {
frm.setTitle("随机变色");
frm.setBounds(200, 200, 400, 300);
frm.setLayout(new FlowLayout());
Button btt=new Button("随机变色");
btt.addActionListener(frm);
frm.add(btt);
frm.addWindowListener(new windowsclosed());
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Random rd=new Random();
int r=rd.nextInt(255);
int g=rd.nextInt(255);
int b=rd.nextInt(255);
frm.setBackground(new Color(r, g, b));
}
static class windowsclosed extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
Java之窗口随机变色
最新推荐文章于 2021-03-05 01:55:14 发布