首页 > 编程语言 >GUI编程--1

GUI编程--1

时间:2022-09-28 12:02:20浏览次数:54  
标签:-- GUI 编程 Frame Button add 400 new frame

GUI编程--1

  • GUI是什么 (Graphical User Interface),即用户图形界面编程。

  • 怎么玩

  • 平时怎么运用

组件

  • 窗口

  • 弹窗

  • 面板

  • 文本框

  • 列表框

  • 按钮

  • 图片

  • 监听事件

1.简介

GUI技术的核心:Swing AWT,因为界面不美观,需要jre环境! 所以不流行。

为啥要学习?

  1. 写出一些想要的小工具。

  2. 工作时候,可能需要维护到swing界面,很低概率。

  3. 了解MVC架构,了解监听。

2.AWT

AWT(Abstract Window Toolkit),中文译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具。

2.1 AWT介绍

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

  2. 元素、窗口、按钮、文本框

  3. java.awt包

 

组件(component):

1. button(按钮)、TextArea(文本框)、Label(标签)....
1. 容器(Container):1中的需要放到里面

2.1. Window(窗口) : Frame(框架)、Dialog(弹窗)

2.2. Panel(面板):Applet(小程序)

2.2 窗口--Frame框架

1.第一个frame窗口

package com.ssl.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
   public static void main(String[] args) {
       //Frame
       Frame frame = new Frame("我的第一个java图形界面");

       //设置可见性
       frame.setVisible(true);

       //设置窗口大小
       frame.setSize(400,400);

       //设置背景颜色
       frame.setBackground(new Color(22, 222, 93));

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

       //设置大小固定
       frame.setResizable(false);
       
  }
}

问题:发现窗口关闭不掉!!

2.生成多个窗口

package com.ssl.lesson01;

import com.sun.beans.editors.ColorEditor;

import java.awt.*;

public class TestFrame2 {
   public static void main(String[] args) {
       new MyFrame(0,400,400,400,Color.red);
       new MyFrame(400,400,400,400,Color.blue);
       new MyFrame(800,400,400,400,Color.green);
       new MyFrame(1200,400,400,400,Color.orange);
  }
}

//利用继承写自己的方法。
class MyFrame extends Frame{

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

   public MyFrame(int x,int y,int w,int h,Color color){
       super("myFrame"+(++id));
       setBounds(x,y,w,h);
       //setLocation(x,y);
       //setSize(w,h);
       setVisible(true);
       setBackground(color);
       setResizable(false);
  }
}

2.3 窗口--面板Panel

面板需要添加到frame中,其中解决了窗口关闭的事件监听。

package com.ssl.lesson01;

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

//Panel 可以看成是一个空间,但是不能单独存在
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(13, 234, 92));

       //panel 设置坐标 相对于frame
       panel.setBounds(50,50,400,400);

       //panel 设置颜色
       panel.setBackground(Color.red);

       //将面板Panel放到frame上
       frame.add(panel);

       frame.setVisible(true);

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

2.4 三种布局管理器

frame.setLayout(null);
  • 流式布局Flow

package com.ssl.lesson01;

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.setLayout(new FlowLayout(FlowLayout.RIGHT)); //右

       frame.setSize(800,800);

       frame.setVisible(true);

       frame.add(button1);
       frame.add(button2);
       frame.add(button3);


  }
}
  • 东西南北中布局border

package com.ssl.lesson01;

import java.awt.*;

public class TestBorderLayout {
   public static void main(String[] args) {
       Frame frame =  new Frame();

       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.setVisible(true);
       frame.setSize(800,400);

  }
}
  • 表格布局Grid

package com.ssl.lesson01;

import java.awt.*;

public class TestGridLayout {
   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");
       Button button4 = new Button("button4");
       Button button5 = new Button("button5");
       Button button6 = new Button("button6");

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


       frame.add(button1);
       frame.add(button2);
       frame.add(button3);
       frame.add(button4);
       frame.add(button5);
       frame.add(button6);

