GUI编程入门到游戏实战

GUI编程

GUI:图形用户界面编程
GUI编程学习路线

  • GUI是什么
  • GUI怎么学
  • GUI使用场景
    组件
  • 监听
  • 弹窗
  • 面板
  • 鼠标
  • 按钮

1、简介

GUI核心技术:Swing AWT

缺点:

  1. 不美观
  2. 需要jre环境

为什么要学习

  1. 可以写出一些自己用的小工具
  2. 可能会涉及到swing的维护工作->破解
  3. 了解MVC框架,了解监听

2.AWT

2.1.AWT介绍

  1. AWT:抽象的窗口工具,包含了很多的类和接口
  2. 元素:窗口、按钮、文本框
  3. java.awt包下
    在这里插入图片描述

2.2.组件和容器

1.Frame

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

        //Frame
        Frame frame = new Frame("我的第一个Java图形界面窗口");

        //需要设置可见性 w h
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);
        frame.setBackground(new Color(23, 136, 167));
        frame.setLocation(200,200);
        
        //设置大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述
问题:关闭按钮无反应,关闭程序进程才会关闭窗口

封装打开多个实现:

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


    }
}
class MyFrame extends Frame{
    static int id = 0; //可能存在多个窗口,我们需要一个计数器

    //封装
    public MyFrame(int x,int y,int w,int h,Color color){
        super("Myframe"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }

}

在这里插入图片描述
2.Panel

//Panel可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        frame.setLayout(null);

        //坐标
        frame.setBounds(400,400,500,500);
        frame.setBackground(new Color(100, 182, 90));

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

        frame.add(panel);

        frame.setVisible(true);
    }
}

在这里插入图片描述
解决关闭事件

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

        frame.setLayout(null);

        //坐标
        frame.setBounds(400,400,500,500);
        frame.setBackground(new Color(100, 182, 90));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(175, 103, 103));
		//将面板放入panel中
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);//0正常退出,1异常退出
            }
        });

    }

2.3.布局管理器

  • 流式布局 FlowLayout
 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.RIGHT));
        frame.setSize(200,200);

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

        frame.setVisible(true);

    }

在这里插入图片描述

  • 东西南北中
    在这里插入图片描述
    在这里插入图片描述
 public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        Button east = new Button("East");
        Button west = new Button("West");
        Button south= new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

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

        frame.setSize(300,400);
        frame.setVisible(true);
    }

在这里插入图片描述

  • 表格式布局 GridLayout
public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");

        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.setSize(400,600);
        frame.pack();//Java函数,自动选择合理的部位布局,自动布局大小,测试过程中发现不能在第一行写这个,需要在添加完毕后增加会分配默认size
        frame.setVisible(true);


    }

在这里插入图片描述

  • 实现效果
    在这里插入图片描述
  • 嵌套布局:

在这里插入图片描述代码:

public static void main(String[] args) {
        //总Frame
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));

        //4个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        //上面ok
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2,BorderLayout.CENTER);

        //下面ok

        p3.add(new Button("West-2"),BorderLayout.WEST);
        p3.add(new Button("East-2"),BorderLayout.EAST);
        //中间的4个
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i));
            
        }
        p3.add(p4,BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);
    }

2.4事件监听

1、按下按钮,触发一些事件
package kuangshen.lesson02;


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 TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();//窗口
        Button button = new Button();//按钮
        //因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();

        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();//自适应

        windowClose(frame);
        frame.setVisible(true);

    }
    //关闭窗体的事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}
//构造了一个ActionListener
class MyActionListener implements ActionListener{

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

    }
}

2、一个监听可以给多个按钮共同使用
package kuangshen.lesson02;

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 TestActionEvent2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setBounds(100,100,500,500);
        //一个监听可以多个按钮共同使用
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                e.getActionCommand()获得按钮的信息

                System.out.println(e.getActionCommand());
            }
        };
        Button button1 = new Button("1");
        //可以为按钮带参数来控制同一个监听执行不同按钮的方法
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
        button1.setActionCommand("1111111111");
        button1.addActionListener(actionListener);

        Button button2 = new Button("2");
        button2.addActionListener(actionListener);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

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

