1. 组件与容器

容器就是窗口和面板,而组件为按钮、文本域、标签等(待续),二者的声明、设置很相似,

因此本文只做对于容器的详解。组件与容器的区别在于组件不能做容器,而部分容器可以兼顾组件和容器的特性

1.1 Frame(窗口)

属于容器,不可作为组件添加至其他容器中

声明

Frame frame = new Frame();

基本方法

public void setBounds(int x, int y, int width, int height);//设置窗口的初始位置和大小,可参照电脑屏幕:1900*1080
public void setTitle(String title);//设置窗口名称
public void setResizable(boolean resizable);//设置窗口大小是否可变,false-窗口大小只能以初始值显示
public void setBackground(Color bgColor);//设置窗口颜色
public void setLayout(LayoutManager mgr);//设置窗口布局
public void setVisible(boolean b);//设置窗口的可见性
import java.awt.*;
public class TheFirstFrame{
  public static void main(string[] args){
Frame frame = new Frame();
frame.setTitle("hello");
//Frame frame = new Frame("hello");与前两句效果一致
frame.setBounds(100,100,100,100);
frame.seBackground(new Color(255,0,0));//Color(r,g,b);
//frame.setLocation(100,100);
//frame.setSize(100,100);以上两句与setBounds效果一致
frame.setResizable(false);
frame.setVisible(true);
}
}

上例的运行结果

1.2 Panel(面板)

与Frame相似,可以作为组件被添加至容器中

Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(new FlowLayout());//设置布局,默认布局为边框布局-居中
frame.add(panel);

2. 布局

绝对布局、非绝对布局

2.1 流式布局

FlowLayout,从上至下,从左至右依次在容器中排列组件,默认为从上到下居中

Frame frame = new Frame();
Button button01 = new Button(“button1”);//Button属于组件,不做解释
Button button02 = new Button("button2");
Button button03 = new Button("button3");
Button button04 = new Button("button4");
//布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//意为从左对齐排列
//添加组件-add
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
//设置窗口属性
frame.setBounds(200,200,200,200);
frame.setVisible(true);
frame.ReSizeable(true);

2.2 边框布局

BorderLayout,分为东西南北中五个区域

public class BorderLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("1");
Button button3 = new Button("1");
Button button2 = new Button("1");
Button button4 = new Button("1");
Button button5 = new Button("1");
frame.setLayout(new BorderLayout());
frame.add(button1,BorderLayout.CENTER);
frame.add(button2,BorderLayout.EAST);
frame.add(button3,BorderLayout.WEST);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.SOUTH);
frame.setBounds(200,200,200,200);
frame.setVisible(true);
}
}

2.3 表格布局

GridLayout,自定义表格的行列数以及行距

public class DemoForBlog01 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("1");
Button button3 = new Button("1");
Button button2 = new Button("1");
Button button4 = new Button("1");
Button button5 = new Button("1");
Button button6 = new Button("1");
frame.setLayout(new GridLayout(2,2,10,10));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setVisible(true);
frame.setBounds(200,200,200,200);
}
}

3. 监听器

监听各个动作的发生并自定义下一个行为,

分为:事件监听、鼠标监听、窗口监听、键盘监听

3.1 事件监听

ActionListener,是一个接口,其中只有actionPerformed(ActionEvent e)一个抽象方法,定义当此事件(调用此监听的主体)发生时的行为

public interface ActionListener extends EventListener {

    /**
* Invoked when an action occurs.
* @param e the event to be processed
*/
public void actionPerformed(ActionEvent e); }

单一监听

一个监听器对应于一种事件

