GUI编程学习陆续更新
2020-07-20 04:17
453 查看
GUI
组件
- 窗口 - 弹窗 - 面板 - 文本框 - 列表框 - 按钮 - 图片 - 监听事件 - 鼠标
1、简介
GUI 核心 – Swing AWT
为什么要学习:—学习MVC
1、可以写自己想要的小工具
2、可以写逆向
3、了解mvc 了解监听
2、AWT
元素:窗口、按钮、文本框、容器等等
2.1 组件
组件(Component):
— button、textarea、lable…
通过add 放到容器中
— 容器:Container
窗口:Window
1、Frame,2、Dialog(弹窗)
面板:Panel:
Applet
2.2组件和容器
package com.kuang.lesson01; import java.awt.*; //GUI 的第一个界面 public class TestFrame { public static void main(String[] args) { // 直接点击查看源码,怎么使用该对象 Frame frame = new Frame("我的第一个Java图像窗口"); //需要设置可见性,直接通过对象点的方式 frame.setVisible(true); // 设置窗口大、小 frame.setSize(400,400); // 设置背景颜色 frame.setBackground(new Color(190, 96, 152)); //窗口弹出的初始位置 frame.setLocation(700,300); //设置大小固定,默认是true frame.setResizable(false); } }
问题:发现窗口关闭不了
多个窗口
package com.kuang.lesson01; import java.awt.*; public class Testframe2 { 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.green); Myframe myframe4 = new Myframe(300, 300, 200, 200, Color.pink); } } 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.3 panel 面板
解决了窗口关闭的问题
package com.kuang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class TestPanel { public static void main(String[] args) { //panel 是一个空间不能单独存在需要放在frame里面 Frame frame = new Frame(); //设置布局 Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(217, 117, 45)); //panel 设置坐标,相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(111, 139, 209)); //frame.add() frame.add(panel); //设置完成需要显现 frame.setVisible(true); //窗口还是无法关闭,需要解决窗口关闭的问题,需要添加监听事件,监听窗口是否关闭 -- System.exit(0); // 监听事件需要重写的方法很多,所有采用适配器设计模式 frame.addWindowListener(new WindowAdapter() { //点击关闭的时候需要做的事情 @Override public void windowClosing(WindowEvent e) { //需要做结束程序 System.exit(0); } }); } }
2.4 设置布局
2.5三种布局方式
- 流式布局
package com.kuang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件--按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); //设置为流式布局.这里就是流式布局,左右上下 frame.setLayout(new FlowLayout()); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //设置界面大小 frame.setSize(200,200); //添加按钮 frame.add(button1); frame.add(button2); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
- 东西南北中
package com.kuang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button North = new Button("North"); Button East = new Button("East"); Button West = new Button("West"); Button South = new Button("South"); Button Center = new Button("Center"); //添加按钮 frame.add(North,BorderLayout.NORTH); frame.add(East,BorderLayout.EAST); frame.add(West,BorderLayout.WEST); frame.add(South,BorderLayout.SOUTH); frame.add(Center,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
- 表格布局(三行两列)
package com.kuang.lesson01; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); 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.pack(); //java 函数。自动布局 frame.setVisible(true); } }
作业:利用现在所学习的知识:画一个下面的图形界面的按钮:
分析:
frame :面框 panel :画板
- 第一种:把它看成4个面板,大面板套小面板这样去做就需要用4个面板,套在一个frame 里面
package com.kuang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Work2 { public static void main(String[] args) { // 第一步先画一个框 Frame frame = new Frame("work"); frame.setSize(400,300); frame.setLocation(300,400); frame.setBackground(new Color(210, 86, 110)); 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)); //上半部 p1.add(new Button("East-1"),BorderLayout.EAST); p1.add(new Button("West-1"),BorderLayout.WEST); p2.add(new Button("p2-btn-1")); p2.add(new Button("p2-btn-2")); p1.add(p2,BorderLayout.CENTER); //下半部 p3.add(new Button("East-2"),BorderLayout.EAST); p3.add(new Button("West-2"),BorderLayout.WEST); for (int i = 0; i < 4; i++) { p4.add(new Button("for" + i)); } p3.add(p4,BorderLayout.CENTER); frame.add(p1); frame.add(p3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
- 第二种:先把它看成表格布局两行一列,然后就是两个按钮 + 1一个面板,上下都是这个布局,唯一不同的就是更改这个中间面板的布局,中间面板的布局在根据列表布局去改变
package com.kuang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class WorkLayout { public static void main(String[] args) { Frame frame = new Frame("习题"); Panel p1 = new Panel(); Panel p2 = new Panel(); Button b1 = new Button("b1"); Button b2 = new Button("b2"); Button b3 = new Button("b3"); Button b4 = new Button("b4"); Button b5 = new Button("b5"); Button b6 = new Button("b6"); Button b7 = new Button("b7"); Button b8 = new Button("b8"); Button b9 = new Button("b9"); Button b10 = new Button("b10"); frame.setLayout(new GridLayout(2,1)); frame.add(b1); frame.add(p1); frame.add(b2); frame.add(b3); frame.add(p2); frame.add(b4); p1.setLayout(new GridLayout(2,1)); p1.add(b5); p1.add(b6); p2.setLayout(new GridLayout(2,2)); p2.add(b7); p2.add(b8); p2.add(b9); p2.add(b10); frame.setSize(600,400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
3、总结
1、Frame 是一个顶级窗口
2、Panel 无法单独显示,必须添加到某个容器中
3、 布局管理
1、流式
2、东西南北中
3、表格
4、大小、定位、颜色、可见性、、、、监听
4、事件
当某个事件发生的时候? 该做什么事情、
package com.kuang.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("btn1"); //因为addActionListener(),需要一个ActionListener,所以我们需要构造ActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button,BorderLayout.CENTER); frame.pack(); frame.setSize(400,400); frame.setVisible(true); windowClose(frame); // 应用 -- 网络编程, 按下一个按钮 触发一个事件发送一个消息 } //关闭窗口抽象成一个方法 private static void windowClose(Frame frame) { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { System.out.println("aaa"); } }
两个按钮,控制一个事件、包括 开始-关闭
package com.kuang.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 TestActionEventTwo { public static void main(String[] args) { // 两个按钮实现一个监听 -- 开始 停止 Frame frame = new Frame("开始-关闭"); Button button1 = new Button("start"); Button button2 = new Button("stop"); button1.setActionCommand("start"); MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor); button2.addActionListener(myMonitor); frame.add(button1,BorderLayout.NORTH); frame.add(button2,BorderLayout.SOUTH); frame.setSize(300,300); frame.setVisible(true); windowClose(frame); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyMonitor implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { System.out.println("点击了按钮:msg==>" + e.getActionCommand()); } }
相关文章推荐
- GUI编程学习陆续更新(二)
- 亲测编程相关的工具的学习网址(陆续更新)
- java基础学习GUI编程
- Java学习心得之GUI编程(二)
- java基础学习总结——GUI编程(一)
- java基础学习总结——GUI编程(二)
- 算法模板学习专栏之总览(会慢慢陆续更新ing)
- C++学习博客&书本推荐(自身学习过程中陆续更新)
- HeadFirstJava学习心得——javaGUI编程
- 内存编程学习(不间断更新)
- 学编程,学单词.....在学习中积累自己的单词(不断更新__ing)
- python自动化测试学习遇到的编程问题:*arg是啥,python自动化测试将测试结果写入excel中【持续更新】
- java基础学习总结——GUI编程(二)
- less.css基础学习,陆续更新中
- 学习中遇到的小技巧 二 (陆续更新……)
- 谈一谈网络编程学习经验(06-08更新)
- 谈一谈网络编程学习经验(06-08更新)
- 学习matlab GUI编程的一点点所得
- 谈一谈网络编程学习经验(06-08更新)
- wxWidgets GUI 编程学习第一天