3、输入框TextField监听
package kuangshen.lesson02;

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 TestActionEvent3 {
    public static void main(String[] args) {
        //main方法只管启动
        new MyFrame();

    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField= new TextField();

        //监听这个文本框输入的文字
//        textField.addActionListener((event)->{
//            System.out.println(textField.getText());
//            textField.setText("");
//        });
        MyActionListener2 mymonitor = new MyActionListener2();
        //按下enter就会触发这个输入框的事件
        textField.addActionListener(mymonitor);

        //设置替换编码
        textField.setEchoChar('*');
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        add(textField);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //向下转型为TextField类型
        TextField field=(TextField) e.getSource();
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//null ""
    }
}

4、计算器

oop原则:组合,大于继承
在这里插入图片描述
(1)原始代码

package kuangshen.lesson02;
//简易计算器
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 TestCalc {
    public static void main(String[] args) {
        new Calc();
    }

}
class Calc extends Frame{
    public Calc(){//构造器
        TextField jia1 = new TextField(10);//字符数
        TextField jia2 = new TextField(10);
        TextField rest = new TextField(20);

        setLayout(new FlowLayout());//从左到右流式布局
        add(jia1);
        add(new Label("+"));
        add(jia2);
       //按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(jia1,jia2,rest));

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

        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    private TextField jia1,jia2,rest;
    public MyCalculatorListener(TextField jia1,TextField jia2,TextField rest) {
        this.jia1 = jia1;
        this.jia2 = jia2;
        this.rest = rest;
    }
    //获取3个变量,通过传参的方式

    @Override
    public void actionPerformed(ActionEvent e) {
        //1、获得加数和被加数
        int n1 = Integer.parseInt(jia1.getText());
        int n2 = Integer.parseInt(jia2.getText());

        //2、将这个值 +法运算后,放到第三个框
        rest.setText(""+(n1+n2));

        //3、清除前两个框
        jia1.setText("");
        jia2.setText("");

    }
}

(2)优化后代码(完全改造为面向对象写法:)

package kuangshen.lesson02;
//简易计算器
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 TestCalc {
    public static void main(String[] args) {
        new Calc().loadFrame();
    }

}
class Calc extends Frame{
    //属性
    TextField jia1,jia2,rest;
    //方法
    public void loadFrame(){
         jia1 = new TextField(10);//字符数
         jia2 = new TextField(10);
         rest = new TextField(20);

        setLayout(new FlowLayout());//从左到右流式布局
        add(jia1);
        add(new Label("+"));
        add(jia2);
        //按钮
        Button button = new Button("=");

        button.addActionListener(new MyCalculatorListener(this));//this把自己丢进去

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

        pack();
        setVisible(true);

    }

}
//监听器类
class MyCalculatorListener implements ActionListener{

