Implementation of Java Graphical Interface

Implementation of Java Graphical Interface

1. The conditions required for the realization of the graphical interface

      Graphical interface form, graphical interface panel

2. The form of the interface

   2.1 What is a graphical interface form

         As shown in the figure, the black outer border is similar to the form of the graphical interface. To realize the graphical interface, the form is indispensable. Some people may ask if only the outer border is enough? Of course it is not possible, but also need The panel, otherwise there is only an outer frame and nothing inside, then the graphical interface can also be called a graphical interface.

   2.1 How to implement a graphical interface form in a Java program

         We all know that Java has a feature that everything can be called

         2.1.1 Form classes in Java

                  Among all the classes in Java, there is a class that can implement a graphical interface form. You know everything about inheritance. The subclass inherits the parent class and inherits all the public methods and properties declared by the parent class.

                  The JFrame class is a form class in Java. We now declare a class to inherit JFrame .

         2.1.2 Steps to customize the form class

                  1. Write a class inherited from JFrame

                  2. Write a construction method to initialize the properties of the form

         2.1.3 Implement a custom form class

                    E.g:

import javax.swing.JFrame;
//写一个类继承于 JFrame 
public class Frame extends JFrame{
    //构造方法
    public Frame(){
    //设置窗体的标题  来源于JFrame setTitle(标题);
        setTitle("图形化界面");
    //设置窗体的大小    setSize(宽度,高度);
        setSize(512,726);
    //设置窗体的位置 设置位置居中显示  setLocationRelativeTo(null);
        setLocationRelativeTo(null);
    //如果不想改变窗口的大小 setResizable(false);
        setResizable(false);
    //窗口关闭后所打开的程序并没有关闭,会大大影响运行内存,所以我们可以每次关闭的时候关闭程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //运行
        public static void main(String args[]){
            //创建Frame的对象
            Frame frame = new Frame();
            //显示窗口    true 是显示窗口,false 是隐藏窗口
            frame.setVisible(true);
        }
    }
}

 result

3. Graphical interface panel

   3.1 What is a graphical interface panel

          

The panel of the graphical interface is the red part within the black outer border. To realize the graphical interface, the panel is also an indispensable part. It is as important as the form, and it is indispensable!

    3.2 Panel class in Java

          Among all the classes in Java, there is a class that can implement a graphical interface panel. You know everything about inheritance. The subclass inherits the parent class and inherits all the public methods and properties declared by the parent class.

           The JPanel class is a form class in Java. We now declare a class to inherit JPanel .

    3.3 Steps to customize the panel

          1. Create a class inheriting JPanel 

          2. Create a construction method to initialize (determine) the properties of the panel

    3.4 Implement a custom panel class

    Add background color

import java.awt.Color;
import javax.swing.JPanel;
//创建一个类去继承Jpanel
public class Panel extends JPanel{
    //构造方法
    public Panel{
        //设置背景的颜色
        setBackground(Color.pink);
    }
}

   Adding a background image Adding a background image requires an APP class 

import java.awt.image.BufferedImage;
public class App{
    /**
       BufferedImage  Java 中用来表示图片类
       @param path 图片的路径
    */
    public static BufferedImage getImage(String path){
    // Java 中的IO流,输送数据的管道
    // 输入输出流
    // App.class  ,找到App类的路径(用到了反射的知识)
    // getResource()  获取资源
    try{
        BufferedImage img = ImageIO.read(App.class.getResource(path));
        //如果找到图片,就将图片返回
        return img;
    }catch(IOException e){
        // catch 如果找不到图片,就会捕获找不到图片的原因
        e.printStackTrace();
    }
        return null;
    }
}

Add a background image

import java.awt.Color;
import javax.swing.JPanel;
//创建一个类去继承Jpanel
public class Panel extends JPanel{
    //1.定义背景图
    BufferedImage bg;
    //构造方法
    public Panel{
        //设置背景的颜色
        setBackground(Color.pink);
        //初始化图片
        bg = App.getImage(图片在哪个路径 如:"/image/1.png");
        //重写Paint 方法
        @override
        public void paint(Graphics g){
        super.paint(g);
        //调用 方法 g.drawImage(),确定图片的坐标, 如 g.drawImage(图片, 图片的横坐标,图片的纵坐标,图片的宽,图片的高,null);
        g.drawImage(bg,0,0,512,726,null);
        }
    }
}

  To realize the panel, add it to the form

import javax.swing.JFrame;
//写一个类继承于 JFrame 
public class Frame extends JFrame{
    //构造方法
    public Frame(){
    //设置窗体的标题  来源于JFrame setTitle(标题);
        setTitle("图形化界面");
    //设置窗体的大小    setSize(宽度,高度);
        setSize(512,726);
    //设置窗体的位置 设置位置居中显示  setLocationRelativeTo(null);
        setLocationRelativeTo(null);
    //如果不想改变窗口的大小 setResizable(false);
        setResizable(false);
    //窗口关闭后所打开的程序并没有关闭,会大大影响运行内存,所以我们可以每次关闭的时候关闭程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //运行
        public static void main(String args[]){
            //创建Frame的对象
            Frame frame = new Frame();
            //创建Panel 对象
            Panel panel = new Panel();
            //显示窗口    true 是显示窗口,false 是隐藏窗口
            frame.setVisible(true);
            //将面板添加到JPrame
            frame.add(panel);
        }
    }
}

Note: The form and panel are inseparable together

result

The optimization of the panel will be detailed next time!

Guess you like

Origin blog.csdn.net/weixin_52011642/article/details/109599958