Java 实现按钮改变页面背景色
效果展示
代码分析
定义一个background类
继承自AWT的Frame类。同时定义4个按钮用于改变颜色。
public class background extends Frame {
Button button1 = new Button("Red");
Button button2 = new Button("Yellow");
Button button3 = new Button("Blue");
Button button4 = new Button("RandomColor");
}
定义background类
添加按钮和窗体。
add(button1); — —将创建的按钮添加到Frame中。
this.setLayout(new FlowLayout());— —为Frame设置布局管理器为FlowLayout,这意味着组件将按照添加的顺序从左到右排列,到达窗口边界时自动换行。
this.setSize(400,300);— —设置Frame的大小为400像素宽,300像素高。
this.setVisible(true);— —使窗口可见。
this.addWindowListener(new WindowAdapter();— —添加窗口监听器WindowAdapter,并重写windowClosing方法,当用户点击窗口关闭按钮时退出程序。
button1.addActionListener(new ActionListener();— —给button1添加事件监听器ActionListener。当点击红色按钮时,会通过导致事件的ActionEvent来调用actionPerformed方法,在方法中设置框架的背景色为红色。
类似地,给button2添加监听器,点击后会将背景颜色设置为黄色。
给button3添加监听器,点击后将背景颜色设置为蓝色。
给button4添加事件监听器。当此按钮被点击时,将生成一个随机颜色(通过随机RGB值)并设置为框架的背景颜色。
public background(){
add(button1);
add(button2);
add(button3);
add(button4);
this.setLayout(new FlowLayout());
this.setSize(400,300);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.red);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.yellow);
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.blue);
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B = (int)(Math.random()*256);
Color color = new Color(R,G,B);
background.this.setBackground(color);
}
});
}
定义程序入口
new background();— —创建background类的实例,这将展示一个窗口,窗口中包含四个按钮;点击不同的按钮可以更改窗口的背景色。
public static void main(String[] args){
new background();
}
完整代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class background extends Frame {
Button button1 = new Button("Red");
Button button2 = new Button("Yellow");
Button button3 = new Button("Blue");
Button button4 = new Button("RandomColor");
public background(){
add(button1);
add(button2);
add(button3);
add(button4);
this.setLayout(new FlowLayout());
this.setSize(400,300);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.red);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.yellow);
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.this.setBackground(Color.blue);
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B = (int)(Math.random()*256);
Color color = new Color(R,G,B);
background.this.setBackground(color);
}
});
}
public static void main(String[] args){
new background();
}
}