    //获得计算器这个对象,在一个类中组合另外一个类,平常最常用的一种方法
    Calc calc = null;
    public MyCalculatorListener(Calc calc) {
        this.calc = calc;


    }
    //获取3个变量,通过传参的方式
    @Override
    public void actionPerformed(ActionEvent e) {
        //1、获得加数和被加数
        int n1 =  Integer.parseInt(calc.jia1.getText());
        int n2 =  Integer.parseInt(calc.jia2.getText());
        //2、将这个值 +法运算后,放到第三个框
        calc.rest.setText(""+(n1+n2));
        //3、清除前两个框
        calc.jia1.setText("");
        calc.jia2.setText("");

    }
}

(3)内部类,更好的包装

package kuangshen.lesson02;
//简易计算器
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 TestCalc {
    public static void main(String[] args) {
        new Calc().loadFrame();
    }

}
class Calc extends Frame{
    //属性
    TextField jia1,jia2,rest;
    //方法
    public void loadFrame(){
         jia1 = new TextField(10);//字符数
         jia2 = new TextField(10);
         rest = new TextField(20);

        setLayout(new FlowLayout());//从左到右流式布局
        add(jia1);
        add(new Label("+"));
        add(jia2);
        //按钮
        Button button = new Button("=");

        button.addActionListener(new MyCalculatorListener());//this把自己丢进去

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

        pack();
        setVisible(true);

    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!降低编码成本
    
    private class MyCalculatorListener implements ActionListener{
        //使用内部类的话,天然的就可以拿到这几行

//        //获得计算器这个对象,在一个类中组合另外一个类,平常最常用的一种方法
//        Calc calc = null;
//        public MyCalculatorListener(Calc calc) {
//            this.calc = calc;
//
//
//        }

        @Override
        public void actionPerformed(ActionEvent e) {
            //1、获得加数和被加数
            int n1 =  Integer.parseInt(jia1.getText());
            int n2 =  Integer.parseInt(jia2.getText());
            //2、将这个值 +法运算后,放到第三个框
            rest.setText(""+(n1+n2));
            //3、清除前两个框
            jia1.setText("");
            jia2.setText("");
        }
    }
}

5、画笔监听
package kuangshen.lesson02;

import zuoshen.class06.Graph;
//画笔,这个不是很懂
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(){
        setVisible(true);
        setBounds(100,100,800,600);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    @Override
    public void paint(Graphics g){
        //画笔,需要有颜色,画笔可以画画
        g.setColor(Color.blue);
        g.drawOval(100,100,100,100);//圆
        g.fillOval(100,200,100,100);//实心的圆
        g.fillRect(100,300,200,100);

        g.setColor(Color.GREEN);//
        g.fillRect(150,400,200,200);
        //养成习惯,画笔用完,将他还原到最初的颜色
    }

}
6、鼠标监听(***)

在这里插入图片描述

package kuangshen.lesson02;

import javax.swing.text.html.HTMLDocument;
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 Paint("画图");
    }
}
//自己的类
class Paint extends Frame{
//    画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;


    public Paint(String title){
        super(title);
        setBounds(100,100,800,600);
        //存鼠标点击的点
        points = new ArrayList<>();

        setVisible(true);
        //        鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());//鼠标就被监听到了
    }

    @Override
    public void paint(Graphics g){
        //具体实施的类
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){//如果iterator里还有点,把它画出来
            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{
        //        鼠标,按下,弹起,按住不放
        //按下鼠标时触发
        public void mousePressed(MouseEvent e){//e代表获取鼠标这个资源
            Paint paint = (Paint)e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点,这个点就是鼠标的点
//            new Point(e.getX(),e.getY());//需要把这个点添加到paint()

            paint.addPaint( new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            paint.repaint();//刷新
        }
   }
}
7、窗口监听

在这里插入图片描述
激活事件:
在这里插入图片描述

8、键盘监听(***)
package kuangshen.lesson02;

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) {
        new MyKeyFrame();
    }

}
class MyKeyFrame extends Frame {
    public MyKeyFrame(){
        super("默认窗口名称");
        
        setBounds(100,100,800,600);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘下的键是哪一个
                int keyCode = e.getKeyCode();//获取当前键盘的码
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_ENTER){//VK是一个前缀
                    System.out.println("按了回车");
                }
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);

            }
        });
    }
}

3.Swing

Swing是amt的封装演化,

3.1 窗口JFrame

public class TestFrame1 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("第一个窗口");
        jFrame.setBounds(10,10,400,400);
        jFrame.setVisible(true);

        JLabel jLabel = new JLabel("测试标签");
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.YELLOW);

        //标签居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jFrame.add(jLabel);
