当前位置: 首页 > news >正文

动态网站制作教程seo网站优化推荐

动态网站制作教程,seo网站优化推荐,广州网站建设制作价格,网站首页模板AWT 课程:1、GUI编程简介_哔哩哔哩_bilibili 一.介绍 包含了很多类和接口!GUI!元素:窗口、按钮、文本框java.awt 二.窗口 1.构造 2.方法 // 实例化frame类Frame frame new Frame("这个一个框");// 设置可见性frame.…

 AWT

课程:1、GUI编程简介_哔哩哔哩_bilibili

一.介绍 

  1. 包含了很多类和接口!GUI!
  2. 元素:窗口、按钮、文本框
  3. java.awt

二.窗口

1.构造

2.方法 

        // 实例化frame类Frame frame = new Frame("这个一个框");// 设置可见性frame.setVisible(true);// 设置窗口大小frame.setSize(500, 500);// 设置窗口背景颜色,实例化Color类frame.setBackground(new Color(50, 70, 300));// 设置窗口的弹出位置frame.setLocation(300, 300);// 设置窗口大小不可拖动改变frame.setResizable(false);

效果:

问题:无法关闭窗口,停止java程序 

 多个frame

我们先写一个frame的封装类:

import java.awt.*;public class MyFrame extends Frame {// 静态序号,计算窗口个数static int id = 0;// 使用构造函数初始化弹出属性// 初始化的属性有大小,弹出位置,背景颜色public MyFrame(int x, int y, int w, int h, Color color) {// 计算窗口个数super("myFrame"+(++id));// 设置窗口可视化setVisible(true);// 设置窗口大小与弹出位置,使用Bounds可以同时设置setBounds(x, y, w, h);// 设置窗口背景颜色setBackground(color);}
}

 再写测试类:

public class Application {public static void main(String[] args) {MyFrame myFrame1 = new MyFrame(100, 100, 400, 400, Color.black);MyFrame myFrame2 = new MyFrame(500, 100, 400, 400, Color.red);MyFrame myFrame3 = new MyFrame(100, 500, 400, 400, Color.blue);MyFrame myFrame4 = new MyFrame(500, 500, 400, 400, Color.green);}
}

效果:

三.panel 面板  

 1.构造

2.方法 

  • frame中放置一个固定面板
            // 先完成窗口的设置Frame frame = new Frame();frame.setVisible(true);frame.setBounds(300, 300, 500, 500);frame.setBackground(new Color(128, 253, 190));// 将组件的布局设置为空布局,使你能够手动控制组件的位置和大小。frame.setLayout(null);// 面板的设置与窗口一样,但是面板在窗口里面要注意位置与大小// Panel是面板,可以放在窗口中,不能单独存在Panel panel = new Panel();frame.setVisible(true);panel.setBounds(50, 50, 400, 400);panel.setBackground(new Color(238, 47, 142));// 添加面板进窗口,frame.add(panel);

    效果:

 解决无法关闭窗口停止java程序,可以使用下面的方法看不懂跳过后面还会再讲

