Java~有哪位大虾可以帮我写一个小程序呀,用Java做一个界面,上面有四个按钮分别是红色,黄色,蓝色,退出,

用Java做一个图形化界面(用AWT),上面有四个按钮分别是红色,黄色,蓝色,退出,当点击不同的按钮的时候,界面的背景会变成相应的颜色,代码要简单简洁越短越好最好能调试通过再发上来不要花哨的东西,拜托了各位~~~~~~~~

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Screen{

public static void main(String args[]){
new Win();
}

static class Win extends JFrame implements ActionListener{
JPanel jp = new JPanel();
JButton jb[] = new JButton[4];
public Win(){
this.setBounds(0, 0, 320, 320);
Color c[] = {Color.red,Color.yellow,Color.blue};
jp.setBackground(Color.black);
for(int i = 0 ; i < 4 ; i++){
jb[i] = new JButton();
if(i!=3){
jb[i].setBackground(c[i]);
}else{
jb[i].setText("退出");
}
jb[i].addActionListener(this);
jp.add(jb[i]);
}
this.add(jp);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(!((JButton)e.getSource()).getText().equals("退出")){//如果不是退出按钮,则换颜色
jp.setBackground(((JButton)e.getSource()).getBackground());
}
else
System.exit(0);//退出
}

}
}
温馨提示:答案为网友推荐,仅供参考
相似回答