Gui-Swing简介

1、Gui核心技术: Swing     AWT

1.因为界面不美观。

2.需要 jre 环境!

学习目的:

1. 可以写出自己心目中想要的一些小工具

2. 工作时候,也可能需要维护到swing界面,概率极小!

3. 了解MVC框架,了解监听!

2.1、什么是AWT

1. 包含了很多类和接口! GUI!

2. 元素: 窗口,按钮,文本框

3. java.awt

 2.2、 组件和容器

1、Frame

package com.wzj.guidemo;


import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {

        Frame frame = new Frame("我的第一个java图像界面窗口");

        //需要设置界面可见性
        frame.setVisible(true);

        //设置界面大小
        frame.setSize(400,400);

        //设置界面的颜色
        frame.setBackground(new Color(13, 128, 119));

        //弹出的初始位置
        frame.setLocation(200,200);

        //窗口大小固定
        frame.setResizable(false);

    }
}

 问题:无法关闭该界面,需要终止程序运行。

类的分装,实现多个窗口;

package com.wzj.guidemo;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //显示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.blue);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.red);
    }
}

 class MyFrame extends Frame{
    static int id = 0;  //可能存在多个窗口,这里id充当计时器

     //构造函数
     public MyFrame(int x,int y ,int w, int h,Color color){
         //调用父类的构造方法
         super("Myframe"+(++id));
         setBounds(x,y,w,h);
         setBackground(color);
         setVisible(true);
         setResizable(false);

     }
 }

2、Panel

解决了弹窗关闭不了的问题

package com.wzj.guidemo;

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

public class TestPanel {

    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        //设计布局,这里不设计
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(124, 128, 53));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(0x2A5B6F));

        //frame.add(panel),括号里需要是组件
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件,System.exit(0)退出程序
        //适配器模式:
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

3、布局管理器

    ·  流式布局

package com.wzj.guidemo;

import java.awt.*;

//流式布局
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件-按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置为流失布局
        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));

        frame.setSize(200,200);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

    ·  东西南北中布局

package com.wzj.guidemo;

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

//东南西北东布局
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        Button east = new Button("east");
        Button north = new Button("north");
        Button south = new Button("south");
        Button west = new Button("west");
        Button center = new Button("center");


        frame.add(east,BorderLayout.EAST);
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(west,BorderLayout.WEST);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(200,200);
        frame.setVisible(true);

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

    ·  表格布局

package com.wzj.guidemo;

import java.awt.*;

//表格布局
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack(); //java函数
        frame.setVisible(true);


    }
}

 

综合布局案例

package com.wzj.guidemo;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test {
    public static void main(String[] args) {
        Frame frame = new Frame("综合案例");
        //表格布局
        frame.setLayout(new GridLayout(2,1));

        //创建四个面板
        Panel p1 = new Panel();
        Panel p2 = new Panel();
        Panel p3 = new Panel();
        Panel p4 = new Panel();

        p1.setLayout(new BorderLayout());
        p2.setLayout(new GridLayout(2,1));
        p3.setLayout(new BorderLayout());
        p4.setLayout(new GridLayout(2,2));

        //总体布局第一行
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("btn-1"));
        p2.add(new Button("btn-2"));
        p1.add(p2, BorderLayout.CENTER);

        //总体布局第二行
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);
        p4.add(new Button("p4-btn-1"));
        p4.add(new Button("p4-btn-2"));
        p4.add(new Button("p4-btn-3"));
        p4.add(new Button("p4-btn-4"));
        p3.add(p4,BorderLayout.CENTER);


        frame.setSize(400,400);
        frame.setVisible(true);
        frame.add(p1);
        frame.add(p3);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


    }
}

总结:

1. Frame是一个顶级窗口

2. Panel无法单独显示,必须添加到某个容器中。

3. 布局管理器

    1. 流式

    2. 东西南北中 

    3. 表格

4. 大小,定位,背景颜色,可见性,监听!

4. 事件监听

一个按钮的情况

package com.wzj.listenerevent;

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

public class Demo1 {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
         Frame frame = new Frame();
         Button button = new Button();
         //因为,addActionListener()需要一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.pack();
        frame.add(button);
        WindowClose(frame);
        frame.setVisible(true);
    }
    //关闭窗口事件,把这个事件写成一个可以公共调用的方法
    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("你点击了按钮");
    }
}

多个按钮共享一个事件

package com.wzj.listenerevent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Demo2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLayout(new BorderLayout());
        Button button1 = new Button("start");
        button1.setActionCommand("start");
        Button button2 = new Button("end");
        button2.setActionCommand("end");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);
        frame.add(button1, BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.pack();
        frame.setVisible(true);
    }
}

class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("msg=>"+e.getActionCommand());
    }
}

5. 输入框监听事件