public class ActionDemo{
public ActionDemo(){
Frame frame = new Frame("ButtonDemo");
MyListener myListener = new MyListener();
Button button = new Button("actionListener");
button.addActionListener(myListener);
frame.add(button);
frame.setBounds(200,200,200,200);
frame.setVisible(true);
}
public static void main(){
new ActionDemo();
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(){
System.out.print("this is a button");
//当事件(即按钮被点击)发生时,会打印出“this is a button”
}
}

多对应监听

指写一个监听器的实现类同时监听两个事

public class ActionListenerDemo {
public static void main(String[] args) {
     Frame frame = new Frame("actionListener");
Button button01 = new Button("start");
Button button02 = new Button("stop");
button01.setActionCommand("button001");
//actionCommand直译为事件注释,是除了按钮title外可以分辨而又不会显示在按钮上的文本信息,String类型
MyListener myActionListener = new MyListener();
button01.addActionListener(myActionListener);
button02.addActionListener(myActionListener);
//监听器通过add加在按钮事件上
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(button01);
frame.add(button02);
frame.setBounds(200,200,200,200);
frame.setVisible(true);
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    //参数e是调用此监听器的主体,即按钮
String str = e.getActionCommand();
if(str.equals("start")){
System.out.println("start button");
}
else if(str.equals("stop")){
System.out.println("stop button");
}
}
}

3.2 鼠标监听

MouseListener对鼠标事件的监听

首先,鼠标行为分为三种:按下、谈起、按住不放,同样地,它的方法也比较少:

public interface MouseListener extends EventListener {

    /**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
* @param e the event to be processed
*/
public void mouseClicked(MouseEvent e); /**
* Invoked when a mouse button has been pressed on a component.
* @param e the event to be processed
*/
public void mousePressed(MouseEvent e); /**
* Invoked when a mouse button has been released on a component.
* @param e the event to be processed
*/
public void mouseReleased(MouseEvent e); /**
* Invoked when the mouse enters a component.
* @param e the event to be processed
*/
public void mouseEntered(MouseEvent e); /**
* Invoked when the mouse exits a component.
* @param e the event to be processed
*/
public void mouseExited(MouseEvent e);
}

适配器模式

鼠标监听中的方法我们不会全部使用,一般而言,只要mousePressed这一个即可。

但是如果直接实例化MouseListener接口的话就需要将其中的方法全部重写,此时我们引入一种设计模式【适配器模式】

可以简单地理解为在这种模式下可以选择性地重写方法。

使用

首先提供有一个adapter类(译为适配器)方法,是已经对MouseListener接口实例化了的一个类,直接对其继承即可

class MyMouse extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e){
System.out.println("mouse press");
}
}

3.3 窗口监听

WindowListener接口,适配器:WindowsAdapter

窗口中比较常用的就是窗口关闭(closing)和窗口激活(activated),

如果没有设置窗口监听的话点击生成窗口上面的关闭按钮时不会起作用的

public class WindowListenerDemo{
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(200,200,200,200);
frame.setVisible(true);
frame.addWindowListener(new MyWindowListener());//调用构造器
}
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
System.out.println("window closing");
System.exit(0);//结束程序(关闭窗口)
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("window activated");
}
}

3.4 键盘监听

KeyLinstener,适配器:KeyAdapter,键盘事件有:按下,弹起,按住不放。一般只用按下(KeyPressed)这一个方法

键盘上每一个按键都是固定的,不能用其他名字等代替,为获取按键值使用getKeyCode()方法,int类型

KeyEvent表示键盘事件,通过类KeyEvent来获取按键,如KeyEvent.VK_Space表示空格

public class MouseListenerDemo {
public MouseListenerDemo(){
Frame frame = new Frame();
frame.addKeyListener(new MyKey());
frame.setBounds(200,200,200,200);
frame.setVisible(true);
} public static void main(String[] args) {
new MouseListenerDemo();
}
}
class MyKey extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE){
System.out.println("space");
}
}
}

