换背景算法
A. 大家帮我写个改变背景颜色的算法,内有代码
//完全按照你的意思来写的
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class MyFrame extends JFrame implements ItemListener{
private Choice cho1;
private JPanel pan;
MyFrame(){
this.setBounds(100,100,300,300);
this.setAlwaysOnTop(true);
pan = new JPanel();
cho1=new Choice();
cho1.addItemListener(this);
cho1.add("绿色");
cho1.add("黄色");
cho1.add("浅蓝色");
pan.add(cho1);
this.add(pan);
this.setVisible(true);
}
public static void main(String args[]){
new MyFrame();
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==cho1){
if(cho1.getSelectedItem()=="绿色"){
pan.setBackground(Color.GREEN);
}else if(cho1.getSelectedItem()=="黄色"){
pan.setBackground(Color.yellow);
}else if(cho1.getSelectedItem()=="浅蓝色"){
pan.setBackground(Color.blue);
}
}
}
}