//        不设置,默认是隐藏窗口
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

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

3.2 弹窗JDialog

package kuangshen.lesson02;

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,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");//创建
        jButton.setBounds(30,30,200,50);

        //点击按钮的时候,弹出一个弹窗
        jButton.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo1();
            }
        });
        contentPane.add(jButton);
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo1 extends JDialog{
    public MyDialogDemo1() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);

        //JFrame 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
       // contentPane.setLayout(null);
        JLabel jLabel = new JLabel("我们由奇迹构成");
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(jLabel);
    }
}

在这里插入图片描述

3.3图标Icon

package kuangshen.lesson02;

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

//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {

    private int width;
    private int height;

    public IconDemo() {
    }

    public IconDemo(int width, int height){
        this.width = width;
        this.height = height;
    }

    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        //图标放在标签之上,也可以放在按钮上
        JLabel jLabel = new JLabel("高桥一生",iconDemo,SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(jLabel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);

    }
    @Override
    public int getIconWidth() {
        return this.width;
    }
    @Override
    public int getIconHeight() {
        return this.height;
    }
}

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

  • 图片形式的图标
package kuangshen.lesson02;

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
//        获取图片的地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url =ImageIconDemo.class.getResource("touxiang.jpg");
        ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突了
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(jLabel);

        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

3.4滚动条面板

  • 面板
package kuangshen.lesson02;

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

public class JPanelDemo extends JFrame {

    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//后面两个参数是设置间隔

        JPanel panel1 = new JPanel(new GridLayout(1,3));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("0"));
        panel1.add(new JButton("2"));
        
        container.add(panel1);
        this.setVisible(true);
        this.setSize(500,50);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

  • 滚动条
  • `package kuangshen.lesson02;

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

public class JSrocllDemo extends JFrame {
public JSrocllDemo() {
Container container = this.getContentPane();
// 文本域
JTextArea textArea = new JTextArea(50,100);
textArea.setText(“我们由奇迹构成”);

    //Scroll面板
    JScrollPane scrollPane = new JScrollPane(textArea);
    container.add(scrollPane);

    this.setVisible(true);
    this.setBounds(200,200,600,600);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
    new JSrocllDemo();
}

}
`
在这里插入图片描述

3.5按钮

  • 图片按钮
package kuangshen.lesson02;

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();
//        将一个图片变为图标
        URL resource =JButtonDemo01.class.getResource("touxiang.jpg");
        Icon icon = new ImageIcon(resource);

