AWT

总体上Swing组件替代了绝大部分AWT组件,对AWT图形用户界面编程有极好的补充和加强。

package ch11;
import java.awt.*; /**
* Created by Jiqing on 2016/12/1.
*/
public class FrameTest {
public static void main(String[] args) {
Frame f = new Frame("测试窗口");
// 设置窗口的大小、位置
f.setBounds(30,30,250,200);
// 将窗口显示出来
f.setVisible(true);
}
}

AWT容器

图形界面编程,类似于小朋友玩的拼图游戏,容器类似于拼图的母板,普通组件类似于拼图图块。

package ch11;
import java.awt.*; /**
* Created by Jiqing on 2016/12/1.
*/
public class PanelTest {
public static void main(String[] args) {
Frame f = new Frame("测试页面");
Panel p = new Panel();
p.add(new TextField(20));
p.add(new Button("Click Me"));
f.add(p);
f.setBounds(30,30,250,120);
f.setVisible(true);
}
}

FlowLayout布局管理器

  • 组件像水流一样向某方向流动,遇到障碍就折回,重头开始排列。
package ch11;
import java.awt.*; /**
* Created by Jiqing on 2016/12/1.
*/
public class FlowLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("测试窗口");
// 设置Frame使用FlowLayout布局管理器
f.setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
// 向窗口添加10个按钮
for (int i = 0;i<10;i++) {
f.add(new Button("Button"+i));
}
// 设置窗口为最佳大小
f.pack();
f.setVisible(true);
}
}

BorderLayout布局管理器

  • BorderLayout将容器分为EAST、SOUTH、WEST、NORTH、CENTER五个区域。普通组件可以放在这五个区域中的任意一个。
  • 默认添加到中间区域。
package ch11;

import java.awt.*;
import static java.awt.BorderLayout.*; /**
* Created by Jiqing on 2016/12/1.
*/
public class BorderLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("测试窗口");
// 设置Frame容器使用BorderLayout布局管理器
f.setLayout(new BorderLayout(50,50));
f.add(new Button("S"),SOUTH);
f.add(new Button("N"),NORTH);
// 默认添加到中间区域
f.add(new Button("M"));
f.add(new Button("E"),EAST);
f.add(new Button("W"),WEST); f.pack();
f.setVisible(true); }
}

GirdLayout布局管理器

GirdLayout布局管理器将容器分割成纵横分割的网格,每个网格所占据的领域大小相同。

package ch11;
import java.awt.*;
import static java.awt.BorderLayout.*; // 默认布局 /**
* Created by Jiqing on 2016/12/1.
*/
public class GridLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("计算器");
Panel p1 = new Panel();
p1.add(new TextField(30));
f.add(p1,NORTH); // import static java.awt.BorderLayout.*;
Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,5,4,4));
String[] name = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","."};
for (int i = 0;i<name.length;i++) {
p2.add(new Button(name[i]));
}
f.add(p2);
f.pack();
f.setVisible(true);
}
}

GridBagLayout

  • 与GridLayout不同,它可以跨越一个或多个网络,并可以设置网络的大小互不相同。

  • 增加布局的灵活性

  • 使用步骤

  1. 创建GridBagLayout布局管理器
  2. 创建GridBagConstraints对象
  3. 建立关联
  4. 添加组件
package ch11;
import java.awt.*; /**
* Created by Jiqing on 2016/12/2.
*/
public class GridBagTest {
private Frame f = new Frame("测试窗口");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private Button[] bs = new Button[10]; public void init() {
f.setLayout(gb);
for (int i = 0;i<bs.length;i++) {
bs[i] = new Button("Button"+i);
} // 所有组件都可以横向、纵向扩大
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
addButton(bs[0]);
addButton(bs[1]);
addButton(bs[2]);
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[3]);
gbc.weightx = 0;
addButton(bs[4]);
gbc.gridwidth = 2;
addButton(bs[5]);
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[6]);
gbc.gridwidth = 1;
gbc.gridheight =2;
gbc.weighty = 1;
addButton(bs[7]);
gbc.weighty = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
addButton(bs[8]);
addButton(bs[9]);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
new GridBagTest().init();
} private void addButton(Button button) {
gb.setConstraints(button,gbc);
f.add(button);
} }

