首页 > 其他 > 详细

颜色改变

时间:2019-05-31 13:21:45      阅读:98      评论:0      收藏:0      [点我收藏+]
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);
}
}

 

颜色改变

原文:https://www.cnblogs.com/YShen0/p/10954545.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!