//        把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        container.add(button);

        this.setBounds(200,200,500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

单选按钮

package kuangshen.lesson02;

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();
//        将一个图片变为图标
        URL resource =JButtonDemo01.class.getResource("touxiang.jpg");
        Icon icon = new ImageIcon(resource);

//        单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");

        //由于单选框只能选择一个,分组
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.SOUTH);
        container.add(radioButton3,BorderLayout.NORTH);


        this.setBounds(200,200,500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

复选按钮

package kuangshen.lesson02;

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();
//        将一个图片变为图标
        URL resource =JButtonDemo01.class.getResource("touxiang.jpg");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);



        this.setBounds(200,200,500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

3.6列表

  • 下拉框
package kuangshen.lesson02;

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

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();

        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");

        container.add(status);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

  • 列表框
package kuangshen.lesson02;

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

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01() {
        Container container = this.getContentPane();

//        生成列表的内容
        String[] contents = {"1","2","3"};
        JList list = new JList(contents);

        container.add(list);


        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

在这里插入图片描述

应用场景

  • 选择地区,或者一些单个选项
  • 列表,展示信息,一般是动态扩容的

3.7文本框

  • 文本框
package kuangshen.lesson02;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container = this.getContentPane();
        container.setLayout(null);

        JTextField textField = new JTextField("hello");
        JTextField textField1 = new JTextField("world", 20);

        container.add(textField,BorderLayout.NORTH);
        container.add(textField1,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

在这里插入图片描述

  • 密码框
package kuangshen.lesson02;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');

        container.add(passwordField);


        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

在这里插入图片描述

  • 文本域
package kuangshen.lesson02;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container = this.getContentPane();

//        文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("我们由奇迹构成");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

在这里插入图片描述

4.贪吃蛇

帧,如果时间片足够小,就是动画 ,一秒30帧,连起来是动画 拆开就是图片

键盘监听 addKeyListener

需要注意 如果是面板添加监听需要设置当前面板获取焦点setFocusable(true);

定时器Timer

StartGame.java

package kuangshen.snake;

import kuangshen.snake.GamePanel;

import javax.swing.*;

public class StartGame {
    public static void main(String[] args) {
        JFrame frame = new JFrame("贪吃蛇");
        //正常的游戏界面都应该在面板上
        frame.add(new GamePanel());
        frame.setResizable(false);//窗口大小不变

        frame.setBounds(10,10,900,720);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

GamePanel.java

package kuangshen.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//定义一个数据、初始化、放上去,监听它(键盘、事件)
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length; //蛇的长度
    int[] snakeX = new int[600];//蛇的x坐标 25*25
    int[] snakeY = new int[500];//蛇的y坐标 25*25
    String fx;

    //食物的坐标
    int foodx;
    int foody;
    Random random = new Random();

    int score; //成绩


    //游戏当前的状态:开始、停止
    boolean isStart = false;//默认不开始
    boolean isFail = false;//默认游戏状态不失败

    //定时器 ,以ms为单位 1000ms = 1s
    Timer timer = new Timer(100,this);//100毫秒执行一次

    //构造器
    public GamePanel() {
        init();
       //获得焦点和键盘事件
        this.setFocusable(true); //获得焦点事件
        this.addKeyListener(this);//获得键盘监听事件
        timer.start(); //游戏一开始定时器就启动
    }

    public void init(){
        length = 3;
        snakeX[0] = 100;snakeY[0] = 100;//脑袋的坐标
        snakeX[1] = 75;snakeY[1] = 100;//第一个身体的坐标
        snakeX[2] = 50;snakeY[2] = 100;//第二个身体的坐标
        fx = "R";//初始方向向右
        score = 0;

        //把食物随机分布在界面上
        foodx = 25 + 25*random.nextInt(34);
        foody = 75 + 25*random.nextInt(24);


    }

//绘制面板,我们游戏中所有东西,都是用这个画笔来画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        //绘制静态的面板
        this.setBackground(Color.WHITE);
        Data.header.paintIcon(this,g,25,11);//头部广告栏画上去
        g.fillRect(25,75,850,600);//默认的游戏界面

        //画积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度"+length,750,30);
        g.drawString("分数"+score,750,50);

        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        //把小蛇画上去
        if (fx.equals("R")){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右,需要通过方向来判断

        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);

        }else if (fx.equals("U")){
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);

        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]); //身体
        }

        //游戏状态
        if(isStart == false){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("按下空格开始游戏",300,300);
        }
        if(isFail){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("失败,按下空格重新开始",300,300);
        }

    }
    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();//获得键盘按键是哪一个

        if(keyCode == KeyEvent.VK_SPACE){
            if(isFail){
                //重新开始
                isFail = false;
                init();
            }else{
                isStart = !isStart;//取反
            }
            repaint();
        }

        //小蛇移动
        if(keyCode == KeyEvent.VK_UP&& fx !="D"){
            fx = "U";
        }else if(keyCode == KeyEvent.VK_DOWN&& fx!="U"){
            fx = "D";

        }else if(keyCode == KeyEvent.VK_LEFT&&fx!="R"){
            fx = "L";

        }else if(keyCode == KeyEvent.VK_RIGHT&&fx!="L"){
            fx = "R";
        }
    }

    //事件监听---需要通过固定事件来刷新,1s = 10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart && isFail == false){//如果游戏是开始状态,就让小蛇动起来!
            //吃食物
            if(snakeX[0] == foodx && snakeY[0] == foody){
                length++;//长度 +1

                //分数加10
                score= score+10;
                //再次随机食物
                foodx = 25 + 25*random.nextInt(34);
                foody = 75 + 25*random.nextInt(24);
            }
            //右移
            for (int i = length -1; i > 0 ; i--) {//后一节移到前一节的位置 snakeX[1] = snakeX[0];
                snakeX[i] = snakeX[i -1];
                snakeY[i] = snakeY[i -1];

            }
            //走向
            if(fx.equals("R")){
                snakeX[0] = snakeX[0] + 25;
                if(snakeX[0] > 850){ snakeX[0] = 25; }//边界判断

            }else if(fx.equals("L")){
                snakeX[0] = snakeX[0] - 25;
                if(snakeX[0] < 25){ snakeX[0] = 850; }//边界判断
            }else if(fx.equals("U")){
                snakeY[0] = snakeY[0] - 25;
                if(snakeY[0] < 75){ snakeY[0] = 650; }//边界判断
            }else if(fx.equals("D")) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] >650) { snakeY[0] = 75; }//边界判断
            }

            //失败判定,撞到自己,就算失败
            for (int i = 1; i < length ; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] ==snakeY[i]){
                    isFail = true;
                }
            }

            repaint();//重画页面
        }
        timer.start();//定时器开启

    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }

}