BoxLayout

  • BoxLayout 可以在垂直和水平两个方向摆放控件
package ch11;
import javax.swing.*;
import java.awt.*; /**
* Created by Jiqing on 2016/12/2.
*/
public class BoxLayoutTest {
private Frame f = new Frame("测试");
public void init() {
f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));
// 下面按钮将垂直排列
f.add(new Button("First Button"));
f.add(new Button("Second Button"));
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
new BoxLayoutTest().init();
}
}
package ch11;

import javax.swing.*;
import java.awt.*; /**
* Created by Jiqing on 2016/12/2.
*/
public class BoxTest {
private Frame f = new Frame("测试");
// 定义水平组件Box对象
private Box horizontal = Box.createHorizontalBox();
// 定义垂直组件Box对象
private Box verical = Box.createVerticalBox();
public void init() {
horizontal.add(new Button("H btn1"));
horizontal.add(new Button("H btn2"));
verical.add(new Button("V btn1"));
verical.add(new Button("V btn2"));
f.add(horizontal,BorderLayout.NORTH);
f.add(verical);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
new BoxTest().init();
}
}

常用的

package ch11;

import javax.swing.*;
import java.awt.*; /**
* Created by Jiqing on 2016/12/2.
*/
public class CommonComponent {
Frame f = new Frame("测试");
Button ok = new Button("Confirm");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox male = new Checkbox("Male",cbg,true);
Checkbox female = new Checkbox("Female",cbg,false); Choice colorChooser = new Choice();
List colorList = new List(6,true);
TextArea ta = new TextArea(5,20);
TextField name = new TextField(50);
public void init() {
colorChooser.add("red");
colorChooser.add("green");
colorChooser.add("blue");
colorList.add("red");
colorList.add("green");
colorList.add("blue");
Panel bottom = new Panel();
bottom.add(name);
bottom.add(ok);
f.add(bottom,BorderLayout.SOUTH);
Panel checkPanel = new Panel();
checkPanel.add(colorChooser);
checkPanel.add(male);
checkPanel.add(female);
Box topLeft = Box.createVerticalBox();
topLeft.add(ta);
topLeft.add(checkPanel); Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
f.add(top);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
new CommonComponent().init();
}
}

Dialog对话框

  • 对话框
package ch11;

import java.awt.*;

/**
* Created by Jiqing on 2016/12/2.
*/
public class DialogTest {
Frame f = new Frame("测试");
Dialog d1 = new Dialog(f,"Modal Dialog",true);
Dialog d2 = new Dialog(f,"Not Modal Dialog",false);
Button b1 = new Button("Open MD");
Button b2 = new Button("Open NMD");
public void init() {
d1.setBounds(20,30,300,400);
d2.setBounds(20,30,300,400);
b1.addActionListener(e->d1.setVisible(true));
b2.addActionListener(e->d2.setVisible(true));
f.add(b1);
f.add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
} public static void main(String[] args) {
new DialogTest().init();
}
}
package ch11;

import java.awt.*;

/**
* Created by Jiqing on 2016/12/2.
*/
public class FileDialogTest {
Frame f = new Frame("测试");
FileDialog d1 = new FileDialog(f,"load file",FileDialog.LOAD);
FileDialog d2 = new FileDialog(f,"save file",FileDialog.SAVE);
Button b1 = new Button("open");
Button b2 = new Button("save");
public void init() {
b1.addActionListener(e->{
d1.setVisible(true);
System.out.println(d1.getDirectory()+d1.getFile());
}); b2.addActionListener(e->{
d2.setVisible(true);
System.out.println(d2.getDirectory()+d2.getFile());
}); f.add(b1);
f.add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true); } public static void main(String[] args) {
new FileDialogTest().init();
}
}