package com.demo.panel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class PanelTest {public static void main(String[] args) {panelTest();}public static void panelTest() {// 先完成窗口的设置Frame frame = new Frame();frame.setVisible(true);frame.setBounds(300, 300, 500, 500);frame.setBackground(new Color(128, 253, 190));// 将组件的布局设置为空布局,使你能够手动控制组件的位置和大小。frame.setLayout(null);// 面板的设置与窗口一样,但是面板在窗口里面要注意位置与大小// Panel是面板,可以放在窗口中,不能单独存在Panel panel = new Panel();frame.setVisible(true);panel.setBounds(50, 50, 400, 400);panel.setBackground(new Color(238, 47, 142));// 添加面板进窗口,frame.add(panel);// 设置窗口关闭,可以自己写一个窗口监听// 监听事件:监听窗口关闭事件 关闭:System.exit(0)// 适配器模式:用Adapter类重写方法    只有一个方法的监听接口没有适配器frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// 窗口点击关闭的时候需要做的事,退出程序System.exit(0);}});}
}

四.四种布局管理器与按钮

按钮的构造器 

Constructor and Description
Button()

构造一个带有标签的空字符串的按钮。

Button(String label)

构造具有指定标签的按钮。

方法

Modifier and TypeMethod and Description
voidaddActionListener(ActionListener l)

添加指定的动作侦听器以从此按钮接收动作事件。

voidaddNotify()

创建按钮的对等体。

AccessibleContextgetAccessibleContext()

获取 AccessibleContext与此相关 Button 。

StringgetActionCommand()

返回此按钮触发的操作事件的命令名称。

ActionListener[]getActionListeners()

返回在此按钮上注册的所有动作侦听器的数组。

StringgetLabel()

获取此按钮的标签。

<T extends EventListener>
T[]
getListeners(类<T> listenerType)

返回当前注册为 FooListener的所有对象的数组,在此 Button 。

protected StringparamString()

返回一个代表此 Button状态的字符串。

protected voidprocessActionEvent(ActionEvent e)

通过将此按钮分派到任何已注册的 ActionListener对象来处理此按钮上发生的操作事件。

protected voidprocessEvent(AWTEvent e)

处理此按钮上的事件。

voidremoveActionListener(ActionListener l)

删除指定的动作侦听器,使其不再从此按钮接收到动作事件。

voidsetActionCommand(String command)

设置此按钮触发的操作事件的命令名称。

voidsetLabel(String label)

将按钮的标签设置为指定的字符串。

1.空布局(绝对布局) 

前面我们使用的是空布局,如下 

// 将组件的布局设置为空布局,使你能够手动控制组件的位置和大小。frame.setLayout(null);

2.流式布局

从左到右从上到下

public static void main() {Frame frame = new Frame();// 按钮Button button1 = new Button("按钮1");Button button2 = new Button("按钮2");Button button3 = new Button("按钮3");// FlowLayout:流式布局 从左到右从上到下// frame.setLayout(new FlowLayout(FlowLayout.LEFT));// 向左// frame.setLayout(new FlowLayout()); // 中间frame.setLayout(new FlowLayout(FlowLayout.RIGHT));// 向右// 设置可见度与大小frame.setVisible(true);frame.setSize(200,200);// 添加按钮frame.add(button1);frame.add(button2);frame.add(button3);}

3.空间布局

东西南北中 

 public static void main() {Frame frame1 = new Frame("BorderLayout布局");// 组件--按钮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");// 设置可见度与大小frame1.setVisible(true);frame1.setSize(200,200);// 添加按钮// BorderLayout:空间布局 东西南北中 南北是拉满的 有南北的情况下左右不一定拉满了frame1.add(east,BorderLayout.EAST);frame1.add(west,BorderLayout.WEST);frame1.add(south,BorderLayout.SOUTH);frame1.add(north,BorderLayout.NORTH);frame1.add(center,BorderLayout.CENTER);}

4.网格布局

几行几列 根据多少,列会产生变化 

 public static void main() {Frame frame2 = new Frame("GridLayout布局");//组件--按钮Button button1 = new Button("1");Button button2 = new Button("2");Button button3 = new Button("3");Button button4 = new Button("4");Button button5 = new Button("5");Button button6 = new Button("6");//设置布局GridLayout  new GridLayout(行,列,行间隔,列间隔)frame2.setLayout(new GridLayout(3,2));// 设置可见度frame2.setVisible(true);// 添加按钮frame2.add(button1);frame2.add(button2);frame2.add(button3);frame2.add(button4);frame2.add(button5);frame2.add(button6);// 使用pack()方法可以自动调节大小和布局大小frame2.pack();}

5.练习 

请用30分钟做以下内容 

思路讲解:6、课堂练习讲解及总结_哔哩哔哩_bilibili 

 public static void main() {// 一.外层窗口Frame frame3 = new Frame();// 设置可见度,弹出位置,大小,颜色frame3.setVisible(true);frame3.setBounds(200,200,200,300);frame3.setBackground(Color.BLACK);// 设置布局frame3.setLayout(new GridLayout(2,1));// 二.设置四个面板// 上面的面板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("上左"),BorderLayout.WEST);p1.add(new Button("上右"),BorderLayout.EAST);p2.add(new Button("上中一"));p2.add(new Button("上中二"));p1.add(p2,BorderLayout.CENTER);// 四.设计下面的面板p3.add(new Button("下左"),BorderLayout.WEST);p3.add(new Button("下右"),BorderLayout.EAST);for (int i = 0; i < 4; i++) {p4.add(new Button("for-"+i));}p3.add(p4,BorderLayout.CENTER);// 五.将上下拼凑起来frame3.add(p1);frame3.add(p3);frame3.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// 窗口点击关闭的时候需要做的事,退出程序System.exit(0);}});}

由于中文的字符编码问题,我的结果中的中文没有显示出来,全部变成了正方形

 五.事件监听

Modifier and TypeMethod and Description
voidactionPerformed(ActionEvent e)

发生动作时调用。

 1.按钮监听

package com.demo.panel;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 ActionEventTest {public static void main(String[] args) {actionEventTest();}// 事件监听  按下按钮 触发一些事件public static void actionEventTest() {// 1.窗口与布局Frame frame = new Frame("开始-停止");frame.setVisible(true);frame.setLayout(new GridLayout(2,1));// 2.按钮Button button1 = new Button("start");Button button2 = new Button("stop");// 4.设置按钮信息button1.setActionCommand("start");// 5.实例化myMonitor与,两个按钮同用一个事件myMonitor myMonitor = new myMonitor();button1.addActionListener(myMonitor);button2.addActionListener(myMonitor);// 6.按钮添加到窗口frame.add(button1);frame.add(button2);frame.pack();// 8.调用关闭窗口事件windowClose(frame);}// 7.关闭窗口的事件,单独写成一个方法private static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}}// 3.按钮事件监听器,需要实现ActionListener接口,并重新actionPerformed方法
class myMonitor implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("按钮button被点击了:"+e.getActionCommand());}
}

 2.输入框事件监听与文本框

构造方法
Constructor and Description
TextField()

构造一个新的文本字段。

TextField(int columns)

构造具有指定列数的新的空文本字段。

TextField(String text)

构造一个使用指定文本初始化的新文本字段。

TextField(String text, int columns)

构造一个新的文本字段,并使用指定的文本进行初始化,以便显示,并且足够宽以容纳指定的列数。

                                                               方法摘要

Modifier and TypeMethod and Description
voidaddActionListener(ActionListener l)

添加指定的动作侦听器以从此文本字段接收动作事件。

voidaddNotify()

创建TextField的对等体。

booleanechoCharIsSet()

指示此文本字段是否具有用于回显的字符集。

AccessibleContextgetAccessibleContext()

获取与此TextField关联的AccessibleContext。

ActionListener[]getActionListeners()

返回在此文本域中注册的所有操作侦听器的数组。

intgetColumns()

获取此文本字段中的列数。

chargetEchoChar()

获取要用于回显的字符。

<T extends EventListener>
T[]
getListeners(类<T> listenerType)

返回当前注册的所有对象的数组 FooListener在这个S TextField 。

DimensiongetMinimumSize()

获取此文本字段的最小尺寸。

DimensiongetMinimumSize(int columns)

获取具有指定列数的文本字段的最小尺寸。

DimensiongetPreferredSize()

获取此文本字段的首选大小。

DimensiongetPreferredSize(int columns)

使用指定的列数获取此文本字段的首选大小。

DimensionminimumSize()已弃用

从JDK 1.1版开始,替换为getMinimumSize() 。

DimensionminimumSize(int columns)已弃用

截至JDK 1.1版,由getMinimumSize(int) 。

protected StringparamString()

返回表示此 TextField的状态的字符串。

DimensionpreferredSize()已弃用

从JDK 1.1版开始,由getPreferredSize() 。

DimensionpreferredSize(int columns)已弃用

截至JDK 1.1版,由getPreferredSize(int) 。

protected voidprocessActionEvent(ActionEvent e)

通过将这些事件发送到任何已注册的 ActionListener对象来处理在此文本字段上发生的操作事件。

protected voidprocessEvent(AWTEvent e)

处理此文本字段上的事件。

voidremoveActionListener(ActionListener l)

删除指定的动作监听器,使其不再从此文本字段接收到动作事件。

voidsetColumns(int columns)

设置此文本字段中的列数。

voidsetEchoChar(char c)

设置此文本字段的回音字符。

voidsetEchoCharacter(char c)已弃用

从JDK 1.1版开始,替换为setEchoChar(char) 。

voidsetText(String t)

将此文本组件呈现的文本设置为指定的文本。

package com.demo.panel;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class TestTest01 {public static void main(String[] args) {// 调用构造器new MyFrame();}
}class  MyFrame extends Frame{// 构造器(无参)public MyFrame()  {//设置窗口的标题//利用super访问父类构造方法super("请输入密码");TextField textField = new TextField();// 因为继承了Frame,使用方法可以直接调用add(textField);//监听这个文本框输入的文字textField.addActionListener(new MyActionListenerTextField());// 设置替换编码textField.setEchoChar('*');// 设置窗口setBounds(200,200,200,200);setVisible(true);}
}class MyActionListenerTextField implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {//获得一些资源,返回一个对象  向下转型获取事件源e.getSource()的返回值是objectTextField textField = (TextField) e.getSource();// 获得输入框的文本System.out.println(textField.getText());// 换行清空与后台显示密码textField.setText("");}
}

3.练习简单加法计算器和标签

构造方法
Constructor and Description
Label()

构造一个空标签。

Label(String text)

用指定的文本字符串构造一个新的标签,左对齐。

Label(String text, int alignment)

构造一个新的标签,以指定的对齐方式显示指定的文本字符串。

 方法

voidaddNotify()

创建此标签的对等体。

AccessibleContextgetAccessibleContext()

获取与此Label相关联的AccessibleContext。

intgetAlignment()

获取此标签的当前对齐方式。

StringgetText()

获取此标签的文本。

protected StringparamString()

返回表示此 Label的状态的字符串。

voidsetAlignment(int alignment)

将此标签的对齐方式设置为指定的对齐方式。

voidsetText(String text)

将此标签的文本设置为指定的文本。

package com.demo.panel;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class CalcTest {public static void main(String[] args) {new MyCalcTest().loadFrame();}
}class MyCalcTest extends Frame{// 调用父类构造设置标题public MyCalcTest() {super("简单加法计算器");}TextField num1,num2,num3;public void loadFrame() {// 设置小组件num1 = new TextField(10);num2 = new TextField(10);num3 = new TextField(20);Button button = new Button("=");Label label = new Label("+");// 布局setLayout(new FlowLayout());setVisible(true);pack();add(num1);add(label);add(num2);add(button);add(num3);// 监听按钮button.addActionListener(new MyCalcListener());}// 内部类实现监听public class MyCalcListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {// 实现加法,使用包装类将String类型转换成intint n1 = Integer.parseInt(num1.getText());int n2 = Integer.parseInt(num2.getText());// 两数相加,回车输出结果num3.setText(""+(n1+n2));}}
}

4.画笔paint 

package com.demo.panel;import java.awt.*;public class PaintTest {public static void main(String[] args) {new MyPaintTest().loadPaint();}
}class MyPaintTest extends Frame{// 画板public void loadPaint() {setTitle("Paint");setBounds(200,200,600,500);setVisible(true);}// 画笔,重写paint()方法//画笔方法 paint创建窗口后,默认只执行一次@Overridepublic void paint(Graphics g) {// 选择颜色g.setColor(Color.GREEN);// 选择图像g.fillOval(100,100,100,100);// 养成习惯,画笔用完,将它还原成最初的颜色g.setColor(Color.black);}
}

5.鼠标监听与Point

Modifier and TypeMethod and Description
voidmouseClicked(MouseEvent e)

在组件上单击(按下并释放)鼠标按钮时调用。

voidmouseEntered(MouseEvent e)

当鼠标进入组件时调用。

voidmouseExited(MouseEvent e)

当鼠标退出组件时调用。

voidmousePressed(MouseEvent e)

在组件上按下鼠标按钮时调用。

voidmouseReleased(MouseEvent e)

在组件上释放鼠标按钮时调用。

构造方法
Constructor and Description
Point()

构造并初始化坐标空间原点(0,0)的点。

Point(int x, int y)

构造并初始化坐标空间中指定的 (x,y)位置的点。

Point(Point p)

构造和初始化与指定的 Point对象相同位置的点。

                                                                       方法 

doublegetX()

返回这个 Point2D的X坐标在 double精度。

doublegetY()

返回这个 Point2D的Y坐标在 double精度。

package com.demo.panel;import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;public class MouseListenerTest {public static void main(String[] args) {new MyFrame2("画画");}
}class MyFrame2 extends Frame{// 需要一个集合来存储画笔画出点的(X,Y)坐标ArrayList<Point> points;// 设置画板(窗口)public MyFrame2(String s)  {// 调用父类的构造器,参数为名字super(s);setBounds(200,200,400,300);setVisible(true);// 存储点的坐标points = new ArrayList<>();// 鼠标监听器,正对这个画板(窗口)addMouseListener(new MyMouseListener());}// 画笔@Overridepublic void paint(Graphics g) {// 利用迭代器遍历,读取点的坐标Iterator<Point> iterator = points.iterator();while (iterator.hasNext()){Point point=iterator.next();// 设置颜色与点的大小g.setColor(Color.PINK);g.fillOval(point.x,point.y,10,10);}}// 将点添加到画板上public void addPaint(Point point) {points.add(point);}// 适配器模式private class MyMouseListener extends MouseAdapter{// 鼠标监听事件:按下   弹起    按住不放@Overridepublic void mousePressed(MouseEvent e) {// 鼠标按下时,运行这个方法MyFrame2 myFrame2=(MyFrame2) e.getSource();// 添加画点坐标myFrame2.addPaint(new Point(e.getX(),e.getY()));//因为paint方法只会自动调用一次,所以通过repaint刷新后重新调用paint方法//每次点击鼠标都需要重新画一次repaint();}}
}

6.窗口监听

Modifier and TypeMethod and Description
voidwindowActivated(WindowEvent e)

当窗口设置为活动窗口时调用。

voidwindowClosed(WindowEvent e)

当窗口关闭时调用窗口调用处理结果时调用。

voidwindowClosing(WindowEvent e)

当用户尝试从窗口的系统菜单中关闭窗口时调用。

voidwindowDeactivated(WindowEvent e)

当窗口不再是活动窗口时调用。

voidwindowDeiconified(WindowEvent e)

当窗口从最小化更改为正常状态时调用。

voidwindowIconified(WindowEvent e)

当窗口从正常状态更改为最小化状态时调用。

voidwindowOpened(WindowEvent e)

第一次调用窗口可见。

package com.demo.panel;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class WindowTest {public static void main(String[] args) {new WindowFrame();}
}class WindowFrame extends Frame {public WindowFrame() {setBackground(Color.cyan);setBounds(200, 200, 300, 200);setVisible(true);addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.out.println("点击x关闭");System.exit(0);}@Overridepublic void windowActivated(WindowEvent e) {// 窗口激活事件:也就是当你点击到这个窗口内时,就是窗口激活了// 鼠标点击窗口外的地方(窗口变灰了),也就是离开了窗口WindowFrame source = (WindowFrame) e.getSource();source.setTitle("被再次激活了");System.out.println("windowActivated");}});}
}

7.键盘监听

Modifier and TypeMethod and Description
voidkeyPressed(KeyEvent e)

按下键时调用。

voidkeyReleased(KeyEvent e)

当键已被释放时调用。

voidkeyTyped(KeyEvent e)

键入键时调用。

package com.demo.panel;import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;public class KeyListenerTest {public static void main(String[] args) {new KeyFrame();}
}class KeyFrame extends Frame{public KeyFrame() {setBounds(200,200,200,200);setVisible(true);// 在这个窗口监听键盘事件this.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {// 读取键盘int keyCode = e.getKeyCode();// 输出System.out.println((char) keyCode);// 如果按到a就fuckif (keyCode == KeyEvent.VK_A ) {System.out.println("fuck");}}});}
}

http://www.15wanjia.com/news/32516.html

相关文章:

  • 大型医院设计网站建设网络营销师报名入口
  • 火车头wordpress建站群湖南疫情最新消息今天
  • 把网站扒下来以后怎么做一键免费生成网页的网站
  • 临汾做网站电话昆明seo技术培训
  • 桥西区网站建设网络营销产品策略的内容
  • 网站怎么做文本跳出来淘宝seo优化是什么
  • 接私活做网站网络销售平台
  • 品牌建设 社会意义东莞市网络seo推广企业
  • 中国工厂网站官方网站吉林seo网络推广
  • 阿里巴巴网站代刷网站推广链接免费
  • 怎么看网站用的什么程序做的360seo
  • 怎么自己做卡盟网站推广自己产品的文案
  • 官方网站下载12306seo怎么做?
  • 全球网站建设品牌百度有钱花人工客服
  • 阿里云oss做视频网站抖音关键词查询工具
  • 做网站如何盈利网络项目资源网
  • 微信公众号编辑教程seo推广优化的方法
  • u盘做网站代做seo关键词排名
  • 郑州市科协网站百度指数人群画像哪里查询
  • 做家具的外国网站百度站长工具数据提交
  • 淄博网站制作多样定制竞价推广方案
  • 毕设做网站可以得高分吗百度竞价推广的技巧
  • 东莞定制建站网站推广公司黄页网站推广公司
  • 建筑工程网上保健网站seo指导
  • 如何在雅虎台湾做企业网站免费网站在线观看人数在哪直播
  • 手机wap网站下载长沙百度网站快速排名
  • 仪征 做网站武汉seo主管
  • wordpress 站内搜索慢百度网站搜索排名
  • 网站的欢迎页怎么做seo教程网
  • 企业如何建设独立电商网站系统重庆森林电影简介