• JFrame画图基础和事件监听


    消息框

    JOptionPane.showMessageDialog(mine.this, "删除不成功!");

    画图

    class MyJPanel extends JPanel //继承面板类
    {
        public void paint(Graphics g) //覆盖父类的方法,paint相当于画图
        {
            g.drawOval(30, 30, 80, 80);  //画圆
            g.drawLine(100, 100, 200, 200); //画直线 ,两点坐标
            g.setColor(Color.RED);
            g.drawRect(50,50,50,50);
            g.draw3DRect(100, 100, 100, 100,true); //画矩形 ,参数是做上角坐标和长宽
            g.setColor(Color.BLUE);
            g.fillRect(100, 100, 100, 100); //填充矩形
            g.fill3DRect(50, 50, 50, 50, false); //填充3d效果的矩形
            
        }
    }

    导入图片

    Image tp=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/龙猫2.jpg")); //固定格式,得到图片
            g.drawImage(tp, 30, 30, 100, 100, this); //画图

    绘文字

    g.setColor(Color.BLUE);
            g.setFont(new Font("华文彩云",Font.BOLD,50));
            g.drawString("很强", 100, 100);

    监听事件

    JButton bt=new JButton("运行");
            bt.addActionListener(this);  //自己的
            MyListener jt=new MyListener();
            bt.addActionListener(jt);  //另一个
            bt.setActionCommand("yunxing");
    
        public void actionPerformed(ActionEvent e) //必须实现监听器的抽象方法
        {
            if(e.getActionCommand()=="yunxing")
            {
                System.out.println("运行成功");
            }
        }
    
    }
    
    class MyListener implements ActionListener  //继承父类
    {
        public void actionPerformed(ActionEvent e) //实现这个抽象函数
        {
            if(e.getActionCommand()=="yunxing") System.out.println("很强");
        }
    }

    键盘事件

    this.add(mjp);
    this.addKeyListener(mjp); //要添加监听器
    
    class MyJPanel extends JPanel implements KeyListener //给面板实现键盘监听接口,以下3个函数必须实现
    {
        int x=50,y=50;
        public void paint(Graphics g)
        {
            super.paint(g); //调用父类
            g.fillRect(x, y, 50, 50); //画矩形
        }
        public void keyPressed(KeyEvent e) { //按键事件
            // TODO Auto-generated method stub
            //System.out.println("键盘被按下");
            //System.out.println((char)e.getKeyCode());
            if(e.getKeyCode()==KeyEvent.VK_UP){    y-=5; }
            else if(e.getKeyCode()==KeyEvent.VK_DOWN){ y+=5; }
            else if(e.getKeyCode()==KeyEvent.VK_LEFT){ x-=5; }
            else if(e.getKeyCode()==KeyEvent.VK_RIGHT){ x+=5; }
            this.repaint();
            
        }
        public void keyReleased(KeyEvent e) { //松开事件
            // TODO Auto-generated method stub
            
        }
        public void keyTyped(KeyEvent e) { //打字事件
            // TODO Auto-generated method stub
            
        }
    }

    鼠标事件

    this.add(mjp);
    this.addMouseListener(mjp);
    
    class MyJPanel extends JPanel implements MouseListener //以下5个函数要写
    {
        int x=50,y=50;
        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawOval(x, y, 50, 50);
        }
        public void mouseClicked(MouseEvent e) //鼠标点击事件
        {
            System.out.println(e.getX()+","+e.getY());
            x=e.getX();
            y=e.getY();
            this.repaint();
        }
        public void mouseReleased(MouseEvent e) //松开
        {
            
        }
        public void mouseExited(MouseEvent e)  //出界面
        {
            
        }
        public void mousePressed(MouseEvent e) //按下
        {
            
        }
        public void mouseEntered(MouseEvent e) //进入界面
        {
            
        }
    }

    增加新的对话窗口

    public void actionPerformed(ActionEvent e) //原来窗口的监听
        {
            if(e.getActionCommand()=="tianjia")
            {
                Add add=new Add(this,"ok",true); //新的对话界面
              
            }
        }
    class Add extends JDialog implements ActionListener
    {
        public Add(Frame fck,String ckm,boolean msck)
        {
            super(fck,ckm,msck);  //父类
            //this.setTitle("学生信息");
            this.setSize(500, 500);
            this.setLocation(100, 100);
            JButton jb=new JButton("取消");
            jb.addActionListener(this);
            jb.setActionCommand("quxiao");
            this.add(jb);
            this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            this.setVisible(true);
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand()=="quxiao")
            {
                this.dispose(); //关闭窗口
            }
        }
    }
  • 相关阅读:
    终于有了自己的blog了。
    [Asp.Net+C#]Datagrid使用技巧二(怎样让对动态创建的列进行排序)
    [Asp.Net+C#]Datagrid使用技巧一(怎样灵活控制表头)
    CentOS下配置iptables防火墙
    ios中提示信息的实现及自动消失
    ios导航条添加按钮
    NSAutoreleasePool自动释放池
    什么是Tollfree bridging
    Android开发中的drawable(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系
    文章逐步迁移过来
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/5819108.html
Copyright © 2020-2023  润新知