编程语言
首页 > 编程语言> > 1.1 GUI编程

1.1 GUI编程

作者:互联网

1、组件

  1. 窗口

  2. 弹窗

  3. 面板

  4. 文本框

  5. 列表框

  6. 按钮

  7. 图片

  8. 监听事件

  9. 鼠标

  10. 键盘

2、简介

Gui的核心技术: Swing () AWT (-Abstact Window Toolkit)

  1. 因为界面不美观

  2. 需要 JRE 环境

    既然GUI属于是过去式,为什么要学习?

    1. MVC基础,了解监听

    2. 写出自己想要的小工具

    3. 工作时候需要用到维护Swing,版本比较老,概率比较小

3、AWT

1、AWT介绍(抽象窗口工具包)

  1. 包含了很多的类和接口 GUI (图形用户界面)

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

  3. Java.awt

(1、1.1 )

 

2、组件和容器

1.1 Frame介绍

Frame 是窗口设置

(1、1.2 )

 

这个程序窗口不能够关闭因为没有设置

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon01

(1、1.3 )

 

下面是封装的方法体,可以直接构造多个窗体窗口

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon02

1.2 Panel面板介绍

Panel可以看作是一个单独的空间,但是不能单独存在,要放在组件中。

(1、1.4 )

 

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon03

1.3 布局管理器

  1. 流式布局(从左到右)

(1、1.5 )

 

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon04

  1. 东西南北中(上下结构)

(1、1.6 )

 

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon5

  1. 表格布局(三行两列)

(1、1.7 )

 

具体参考 : D: \ 项目 \ untitled 1 \ src \ main \ java \ GUI \ Demon06

1.4 事件监听器

事件监听就是监听鼠标键盘等,当做出操作后显示。

  1. 鼠标监听

package Listener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
​
public class Demon01 {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
class MyFrame extends Frame{
      //画画需要需要画笔,监听当前鼠标的位置,需要集合来存储这些画笔的点
      ArrayList points;
​
    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);
        //存取鼠标点
        points = new ArrayList<>();
​
        setVisible(true);
        //鼠标监听器监听窗口
        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.cyan);
            g.fillOval(point.x,point.y,10,10);
        }
    }
​
    //添加一个点添加到界面上
    public void addPaint(Point point){
        points.add(point);
    }
​
    //适配器模式
    private class MyMouseListener extends MouseAdapter {
        //鼠标  按下 弹起 按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame  frame = (MyFrame) e.getSource();
            //这个我们点击的时候就会在界面上产生一个点
​
            //这个点就是鼠标的点
            frame.addPaint(new Point(e.getX(),getY()));
​
            //new Point(e.getX(),e.getY());
            frame.repaint();
​
        }
    }
}

  1. 窗口监听

package Listener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
​
public class Demon02 {
    public static void main(String[] args) {
    new WindowFrame();
    }
}
​
class WindowFrame extends Frame {
    public WindowFrame() {
​
        setVisible(true);
        pack();
​
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
​

  1. 键盘监听

package Listener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
​
public class Demon03 {
    public static void main(String[] args) {
    new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
​
        setBounds(1,2,300,400);
        setVisible(true);
​
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
​
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_W){
                    System.out.println("你按下了向上键");
                }
            }
        });
    }
}
​

1.5 画笔

package GUI;
​
import java.awt.*;
​
public class Demon10 {
    public static void main(String[] args) {
         new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame {
    public void loadFrame(){
​
        setBounds(400,200,500,600);
        setVisible(true);
    }
​
    @Override
    public void paint(Graphics g) {
       g.setColor(Color.red);
       g.drawLine(45,45,500,500);
​
       g.setColor(Color.green);
       g.draw3DRect(45,45,45,45,true);
​
       g.setColor(Color.orange);
       g.fillOval(45,45,50,50);
    }
}
​

4、Swing 之 JFrame 窗体

1、窗口

package JFrame;
import javax.swing.*;
import java.awt.*;
​
public class Demon01 {
​
    //init();初始化
    public void init(){
​
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setBounds(50,50,400,400);
        frame.setVisible(true);
​
        //因为JFrame是一个容器 所以要提取出来才能再给他一个颜色
        Container contentPane = frame.getContentPane();
        contentPane.setBackground(Color.red);
​
        //设置文字
        JLabel jLabel = new JLabel("欢迎来到德莱联盟");
        frame.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
​
        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
​
​
    public static void main(String[] args) {
        //建立一个窗口
        new Demon01().init();
    }
}

2、弹窗

package JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class Demon02 extends JFrame {
​
    public Demon02()  {
​
        setBounds(450,450,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
​
        //容器
        Container container = this.getContentPane();
        container.setBackground(Color.red);
​
        //绝对定位
        container.setLayout(null);
​
        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(50,50,300,300);
​
        //点击按钮,弹出弹窗
        container.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            //弹窗
                new MyDialogDemo();
            }
        });
​
    }
​
    class MyDialogDemo extends JDialog{
        public MyDialogDemo() {
​
           this.setVisible(true);
           this.setBounds(100,100,500,500);
​
            Container container= this.getContentPane();
            container.setLayout(null);
            container.add(new Label("开始"));
​
        }
    }
​
    
    public static void main(String[] args) {
        new Demon02();
​
    }
}

3、面板

package JFrame;
import javax.swing.*;
import java.awt.*;
​
public class Demon03 extends JFrame {
    public Demon03 (){
        Container container = this.getContentPane();
​
        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("欢迎");
        
        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        container.add(scrollPane);
​
        this.setVisible(true);
        this.setBounds(50,50,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new Demon03();
    }
}
​

4、按钮

  1. 单选按钮

  2. 复选按钮

5、列表

  1. 下拉框

package JFrame;
import javax.swing.*;
import java.awt.*;
​
public class Demon05 extends JFrame {
    public Demon05()  {
​
        Container container = this.getContentPane();
​
        JComboBox status = new JComboBox<>();
​
        status.addItem(null);
        status.addItem("狗");
        status.addItem("住");
        status.addItem("人");
​
        container.add(status);
​
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(50,50,500,500);
​
​
    }
​
    public static void main(String[] args) {
    new Demon05();
​
    }
}
​

  1. 列表框

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

public class Demon06 extends JFrame {

    public Demon06(){
        Container container = this.getContentPane();

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

        container.add(list);

        this.setVisible(true);
        this.setBounds(50,50,600,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

6、文本框

  1. 文本框

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

public class Demon07 extends JFrame {
    public Demon07 (){

        Container container = this.getContentPane();

        //文本框内容
        JTextField jTextField = new JTextField("hello");
        JTextField jTextField2 = new JTextField("word",20);
        //用add设置文本框位置
        container.add(jTextField,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);

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

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

  1. 文本域

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

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

        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("欢迎");

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

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

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

  1. 密码框

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

public class Demon08 extends JFrame {
    public Demon08 (){

        Container container = this.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        container.add(jPasswordField);


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

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

标签:awt,java,1.1,GUI,编程,void,import,new,public
来源: https://blog.csdn.net/m0_53879048/article/details/123060860