GUI编程学习笔记——day01

前言:告诉大家应该怎么学?

这是什么?

它怎么玩?

该如何在我们平时运用?

组件

窗口

弹窗

面板

文本框

列表框

按钮

图片

监听事件

鼠标

键盘事件

破解工具

一、是什么

GUI是图形界面编程

GUI的核心技术:Swing AWT

GUI缺点:界面不美观;需要jar环境

二、为什么

为什么我们要学习

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

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

了解MVC架构,了解监听!

三、怎么做 1、AWT 1.1 AWT介绍

包含了很多类和接口!

元素:窗口、按钮、文本框

1.2 组件和容器 1.2.1. Frame(容器) package com.gui; import java.awt.*; public class TestFrame { public static void main(String[] args) { Frame frame =new Frame("我的第一个JAVA图像界面窗"); frame.setVisible(true); frame.setSize(200,200); frame.setBackground(Color.BLUE); frame.setLocation(200,200); frame.setResizable(false); } }

GUI编程学习笔记——day01

展示多个窗口

package com.gui; import java.awt.*; public class TestFrame02 { //展示多个窗口 public static void main(String[] args) { MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.BLUE); MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.GREEN); MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.MAGENTA); MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.YELLOW); } } 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); } }

GUI编程学习笔记——day01

1.2.2. Panel(面板) package com.gui; 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(Color.GREEN); //相对于frame的面板坐标 panel.setBounds(50,50,400,400); panel.setBackground(Color.BLUE); //将面板添加到窗口 frame.add(panel); //设置可见性 frame.setVisible(true); //监听事件,监听窗口关闭事件 System.exit(0) //适配器模式 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0);//结束程序 } }); } }

GUI编程学习笔记——day01

1.2.3. 布局管理器

流式布局

package com.gui; import com.sun.media.jfxmedia.events.NewFrameEvent; import java.awt.*; public class TestLayout { 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"); //设置为流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setVisible(true); frame.setSize(200,200); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); } }

GUI编程学习笔记——day01

东西南北中

package com.gui; import java.awt.*; public class TestLayoutBorder { 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.setSize(200,200); frame.setVisible(true); } }

GUI编程学习笔记——day01

表格布局

package com.gui; import java.awt.*; public class TestLayoutGrid { 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"); //设置表格布局 3*2 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(); frame.setVisible(true); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdpjg.html