Java-GUI基础(二)java.awt的更多相关文章

  1. java多线程基础(二)--java多线程的基本使用

    java多线程的基本使用 在java中使用多线程,是通过继承Thread这个类或者实现Runnable这个接口或者实现Callable接口来完成多线程的. 下面是很简单的例子代码: package c ...

  2. java GUI编程二

    java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...

  3. java基础(二)-----java的三大特性之继承

    在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...

  4. Java 数组基础,java.util.Arrays

    定义数组 方式1(推荐,更能表明数组类型) 方式2(同C语言) 方式3定义时直接初始化 数组运用基础 数组长度 equals() 数组元素不为基本数据类型时 二维数组 二维数组基础 变长的二维数组 j ...

  5. Java 语言基础 (初识Java语言, 变量和数据类型, 运算符, 流程控制语句, 数组)

    初始 Java 语言 Java SE -- Java Platform, Standard Edition 是 Java 平台的基础 Java SE 以前称为 J2SE, 可以编写桌面应用和基于 we ...

  6. Java语言基础及java核心

    一.Java语言特点 1. 简单 2. 面向对象 3. 分布式 4. 健壮 5. 安全 6. 中性架构跨平台 7. 超强的可移植性 8. 高性能 9. 多线程 二.java的环境变量 JAVA_HOM ...

  7. 【Java并发基础】Java线程的生命周期

    前言 线程是操作系统中的一个概念,支持多线程的语言都是对OS中的线程进行了封装.要学好线程,就要搞清除它的生命周期,也就是生命周期各个节点的状态转换机制.不同的开发语言对操作系统中的线程进行了不同的封 ...

  8. java线程基础知识----java线程模型

    转载自http://www.cnblogs.com/nexiyi/p/java_memory_model_and_thread.html 1. 概述 多任务和高并发是衡量一台计算机处理器的能力重要指标 ...

  9. java线程基础知识----java daemon线程

    java线程是一个运用很广泛的重点知识,我们很有必要了解java的daemon线程. 1.首先我们必须清楚的认识到java的线程分为两类: 用户线程和daemon线程 A. 用户线程: 用户线程可以简 ...

  10. 【Java并发基础】Java内存模型解决有序性和可见性

    前言 解决并发编程中的可见性和有序性问题最直接的方法就是禁用CPU缓存和编译器的优化.但是,禁用这两者又会影响程序性能.于是我们要做的是按需禁用CPU缓存和编译器的优化. 如何按需禁用CPU缓存和编译 ...

随机推荐

  1. Leetcode-数组&链表

    常见双指针技巧用法,只总结思路,具体边界判定想不清楚的时候稍微画个图就行了 1. 快慢指针判断链表是否含有环.环入口(快慢指针再次相遇即有环:再从头节点和快慢指针的相遇位置同速度向后,相遇点即为环入口 ...

  2. Java (四)APACHE Commons IO 复制文件

    上一篇:Java (三)APACHE Commons IO 常规操作 例1:复制文件 1 import java.io.File; 2 import java.io.IOException; 3 4 ...

  3. 头文件.h的作用

    参考链接http://www.cnblogs.com/webcyz/archive/2012/09/16/2688035.html懒得复制过来

  4. P2832 行路难

    题面 Link 题目背景 小X来到了山区,领略山林之乐.在他乐以忘忧之时,他突然发现,开学迫在眉睫 题目描述 山区有 \(n\) 座山.山之间有 \(m\) 条羊肠小道,每条连接两座山,只能单向通过, ...

  5. ActiveMQ详细入门教程系列(一)

    一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...

  6. Linux 杀毒软件ClamAV安装部署

    环境说明 系统安全需求,批量安装免费杀毒软件: 操作系统统一为CentOS 7 x64,在此选择免费开源杀毒软件ClamAV: 两种安装方式 1.yum 安装: 2.源码包编译安装: 安装参考网址: ...

  7. Docker笔记6:Docker 常见命令及镜像管理

    目  录 一.Docker 常用命令 docker version 命令 docker info 命令 二.Docker 镜像管理 搜索镜像:docker search 镜像名 获取镜像:docker ...

  8. rs232转rs485

    rs232转rs485 rs232转rs485 ZLAN9223E是上海卓岚科技开发的一款先进的无源RS232转RS485转换器.具有如下优点: 支持最高达230400bps的波特率.高波特率下供电能 ...

  9. 1、微信小程序开发介绍。

    微信小程序如何能达到快速的开发效果,下面首先介绍一下需要的框架,使用这些框架可以减少大部分编写代码时间. 微信小程序使用的框架:weui开源框架 后端数据使用的框架(包含管理和api接口框架):YiS ...

  10. 最大子段和之M子段和

    最大M子段和 题目模型 N个整数组成的序列 \(a_1,a_2,a_3,-,a_n\) ,将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的. 问题分析 方法一: 看到序列,我们首先要尝试 ...