拔丝芋头的Java学习日记---Day15、16---GUI编程

< 今日知识点 >

  • 窗口监听事件
  • 键盘监听事件
  • Swing之JFrame窗体
  • JDialog弹窗

·
·
·
·

—01 窗口监听事件
  • 大多数时候是用关闭和激活窗口的操作
  • 代码实例:
import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

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

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

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.pink);
        setBounds(100, 100, 300, 300);
        setVisible(true);

//        addWindowListener(new MyWindowListener());

        //匿名内部类
        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
//                        super.windowClosing(e);
                        System.out.println("你点击了关闭");
                    }

                }

        );
    }


    class MyWindowListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {

            System.exit(0);     //正常退出
        }
    }
}
  • 运行结果:
    在这里插入图片描述点击关闭按钮:
    在这里插入图片描述

·
·
·
·

—02 键盘监听事件
  • 代码实例:
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        KeyFrame keyFrame = new KeyFrame();
        WindowClose(keyFrame);

    }
    private static void WindowClose (Frame frame){     //关闭窗口的方法
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class KeyFrame extends Frame {
    public KeyFrame(){
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前按下的键盘的码
                //不需要去记住这个数值,直接使用静态属性 VK_XXX
                int keyCode =  e.getKeyCode();
                System.out.println(keyCode);
               if (keyCode == KeyEvent.VK_UP){
                   System.out.println("你按下了上键");
               }
            }
        });
    }
}
  • 运行结果:
    –窗口打开
    在这里插入图片描述
    –按下上键
    在这里插入图片描述
    ·
    ·
    ·
    ·

—03 Swing之JFrame窗体

— 代码实例01

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {

    //建立一个窗口
    public void init(){
        //顶级窗口
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,200,200);
        frame.setBackground(Color.orange);

        //设置文字JLabel
        JLabel label = new JLabel("欢迎光临");
        label.setHorizontalTextPosition(SwingConstants.CENTER);

        frame.add(label);
//        frame.getC


        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();


    }
}
  • 运行结果:

在这里插入图片描述


—代码实例02:

import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame().init();
    }
}

class MyJFrame extends JFrame{
    public void init(){
        //获得一个容器
        this.setVisible(true);
        this.setBounds(20,20,200,200);

        JLabel label = new JLabel("欢迎光临");
        this.add(label);

        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container  = this.getContentPane();
        container.setBackground(Color.orange);
    }
}
  • 运行结果:
    在这里插入图片描述
    ·
    ·
    ·
    ·

—04 JDialog弹窗

(弹窗自带有关闭功能,不用重复写)

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

//主窗口
public class DialogDemo extends JFrame {

    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700,700);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭

        //JFrame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击弹出一个对话框");   //创建
        button.setBounds(30,30,400,50);

        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() {     //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });      //这是一条语句

        container.add(button);

    }

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


//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){   //构造方法
        this.setVisible(true);
        this.setBounds(100,100,500,500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   //关闭

        Container container1 = this.getContentPane();
//        container1.setLayout(null);

        container1.add(new Label("BlackPink in your area!"));
    }


}

运行结果:
在这里插入图片描述

点击按钮
在这里插入图片描述
·
·
·
·

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值