       frame.setSize(400,400);

       frame.setVisible(true);

  }
}

 

2.5 小练习

 

 

package com.ssl.lesson01;

import java.awt.*;

public class TestTest {
   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");
       Button button4 = new Button("button4");
       Button button5 = new Button("button5");
       Button button6 = new Button("button6");
       Button button7 = new Button("button7");
       Button button8 = new Button("button8");
       Button button9 = new Button("button9");
       Button button10 = new Button("button10");

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

       Panel panel1 = new Panel();
       Panel panel2 = new Panel();

       frame.add(button1);
       frame.add(panel1);
       panel1.setLayout(new GridLayout(2,1));
       panel1.add(button2);
       panel1.add(button3);
       frame.add(button4);
       frame.add(button5);
       frame.add(panel2);
       panel2.setLayout(new GridLayout(2,2));
       panel2.add(button6);
       panel2.add(button7);
       panel2.add(button8);
       panel2.add(button9);
       frame.add(button10);

       frame.setSize(400,400);

       frame.setVisible(true);

  }
}

总结

  1. Frame是一个顶级窗口

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

  3. 布局管理器

标签:--,GUI,编程,Frame,Button,add,400,new,frame
From: https://www.cnblogs.com/ssl-study/p/16737512.html

相关文章

  • 手把手教你使用LabVIEW人工智能视觉工具包快速实现传统Opencv算子的调用(含源码)
    前言今天我们一起来使用LabVIEWAI视觉工具包快速实现图像的滤波与增强;图像灰度处理;阈值处理与设定;二值化处理;边缘提取与特征提取等基本操作。工具包的安装与下载方法可见......
  • REDIS面试题(7)
    20 Redis的大key问题  21如何解决Redis使用key命名阻塞的问题? 如果想要获取整个实例的所有key,建议使用SCAN命令代替。客户端通过执行SCAN$cursorCOUNT$count......
  • 物理地址(MAC地址)是什么?
    物理地址是一种标识符,用来标记网络中的每个设备。同现实生活中收发快递一样,网络内传输的所有数据包都会包含发送方和接收方的物理地址。由于网络设备对物理地址的处理能力......
  • Java_day01
    Java的特性和优势-简单性-面向对象-可移植性-高性能-分布式-动态性-多线程-安全性-健壮性Java成功的原因个人认为在于语言本身的优势之外还有个人电脑的普及......
  • python添加模块路径的三种方法
    之前对macos系统自带的python进行了升级,结果发现新安装的python的site-packages目录并没有加到python的系统路径中,所以在使用其他库时发现出现了缺少模块的错误。查看pyt......
  • 计算机学习心得
    先看个小故事。有一天我问我媳妇:你觉得操作系统复杂么?我媳妇想都没想就来了一句:复杂!我又问:为什么觉得操作系统复杂?或者操作系统复杂在哪里?我媳妇眼睛转了两圈之后说:不......
  • SHAREit(国内茄子快传):让用户能够发掘有价值的优质本地化内容
    伴随着国内互联网市场逐渐饱和,大批企业如火如荼的挖掘海外市场,以寻求新的增长点。作为穿越周期的新⼀代全球化互联⽹科技公司,茄子科技(海外SHAREitGroup)在新兴市场寻找蓝海的......
  • 制造企业使用数字化安灯系统工具的原因是什么?
    制造企业使用数字化安灯系统工具就是采用物联网的方式解决工厂数据采集实现对工厂某些数据进行感知的一种方式。工厂在进行智能化管理或数字化转型过程中需要将设备信号、......
  • 实验3:OpenFlow协议分析实践
    基础要求一、拓扑文件#!/usr/bin/envpythonfrommininet.netimportMininetfrommininet.nodeimportController,RemoteController,OVSControllerfrommininet.......
  • JS 算法
    1、js统计一个字符串出现频率最高的字母/数字letstr='absafdaf234234323232'leta=[...str]//alert(ainstanceofArray)conststrChar=......