package com.yjq.lesson06; //import java.awt.Color; //import java.awt.event.ActionEvent; //import java.awt.event.ActionListener; //import java.awt.event.WindowAdapter; //import java.awt.event.WindowEvent; import javax.swing.; import java.awt.; import java.awt.event.*; public class Test_4 extends JFrame { //三个按钮 JButton jb1,jb2,jb3; //三原色初始值 int r=100,g=100,b=255; JPanel jp=new JPanel(); Test_4() { jp.setBackground(new Color(r,g,b)); jp.setLayout(null); this.setSize(320, 240); this.add(jp); this.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); //对三个按钮的设置 jb1=new JButton("红色"); jb2=new JButton("绿色"); jb3=new JButton("蓝色"); jb1.setBackground(Color.RED); jb2.setBackground(Color.GREEN); jb3.setBackground(Color.BLUE); jb1.setBounds(20,80,80,40); jb2.setBounds(120,80,80,40); jb3.setBounds(220,80,80,40); jb1.addActionListener(new changeColor()); jb2.addActionListener(new changeColor()); jb3.addActionListener(new changeColor()); jp.add(jb1); jp.add(jb2); jp.add(jb3); this.setVisible(true); } private class changeColor implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==jb1) { r+=10; if(r>255)r=0; jp.setBackground(new Color(r,g,b)); } if(e.getSource()==jb2) { g+=10; if(g>255) g=0; jp.setBackground(new Color(r,g,b)); } if(e.getSource()==jb3) { b+=10; if(b>255) b=0; jp.setBackground(new Color(r,g,b)); } } } public static void main(String[] args) { new Test_4(); } } 帮我注释一下代码
时间: 2024-01-21 17:16:37 浏览: 169
```
package com.yjq.lesson06;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test_4 extends JFrame {
// 三个按钮
JButton jb1, jb2, jb3;
// 三原色初始值
int r = 100, g = 100, b = 255;
JPanel jp = new JPanel();
Test_4() {
jp.setBackground(new Color(r, g, b));
jp.setLayout(null);
this.setSize(320, 240);
this.add(jp);
// 窗口关闭事件
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
// 对三个按钮的设置
jb1 = new JButton("红色");
jb2 = new JButton("绿色");
jb3 = new JButton("蓝色");
jb1.setBackground(Color.RED);
jb2.setBackground(Color.GREEN);
jb3.setBackground(Color.BLUE);
jb1.setBounds(20, 80, 80, 40);
jb2.setBounds(120, 80, 80, 40);
jb3.setBounds(220, 80, 80, 40);
jb1.addActionListener(new changeColor());
jb2.addActionListener(new changeColor());
jb3.addActionListener(new changeColor());
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
this.setVisible(true);
}
private class changeColor implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
r += 10;
if (r > 255)
r = 0;
jp.setBackground(new Color(r, g, b));
}
if (e.getSource() == jb2) {
g += 10;
if (g > 255)
g = 0;
jp.setBackground(new Color(r, g, b));
}
if (e.getSource() == jb3) {
b += 10;
if (b > 255)
b = 0;
jp.setBackground(new Color(r, g, b));
}
}
}
public static void main(String[] args) {
new Test_4();
}
}
```
注释:
1.导入需要的类
2.定义一个类Test_4,继承JFrame
3.定义三个按钮jb1,jb2,jb3
4.定义三原色初始值r,g,b
5.定义一个面板jp
6.在构造函数中初始化窗口、面板、按钮
7.添加窗口关闭事件
8.为三个按钮设置背景色、位置和监听器
9.定义一个内部类changeColor,实现ActionListener接口
10.在内部类中实现对三个按钮的监听事件
11.定义main方法,创建Test_4对象,运行程序
阅读全文