数据类

package kuangshen.snake;

import javax.swing.*;
import java.net.URL;
//数据中心

public class Data {

    public static URL headerURL = Data.class.getResource("statics/header.png");
    public static ImageIcon header = new ImageIcon(headerURL);

    public static URL upURL = Data.class.getResource("statics/up.png");
    public static URL downURL = Data.class.getResource("statics/down.png");
    public static URL leftURL = Data.class.getResource("statics/left.png");
    public static URL rightURL = Data.class.getResource("statics/right.png");

    public static ImageIcon up = new ImageIcon(upURL);
    public static ImageIcon down = new ImageIcon(downURL);
    public static ImageIcon left = new ImageIcon(leftURL);
    public static ImageIcon right = new ImageIcon(rightURL);

    public static URL bodyURL = Data.class.getResource("statics/body.png");
    public static ImageIcon body = new ImageIcon(bodyURL);

    public static URL foodURL = Data.class.getResource("statics/food.png");
    public static ImageIcon food = new ImageIcon(foodURL);







}

  • GUI学习总结

在这里插入图片描述

在这里插入图片描述

常用码

Frame;											框架
Panel;											面板
setVisible;									    可见性true
setSize(x,x);									初始尺寸
setLocation(x,x);								初始位置,x,y
setBounds(x,x,x,x);								初始坐标+尺寸
setBackground(new color(x,x,x);   		    颜色,三基色
setResizable;								    大小是否可调,true,false
setLayout(new FlowLayout(FlowLayout.LEFT));		流式布局
frame.add(east,BorderLayout.EAST);				方向布局
frame.setLayout(new GridLayout(3,2));			表格布局
ActionListener;									监听器
TextField;										文本框
TextArea;										文本域
PasswordField;									密码框
Integer.parseInt();								String类转int类
paint;											画笔
MouseAdapter;									鼠标监听器
WindowListener;									窗口监听
KeyListener;									键盘监听
DefaultCloseOperation(WindowConstants.);		关闭事件(JFrame)
ContentPane;									容器(JFrame)
Layout;											容器自动定位(JFrame)
Button;											按钮
RadioButton;									单选按钮
ButtonGroup;CheckBox;										多选按钮
ComboBox;										下拉框
List;											列表框
Dialog;											对话框
Label;											标签
IconDemo;										图标
ImageIcon;										图片
Scroll;											滚动条
Timer;											定时器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值