package com.wzj.listenerevent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static com.wzj.listenerevent.Demo1.WindowClose;

public class Demo3 {
    public static void main(String[] args) {
        new MyFrame();
        WindowClose(new MyFrame());
    }
}

class MyFrame extends Frame{
  public MyFrame(){
      TextField textField = new TextField();
      this.add(textField);

      //监听这个文本框输入的文字
      MyActionListener1 myActionListener1 = new MyActionListener1();
      //按下enter,就会触发这个输入框的事件
      textField.addActionListener(myActionListener1);

      pack();
      setVisible(true);

  }
}

class MyActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();  //获得一些资源,返回的一个对象
        System.out.println(textField.getText());   //获得输入框的文本
        textField.setText(""); //null
    }
}

6. 实现计算器的加法功能

package com.wzj.listenerevent;

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

public class TextCaculator{
    public static void main(String[] args) {
    //启动
        Caculator caculator =new Caculator();

    //关闭
        caculator.WindowClose(caculator);
    }
}

//创建计算机类
class Caculator extends Frame{
  public Caculator(){
      //构建组件,三个文本框
      TextField num1 = new TextField(10);
      TextField num2 = new TextField(10);
      TextField num3 = new TextField(20);

      //一个符号
      Label label = new Label("+");

      //一个按钮
      Button button = new Button("=");

      //布局,这里选择浮动布局
      setLayout(new FlowLayout());

      add(num1);
      add(label);
      add(num2);
      add(button);
      add(num3);

      //调用我们的监听事件
      button.addActionListener(new MyAction(num1,num2,num3));

      pack();
      setVisible(true);
}

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


//监听器类
  class MyAction implements ActionListener{
    //接受输入的内容
    private TextField num1,num2,num3;
    public MyAction(TextField num1,TextField num2,TextField num3){
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //将num1和num2的和显示在num3文本框中
        num3.setText(Integer.parseInt(num1.getText())+Integer.parseInt(num2.getText())+"");

        //num1和num2框中的内容消失
        num1.setText("");
        num2.setText("");
    }
}

完全改造为面向对象

package com.wzj.listenerevent;

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

public class TextCaculatorPro {
    public static void main(String[] args) {
        //开启
        CaculatorPro caculatorPro = new CaculatorPro();
        caculatorPro.loadFrame();
        //关闭
        caculatorPro.WindowClose(caculatorPro);
    }
}

//计算器类
class CaculatorPro extends Frame{

    //属性
    TextField num1,num2,num3;

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

    public void loadFrame(){
     //构建组件
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        Button button = new Button("=");

        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        button.addActionListener(new MyActionPro(this));

        pack();
        setVisible(true);
    }

}

//监听器
class MyActionPro implements ActionListener{
    CaculatorPro caculatorpro = null;

    //构造器
    public MyActionPro(CaculatorPro caculatorpro){
        this.caculatorpro = caculatorpro;
    }

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

    caculatorpro.num1.setText("");
    caculatorpro.num2.setText("");
    }
}

 7. 鼠标监听

package com.wzj.paint;

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

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

//自己的类
   class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标的当前位置,需要集合存储这个点
    ArrayList points;
    public MyFrame(String title){
        super(title);
        setBounds(200,200,500,500);
        setVisible(true);
        //存鼠标点击的点
        points = new ArrayList<>();
        //鼠标监听器
        this.addMouseListener(new MyMouseListener());
    }

    @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);
    }

}


   //适配器模式
 class MyMouseListener extends MouseAdapter {

    @Override
    public void mouseClicked(MouseEvent e) {
        MyFrame myFrame =(MyFrame) e.getSource();
        //点击的时候会产生一个点
        //这个点就是鼠标的点
        myFrame.addPaint(new Point(e.getX(),e.getY()));
        myFrame.repaint();
    }
}

8. 键盘监听

package com.wzj.caselistener;

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

public class CaseListenerTest {
    //按下键盘上的一个键产生相对应的结果
    public static void main(String[] args) {
        new KeyFra();
    }
}

class KeyFra extends Frame{
    public KeyFra(){
        setBounds(1,2,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("你按下了上键");
                }
            }
        });
    }
}

9,窗口监听

package com.wzj.windowlistener;

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

public class WindowListenerTest {
    //隐藏窗口,通过按钮,隐藏当前窗口
    public static void main(String[] args) {
        new Hide("隐藏窗口");
    }
}

class Hide extends Frame{
    public Hide(String title) {
        super(title);
        setBounds(200,200,500,500);
        setVisible(true);
        addWindowListener(new HideWindow(this));
    }
}

class HideWindow extends WindowAdapter {
    Hide hide = null;

    public HideWindow(Hide hide){
        this.hide = hide;
    }
    
    @Override
    public void windowClosing(WindowEvent e) {
        hide.setVisible(false);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值