如何创建一个窗口并显示

package com.heima.gui;

import java.awt.*;

/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        f.setVisible(true); //设置窗体可见
    }
}

布局管理器

package com.heima.gui;

import java.awt.*;

/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        Button b1 = new Button("button1");
        f.add(b1);
        f.setLayout(new FlowLayout());
        f.setVisible(true); //设置窗体可见
    }
}

窗体监听

package com.heima.gui;

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


/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        Button b1 = new Button("button1");
        f.add(b1);
        f.setLayout(new FlowLayout());
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setVisible(true); //设置窗体可见
    }
}

/*class MyWindowListener implements WindowListener{

    @Override
    public void windowOpened(WindowEvent e) {

    }

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

    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("closed");

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}*/

鼠标监听

package com.heima.gui;

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


/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        Button b1 = new Button("button1");
        f.add(b1);
        f.setLayout(new FlowLayout());
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        b1.addMouseListener(new MouseAdapter() {    //鼠标监听
            @Override
            public void mouseReleased(MouseEvent e) {   //释放
                System.exit(0);
            }
        });
        f.setVisible(true); //设置窗体可见
    }
}

/*class MyWindowListener implements WindowListener{

    @Override
    public void windowOpened(WindowEvent e) {

    }

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

    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("closed");

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}*/

键盘监听和键盘事件

package com.heima.gui;

import java.awt.*;
import java.awt.event.*;


/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        Button b1 = new Button("button1");
        f.add(b1);
        f.setLayout(new FlowLayout());
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        b1.addMouseListener(new MouseAdapter() {    //鼠标监听
            @Override
            public void mouseReleased(MouseEvent e) {   //释放
                System.exit(0);
            }
        });

        b1.addKeyListener(new KeyAdapter() {    //键盘监听
            @Override
            public void keyReleased(KeyEvent e) {
                //System.exit(0);
                //System.out.println(e.getKeyCode());
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.exit(0);
                }
            }
        });
        f.setVisible(true); //设置窗体可见
    }
}

/*class MyWindowListener implements WindowListener{

    @Override
    public void windowOpened(WindowEvent e) {

    }

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

    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("closed");

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}*/

动作监听

package com.heima.gui;

import java.awt.*;
import java.awt.event.*;


/**
 * Code by pureqh on 2021-03-18
 */

public class Demo1_Frame {
    public static void main(String[] args) {
        Frame f = new Frame("my first windows");
        f.setSize(400,600);
        f.setLocation(300,50);
        f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/Public/day25/src/com/heima/gui/ico.png"));
        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        f.add(b1);
        f.add(b2);
        f.setLayout(new FlowLayout());
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        b1.addMouseListener(new MouseAdapter() {    //鼠标监听
            @Override
            public void mouseReleased(MouseEvent e) {   //释放
                System.exit(0);
            }
        });

        b1.addKeyListener(new KeyAdapter() {    //键盘监听
            @Override
            public void keyReleased(KeyEvent e) {
                //System.exit(0);
                //System.out.println(e.getKeyCode());
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.exit(0);
                }
            }
        });

        b2.addActionListener(new ActionListener() { //动作监听
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        f.setVisible(true); //设置窗体可见
    }
}

/*class MyWindowListener implements WindowListener{

    @Override
    public void windowOpened(WindowEvent e) {

    }

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

    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("closed");

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}*/

适配器设计模式

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注