• awt


    public class MouseTest extends Frame{
        
        private static final long serialVersionUID = 5437685336595276319L;
        List<Point> list = new ArrayList<Point>();
        
        
        public void addPoint(Point p){
            
            list.add(p);
        }
        
        /**
         * 重写画笔
         */
        @Override
        public void paint(Graphics g) {
            Color c = g.getColor();
            for (Point point : list) {
                g.setColor(Color.white);
                g.fillOval(point.x, point.y, 10, 10);
            }
            g.setColor(c);
            
        }
    
        public static void main(String[] args) {
            final MouseTest f = new MouseTest();
            f.setBackground(Color.black);
            f.setLayout(null);
            f.setBounds(600, 100, 300, 300);
            f.setVisible(true);
            //窗口添加鼠标点击事件
            f.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    MouseTest f = (MouseTest) e.getSource();
                    f.addPoint(new Point(e.getX(), e.getY()));
                    //frame重画
                    f.repaint();
                    System.out.println("鼠标点击");
                }
            });
            
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    MouseTest f2 = (MouseTest) e.getSource();
                    f2.setVisible(false);
                    //f.setVisible(false);
                    //0正常退出,-1非正常退出
                    System.exit(0);
                    
                }
            });
            
            
            
        }
    
    }
    public class FrameTest extends Frame{
        
        @Override
        public void paint(Graphics g) {
            Color c = g.getColor();
            g.setColor(Color.green);
            g.drawLine(100, 100, 200, 200);
            g.setColor(c);
            
            //super.paint(g);
        }
        
        public void createFrame(String title,int x,int y,int w,int h){
            this.setTitle(title);
            this.setLayout(null);
            
            //this.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
            
            //new BorderLayout();
            //BorderLayout.CENTER;
            //new GridLayout(3, 2);3行两列
            
            this.setBounds(x, y, w, h);
            this.setBackground(Color.blue);
            Panel panel = new Panel();
            panel.setBounds(w/4, h/4, w/2, h/2);
            panel.setBackground(Color.yellow);
            this.add(panel);
            
            //this.add(panel, BorderLayout.NORTH);
            
            this.setVisible(true);
            
        }
        
        
    
        public static void main(String[] args) {
            FrameTest frameTest = new FrameTest();
            frameTest.createFrame("测试", 600, 400, 600, 600);
            
            /*Frame frame = new FrameTest();
            //frame.setLayout(null);
            frame.setBounds(200, 200, 300, 300);
            Button b = new Button("denglu");
            
            b.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("点击登陆");
                    
                }
            });
            
            frame.add(b);
            
            TextField field = new TextField();
            field.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    TextField tf = (TextField) e.getSource();
                    System.out.println(tf.getText());
                    tf.setText("");
                }
            });
            field.setEchoChar('*');
            frame.add(field);
            
            //frame.pack();
            frame.setVisible(true);*/
    
        }
    
    }

     键盘

    import java.awt.Frame;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class KeyTest extends Frame{
    
        public static void main(String[] args) {
    
            KeyTest k = new KeyTest();
            
            k.setLayout(null);
            k.setBounds(600, 100, 300, 300);
            k.setVisible(true);
            //键盘按下事件
            k.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    //System.out.println("键盘按下-->"+e.getKeyCode());
                    //w s a d  上下左右
                    switch (e.getKeyCode()) {
                    case 87:
                        System.out.println("上");
                        break;
                    case 83:
                        System.out.println("下");
                        break;
                    case 65:
                        System.out.println("左");
                        break;
                    case 68:
                        System.out.println("右");
                        break;
                    default:
                        break;
                    }
                    
                    
                }
            });
            
            k.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    KeyTest k2 = (KeyTest) e.getSource();
                    k2.setVisible(false);
                    System.exit(0);
                }
            });
        }
    
    }
  • 相关阅读:
    我很喜欢玩游戏,那么我就适合做游戏程序员吗?
    宁可多花1000元租房,也绝不要去挤半小时地铁
    996 盛行的年代,互联网人如何平衡工作和生活 ?
    互联网公司里都有哪些潜规则?
    那些拼命加班的程序员们,后来都怎么样了?
    MongoDB更需要好的模式设计 及 案例赏析
    MongoDB 提升性能的18原则(开发设计阶段)
    关于MongoDB数据库的日志解析
    实现MongoDB读写分离的“读偏好”介绍
    MongoDB分片 在部署和维护管理 中常见事项的总结
  • 原文地址:https://www.cnblogs.com/jentary/p/6376031.html
Copyright © 2020-2023  润新知