寒假笔记08

画笔
package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame{

    public void  loadFrame(){

        setBounds(200,200,500,500);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
    // 画笔

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
       // g.fillRect(100,100,100,100);
        g.fill3DRect(100,100,100,100,true);
    }
}
鼠标监听
package GUI;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouse {
    public static void main(String[] args) {
        new  MyFrame1().loadFrame();
    }
}

class MyFrame1 extends Frame{

    ArrayList points;

    public void loadFrame(){
        setBounds(100,100,500,500);
        setVisible(true);
        points = new ArrayList<>();
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addMouseListener(new MouseListener());
    }

    @Override
    public void paint(Graphics g) {
    // 画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.RED);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上
    public  void addPaint(Point point){
        points.add(point);
    }
    private  class MouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {

            MyFrame1 frame1 =(MyFrame1) e.getSource();
            // 鼠标点击产生一个点
            frame1.addPaint(new Point(e.getX(),e.getY()));

            // 每次点击鼠标都需要重新画
            frame1.repaint();
        }
    }
}

窗口监听
package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new  FrameWindow().FrameWindow();
    }
}

class  FrameWindow extends Frame{
    public  void FrameWindow(){
        setBackground(Color.blue);
        setBounds(100,100,300,300);
        setVisible(true);
        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowActivated(WindowEvent e) {
                        FrameWindow source = (FrameWindow) e.getSource();
                        source.setTitle("激活");
                        System.out.println("windowClosed");
                        super.windowActivated(e);
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                        System.exit(0);

                    }
                }
        );
    }
}

键盘监听

package GUI;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public  KeyFrame(){
        setBounds(100,100,500,500);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 获得键盘下的键
                int keyCode = e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("上键");
                }
            }
        });
    }
}

JFrame

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class  DialogDemon extends JFrame {
    public  DialogDemon(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // JFeame 放东西
        Container container = this.getContentPane();
        // 绝对布局
        container.setLayout(null);

        // 按钮
        JButton button = new JButton("点击");
        button.setBounds(10,10,100,100);
        // 点击按钮
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDiYiTanChuang();
            }
        });
        container.add(button);
    }
}
public class DialogDemo {

    public static void main(String[] args) {
        new DialogDemon();
    }
}

class MyDiYiTanChuang extends JDialog{
    public  MyDiYiTanChuang(){
        this.setVisible(true);
        this.setBounds(200,200,300,300);

        Container container = this.getContentPane();
        container.setLayout(null);

        JLabel label = new JLabel("加油");
        label.setBounds(100,100,100,100);
        container.add(label);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值