• 监听按钮


    监听按钮

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.* ;
    
    import javax.swing.* ;
    
    class myTest extends JFrame implements ActionListener{
        JButton b1 = null;
        public myTest(){
            this.setSize(400,400) ;
            b1 = new JButton("改变 ");
            Container c = this.getContentPane();
            c.setLayout(new FlowLayout()) ;
            c.add(b1) ;
            //加一个监听
            b1.addActionListener(this);
            
            
            this.setVisible(true) ;
        }
        public void actionPerformed(ActionEvent e){
            if(b1.getForeground() == Color.GREEN){
                b1.setForeground(Color.RED);
            } else {
                b1.setForeground(Color.GREEN) ;
            }
        }
        
        
    }
    
    
    public class GUI {
        public static void main(String[] args) {
            myTest t = new myTest() ;
    
        }
    
    }

    鼠标监听

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    class myTest extends JFrame implements MouseListener, ActionListener{
        private JLabel l = null ;
        private JButton b1 = null ;
        private JButton b2 = null ;
        private JButton close = null ;
        public myTest(){
            this.setSize(400,400) ;
            Container c = this.getContentPane() ;
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
            c.setLayout(new FlowLayout()) ;
            l = new JLabel ("卓鹏真帅") ;
            l.addMouseListener(this) ;
            c.add(l) ;
            
            b1= new JButton("点我") ;
            b2 = new JButton("点我") ;
            b1.addActionListener(this) ;
            b2.addActionListener(this) ;
            c.add(b1) ;
            c.add(b2) ;
            
            close = new JButton("关闭") ;
            close.addActionListener(this) ;
            
            c.add(close) ;
            this.setVisible(true) ;
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override//鼠标进去
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub
            l.setForeground(Color.RED) ;
        }
    
        @Override//鼠标离开
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub
            l.setForeground(Color.BLUE) ;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == b1){
                System.out.println("HHH") ;
            } else if(e.getSource() == b2) {
                System.out.println("hhh") ;
            } else if(e.getSource() == close){
                setVisible(false) ;
                System.exit(0) ;// 1,-1.??
            }
        }
        
        
    }
    public class GUI{
        public static void main(String arg[]){
            myTest t = new myTest () ;
        }
    }
  • 相关阅读:
    打印机故障之乌龙事件
    为什么 FastAdmin 的插件不全部免费?
    PADS Logic 脚本的 Fields 一个对象记录
    时间模块和random模块

    模块的导入和使用
    递归函数与二分查找算法
    递归函数
    匿名函数
    内置函数
  • 原文地址:https://www.cnblogs.com/da-peng/p/5143458.html
Copyright © 2020-2023  润新知