Java可视化AWT的更多相关文章

  1. JAVA 可视化分析工具 第12节

    JAVA 可视化分析工具  第12节 经过前几章对堆内存以及垃圾收集机制的学习,相信小伙伴们已经建立了一套比较完整的理论体系!那么这章我们就根据已有的理论知识,通过可视化工具来实践一番. 我们今天要讲 ...

  2. JAVA中AWT编程

    JAVA使用AWT和Swing 类完成图形用户界面编程,AWT全称是抽象窗口工具集(Abstract Window Toolkit),它是最早的sun提供的GUI库(Graphics User Int ...

  3. Java中AWT、Swing与SWT三大GUI技术的原理与效率差异

    Java中AWT.Swing与SWT三大GUI技术的原理与效率差异 转 https://blog.csdn.net/weixin_37703598/article/details/81843810   ...

  4. JAVA学习AWT绘图

    package com.graphics; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel ...

  5. Java可视化编程---SendMail工具的开发

    介绍: SendMail是一款简便的163邮箱发件工具 利用了163的SMTP接口来发送邮件 版本号:SendMail v1.0 在编写发送邮件工具之前,还需要安装 JavaMail API 和Jav ...

  6. JAVA可视化闹钟源码

    概述 一些同学的Java课设有这样一个问题,比较感兴趣就做了一下 功能介绍: 1.可增加闹钟 2.可删除闹钟 3.时间到了响铃 4.关闭闹钟不会丢失闹钟(因为闹钟存储在txt文件中,不会因程序关闭就终 ...

  7. Java可视化计算器

    利用java中的AWT和SWING包来做可视化界面. 首先来简单了解一下这两个包: AWT和Swing都是Java中用来做可视化界面的.AWT(Abstract Window Toolkit):抽象窗 ...

  8. [java] 可视化日历的实现(基于Calendar类 )

    写在前面 博文安排顺序如下 1.写在前面 2.源码 3.思路 4.相关知识 该小程序是对Date类及其相关类的复习 要求如下图:实现可视化日历 实现思路 1.先从键盘输入指定格式的字符串(str)2. ...

  9. Java基础---AWT

    流式布局FlowLayout package net.zyz; import java.awt.Button; import java.awt.FlowLayout; import java.awt. ...

随机推荐

  1. 使用Jil序列化JSON提升Asp.net web api 性能

    JSON序列化无疑是Asp.net web api 里面性能提升最重要的一环. 在Asp.net web api 里面我们可以插入自定义的MediaTypeFormatter(媒体格式化器), 说白了 ...

  2. google 地图层级和对应关系

    google 地图层级和对应关系

  3. ACM题目————Team Queue

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  4. MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

    MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...

  5. 如何在 Eclipse 中连接源码

    1:首先在window 中 打开首选项(preferences) 找到如下java -- 已安装的JRE

  6. 简单的分页sql

    select top 5 * from books where id not in(select top (5*(5-1)) id from Books order by id) order by i ...

  7. JAVA基础知识之JVM-——URLClassLoader

    URLClassLoader是ClassLoader的一个实现类,它既能从本地加载二进制文件类,也可以从远程加载类. 它有两个构造函数, 即 URLClassLoader(URL[] urls),使用 ...

  8. 测试post

    http://www.atool.org/httptest.php 这里也可以测试 火狐有插件:HttpRequester Chrome有插件:Postman

  9. c++中函数empty()怎么使用

    string s = "";if (s.empty()){ cout << "字符串为空..";}else{ cout << " ...

  10. Git合并开发代码分支到测试代码分支

    ——转载请注明出自天外归云的博客园 用TortoiseGit下载代码到本地 首先需要在本机安装好TortoiseGit.然后在随便哪个路径下比如D盘,右键“Git Clone”: 然后URL处选择项目 ...