JavaSE------GUI(弹窗,面板,按钮,监听器,键盘事件,鼠标监听)

1.窗口

public class demo3 {
    public static void main(String[] args) {
        Frame frame = new Frame("昨晚梦到了那个她");

        Button CENTER = new Button("CENTER");
        Button EAST = new Button("EAST");
        Button WEST = new Button("WEST");
        Button NORTH = new Button("NORTH");
        Button SOUTH = new Button("SOUTH");

        frame.add(CENTER,BorderLayout.CENTER);
        frame.add(EAST,BorderLayout.EAST);
        frame.add(WEST,BorderLayout.WEST);
        frame.add(NORTH,BorderLayout.NORTH);
        frame.add(SOUTH,BorderLayout.SOUTH);

        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setVisible(true);
        frame.setResizable(false);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

在这里插入图片描述

2.弹窗,面板

public class demo1 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLocation(100,100);
        frame.setSize(400,400);
        //frame.setLayout(new GridLayout(2,2));//设置布局 网格布局
        Panel panel = new Panel(new GridLayout(2,2));

        panel.add(new Button("shiyan"),BorderLayout.CENTER);
        panel.add(new Button("shiyan"),BorderLayout.WEST);
        panel.add(new Button("shiyan"),BorderLayout.EAST);
        panel.add(new Button("shiyan"),BorderLayout.NORTH);

        frame.add(panel);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//0正常结束程序  1异常结束程序
            }
        });
    }
}

在这里插入图片描述

3.按钮

public class demo3 {
    public static void main(String[] args) {
        Frame frame = new Frame("昨晚梦到了那个她");

        Button CENTER = new Button("CENTER");
        Button EAST = new Button("EAST");
        Button WEST = new Button("WEST");
        Button NORTH = new Button("NORTH");
        Button SOUTH = new Button("SOUTH");

        frame.add(CENTER,BorderLayout.CENTER);
        frame.add(EAST,BorderLayout.EAST);
        frame.add(WEST,BorderLayout.WEST);
        frame.add(NORTH,BorderLayout.NORTH);
        frame.add(SOUTH,BorderLayout.SOUTH);

        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setVisible(true);
        frame.setResizable(false);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

在这里插入图片描述

4.监听器

按一次按钮运行一次,实现监听


public class demo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();
        MyActionListener actionListener = new MyActionListener();
        button.addActionListener(actionListener);

        frame.setBounds(400,400,400,400);
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
        windowclose(frame);
    }
    public static void windowclose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

5.简易计算器(加法)

public class demo3 {
    public static void main(String[] args) {

        new calcular().loadframe();

    }
}
class calcular extends Frame{

    TextField num1,num2,num3;

    public void loadframe(){
        num1=new TextField(10);
        num2=new TextField(10);
        num3=new TextField(10);

        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new mycalculatorlisener());

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);


    }

    private class mycalculatorlisener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int i = Integer.parseInt(num1.getText());
            int i1 = Integer.parseInt(num2.getText());
            num3.setText(""+(i+i1));
            num1.setText("");
            num2.setText("");
        }
    }
}

在这里插入图片描述

6.键盘事件


public class demo {
    public static void main(String[] args) {
        new key();
    }
}
class key extends Frame{
    public key(){
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("你按了上键");
                }
            }
        });
    }
}

在这里插入图片描述

7.画图

public class demo2 {
    public static void main(String[] args) {

        new huahua("画图");
    }
}
class huahua extends Frame{
    ArrayList points;
    public huahua(String title){
        super(title);
        setBounds(200,200,300,400);

        points=new ArrayList();
        setVisible(true);

        this.addMouseListener(new MyMouselistener());


    }
    public void paint(Graphics g){//绘图
        Iterator iterator = points.iterator();//迭代器
        while(iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.BLUE);
           g.fillOval(point.x,point.y,10,10);

        }
    }
    /*@Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }*/

    public void addPaint(Point point){
        points.add(point);
    }
    private class MyMouselistener extends MouseAdapter{

        @Override
        public void mousePressed(MouseEvent e) {
            huahua huahua = (huahua) e.getSource();
            huahua.addPaint(new Point(e.getX(),e.getY()));


            huahua.repaint();
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值