Color change

package bbb;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class myframe implements ActionListener{
    JFrame f;
    JPanel p1,p2;
    JButton b1,b2,b3;
public myframe(){
    JFrame f=new JFrame("颜色改变");
    JPanel p1= new JPanel();
    JPanel p2= new JPanel();
    p2.setBackground(Color.yellow);
    JButton b1=new JButton("红色");
    b1.addActionListener(this);
    JButton b2=new JButton("绿色");
    b2.addActionListener(this);
    JButton b3=new JButton("蓝色");
    b3.addActionListener(this);
    f.setLayout(new BorderLayout());
      p1.add(b1);
      p1.add(b2);
      p1.add(b3);
      f.add(p1,BorderLayout.NORTH);
      f.add(p2,BorderLayout.CENTER);
    f.setSize(500,500);
    f.setVisible(true);
}
public  static void main(String []args){
    new myframe();
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO 自动生成的方法存根
    if(arg0.getSource() == b1)
    p2.setBackground(Color.red);
    else if(arg0.getSource() == b2)
    p2.setBackground(Color.green);
    else if(arg0.getSource() == b3)
    p2.setBackground(Color.blue);
}
}

 

Guess you like

Origin www.cnblogs.com/YShen0/p/10954545.html