zoukankan      html  css  js  c++  java
  • 自学第六次博客(动作事件的处理)

    按钮动作事件示例

    import javax.swing.*;
    
    public class Action extends JFrame{
        static JButton b1=new JButton("红色");
        static JButton b2=new JButton("黄色");
        static JButton b3=new JButton("蓝色");
        static JPanel p=new JPanel();
        static JLabel l=new JLabel("请单击下面按钮");
    
        public Action(){
            super("动作事件");
            setBounds(10,20,220,200);
            l.setOpaque(true);
            l.setBounds(0, 0, 220, 150);
            l.setHorizontalAlignment(JLabel.CENTER);
            add(l,"Center");//将标签置于中间
            p.add(b1);//添加按钮
            p.add(b2);
            p.add(b3);
            add(p,"South");
            b1.addActionListener(new B());
            b2.addActionListener(new B());
            b3.addActionListener(new B());
            setVisible(true);
            
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Action f=new Action();
        
    
        }
    
    }
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class B implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            
            // TODO Auto-generated method stub
            if(e.getSource()==Action.b1){
                Action.l.setText("按下的是红色按钮");
                Action.l.setBackground(Color.red);
            } 
            if(e.getSource()==Action.b2){
                Action.l.setText("按下的是黄色按钮");
                Action.l.setBackground(Color.yellow);
            }
            if(e.getSource()==Action.b3){
                Action.l.setText("按下的是蓝色按钮");
                Action.l.setBackground(Color.blue);
            }
    
        }
    
    }

    运行结果:

     点击红色按钮:

     点击黄色按钮:

     点击蓝色按钮:

  • 相关阅读:
    几种数据结构的查找、删除、插入的时间复杂度(数组 链表 二叉查找树 平衡二叉查找树 哈希表)
    java 多线程的状态迁移 常用线程方法分析
    JVM内存分区
    详解 Java I/O 与装饰者模式
    详解 java 异常
    ExecutorService 线程池详解
    CG group
    VIM 配置python
    Q35+uefi or bios+legacy // PCI | PCIE
    硬盘接口协议
  • 原文地址:https://www.cnblogs.com/sunblingbling/p/11979555.html
Copyright © 2011-2022 走看看