1. AWT 容器继承关系

示例1:

public class Main {
public static void main(String[] args) throws Exception { Frame f = new Frame();
Panel p = new Panel(); //Panel容器不能单独存在,需要添加组件,放到frame或其他容器中
p.add(new TextField(20));
p.add(new Button("clicked me")); ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);//ScrollPanle也不能单独存在
sp.add(p);
f.add(sp);
f.setBounds(30, 30, 300, 300);
f.setVisible(true);
}
}

2. 布局管理器: 默认的布局管理器是 BorderLayout.

FlowLayout布局: 流水一样,向某一方向顺序排列,直到边界再返回 继续顺序排列。

public class Main {

	public static void main(String[] args) throws Exception {

		Frame f = new Frame();
f.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5)); //Layout不采用默认构造器,左对齐,垂直水平间距为20 5
for(int i=0; i<10; i++)
{
f.add(new Button("clicked me" + i));
} f.setBounds(30, 30, 300, 300);
f.setVisible(true);
}
}

BorderLayout : 分为东南西北中5个区域,每个区域只能添加一个组件, 但是容器也是一种组件,所以可以先添加到一个容器里。

public class Main {

	public static void main(String[] args) throws Exception {

		Frame f = new Frame();
f.setLayout(new BorderLayout(20,5)); //设置5部分之间的垂直水平间隔 20 5
f.add(new Button("South"), BorderLayout.SOUTH);
f.add(new Button("North"), BorderLayout.NORTH);
f.add(new Button("West"), BorderLayout.WEST);
f.add(new Button("East"), BorderLayout.EAST);
f.add(new Button("Center"), BorderLayout.CENTER); f.setBounds(30, 30, 300, 300);
f.setVisible(true);
}
}

GridLayout : 网格布局

public class Main {

	public static void main(String[] args) throws Exception {

		Frame f = new Frame();
Panel p = new Panel();
p.setLayout(new GridLayout(3, 5, 4, 4));
String [] names = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"+", "-", "*", "/"};
for(int i=0; i!=names.length; i++)
{
p.add(new Button(names[i]));
}
p.add(new Button("Clicked me"), null, 14); //插入指定位置 f.add(p);
f.setBounds(30, 30, 300, 300);
f.setVisible(true);
}
}

GridBagLayout: 和GridLayout相似,不同的是一个组件可以跨越多个网格,并且可以设置网格的大小互不相同。

参考文章:    GridBagLayout网格包布局管理器:http://wenku.baidu.com/link?url=hVhmI-yAsMyblLShSPOCB89ffR3Nqsur8onMDVPzgdtTOuU-13U7qhtEhFygkkWSRvooneqSdBduX-Ctj6zjPxIUVrTmWGve26MDvcCNVF3

public class Main {

	private Frame f = new Frame("test GridBag");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private Button[] bs = new Button[10]; private 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 = 0; //设置缩放时的比例
gbc.weighty = 0;
addButton(bs[0]);
addButton(bs[1]);
addButton(bs[2]);
gbc.gridwidth = GridBagConstraints.REMAINDER; //设置为边界
addButton(bs[3]); gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 1;
addButton(bs[4]);
addButton(bs[5]);
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[6]); gbc.gridwidth = 2; //所占网格个数
gbc.gridheight = 2;
gbc.ipadx = 50; //组件横纵 所占像素个数
gbc.ipady = 50;
addButton(bs[7]); gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weighty = 0;
gbc.ipadx = 0;
gbc.ipady = 0;
addButton(bs[8]); gbc.gridx = 4; //组件插入到网格的坐标
gbc.gridy = 4;
addButton(bs[9]); f.pack();
f.setVisible(true);
} private void addButton(Button button)
{
gb.setConstraints(button, gbc);
f.add(button);
} public static void main(String[] args) throws Exception {
new Main().init();
}
}

CardLayout: 卡片一样堆列, 每次只能看到最上面的一张。

public class Main {

	private Frame f = new Frame("test CardLayout");
String [] names = {"NO1", "NO2", "NO3", "NO4", "NO5"};
Panel p1 = new Panel();
CardLayout c = new CardLayout(); public void init()
{
p1.setLayout(c);
for(int i=0; i<names.length; i++)
{
p1.add(names[i], new Button(names[i]));
} Panel p2 = new Panel();
Button previous = new Button("Previous");
previous.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.previous(p1); //前一张
}
}); Button next = new Button("Next");
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.next(p1); //下一张
}
}); Button first = new Button("First");
first.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.first(p1); //第一张
}
}); Button last = new Button("Last");
last.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.last(p1); //最后一张
}
}); Button third = new Button("NO3");
third.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
c.show(p1, "NO3"); //按名字“NO3”查找
}
}); p2.add(previous);
p2.add(next);
p2.add(first);
p2.add(last);
p2.add(third);
f.add(p2);
f.add(p1, BorderLayout.NORTH);
f.pack();
f.setVisible(true);
} public static void main(String[] args) throws Exception {
new Main().init();
}
}

2. BOX 容器

public class Main {

	private Frame f = new Frame("test CardLayout");
private Box horizontal = Box.createHorizontalBox(); //水平Box容器
private Box vertical = Box.createVerticalBox(); //竖直BOX容器
public void init()
{
horizontal.add(new Button("h_Button1"));
horizontal.add(Box.createHorizontalGlue()); //添加可拉伸的水平间距
horizontal.add(new Button("h_Button2"));
horizontal.add(Box.createHorizontalStrut(50)); //添加不可拉升的水平间距,固定宽度50 vertical.add(new Button("v_Button1"));
vertical.add(Box.createVerticalGlue()); //可拉伸
vertical.add(new Button("v_Button2"));
vertical.add(Box.createVerticalStrut(50)); //固定50,不可拉伸
vertical.add(new Button("v_Button3")); f.add(horizontal, BorderLayout.NORTH);
f.add(vertical);
f.pack();
f.setVisible(true); }
public static void main(String[] args) throws Exception {
new Main().init();
}
}

3. 基本组件

public class Main {
public static void main(String[] args) throws Exception { Frame f = new Frame("test CommonComponent");
Button okButton = new Button("OK");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox maleCheckbox = new Checkbox("man", cbg, false);
Checkbox femaleCheckbox = new Checkbox("woman", cbg, false);
Checkbox marCheckbox = new Checkbox("married ?", false);
Choice colorChooser = new Choice();
colorChooser.add("Red");
colorChooser.add("Green");
colorChooser.add("Blue");
List colorList = new List(6, true);
colorList.add("Red");
colorList.add("Green");
colorList.add("Blue");
TextArea ta = new TextArea(5, 20);
TextField name = new TextField(50); Panel bottom = new Panel();
bottom.add(name);
bottom.add(okButton);
Panel checkPanel = new Panel();
checkPanel.add(colorChooser);
checkPanel.add(maleCheckbox);
checkPanel.add(femaleCheckbox);
checkPanel.add(marCheckbox); Box topLeft = Box.createVerticalBox();
topLeft.add(ta);
topLeft.add(checkPanel);
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList); f.add(bottom, BorderLayout.SOUTH);
f.add(top);
f.pack();
f.setVisible(true);
}
}

4. Dialog 和普通窗口用法基本一样,只是可以有父控件,有两种模式“mode” "non-mode",在“mode”下子窗口在父窗口之上,并且父窗口不能再获得焦点,构造器如下:

Dialog d1 = new Dialog(f, "mode", true);

  Dialog d2 = new Dialog(f, "non-mode", false);

Dialog有子类FileDialog 用打开和保存文件。。

public class Main {
public static void main(String[] args) throws Exception { Frame f = new Frame("test Dialog");
final FileDialog d1 = new FileDialog(f, "open file", FileDialog.LOAD);
final FileDialog d2 = new FileDialog(f, "save file", FileDialog.SAVE);
Button bt1 = new Button("open file");
Button bt2 = new Button("save file");
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d1.setVisible(true);
System.out.println(d1.getDirectory() + d1.getFile());
}
});
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d2.setVisible(true);
System.out.println(d1.getDirectory() + d1.getFile());
}
}); f.add(bt1);
f.add(bt2, BorderLayout.SOUTH);
f.pack();
f.setVisible(true); }
}

5. 事件处理分类

如上表 如果直接添加WindowListener 接口那将会有右边那么多方法需要实现,所以Java会有一个WindowAdapter适配器去实现WindowListener的各个方法,但是这些方法只是空方法,User只需要继承WindowAdapter实现自己需要的方法即可。 其他Listener也有其对应的Adapter.

Java -- AWT , GUI图形界面的更多相关文章

  1. Java中的图形界面编程

    前言 正文 Java中的图形界面编程 AWT/Swing AWT(Abstract Window ToolKits,抽象窗体工具集) 1.容器类:用来存储组件,实现容器布局 2.组件类:实现界面的一些 ...

  2. Quartz(GUI)图形界面程序----Quartz Web

    下载.设置和运行Quartz(GUI)图形界面程序----Quartz Web 一.获取Quartz Web程序(Quartz GUI).早期的 Quartz 框架开发者意识到一个 GUI 对于某类用 ...

  3. Java计算器的图形界面应用程序

      JAVA计算器的图形界面应用程序 题目简介: 整体分析: 实验代码: /*部分使用插件做界面*/ import java.awt.EventQueue; import javax.swing.JB ...

  4. JAVA简单Swing图形界面应用演示样例

    JAVA简单Swing图形界面应用演示样例 package org.rui.hello; import javax.swing.JFrame; /** * 简单的swing窗体 * @author l ...

  5. centOS7下安装GUI图形界面

    1.如何在centOS7下安装GUI图形界面 当你安装centOS7服务器版本的时候,系统默认是不会安装GUI的图形界面程序,这个需要手动安装CentOS7 Gnome GUI包. 2.在系统下使用命 ...

  6. CentOS7安装GUI图形界面

    本文转自centOS7下安装GUI图形界面,侵权删. 1. 在命令行下 输入下面的命令来安装Gnome包. # yum groupinstall "GNOME Desktop" & ...

  7. CentOS7 下安装GUI图形界面GNOME

    在安装Gnome包之前,需要检查一下网络是否有网络(使用ping www.baidu.com) 一.先装X windows,-y表示参数同意所有软件安装操,当出现 Complete!说明这里安装成功了 ...

  8. CentOS7安装Gnome GUI图形界面

    CentOS7安装Gnome GUI图形界面  最小化安装了.当时没 注意,后面一步步安装完了,结果直接启动到命令行模式了. 晕,又不想重新安装,直接想从命令行模式安装.在网上找了半天,终于找到一点小 ...

  9. Java GUI图形界面开发工具

    Applet 应用程序     一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...

  10. 第58节:Java中的图形界面编程-GUI

    欢迎到我的简书查看我的文集 前言: GUI是图形用户界面,在Java中,图形用户界面我们用GUI表示,而GUI的完整英文为: Graphical User Interface(图形用户接口), 所谓图 ...

随机推荐

  1. JavaScript事件使用指南

    事件流 事件流描述的是从页面中接收事件的顺序,IE和Netscape提出来差不多完全相反的事件流的概念,IE事件流是事件冒泡流,Netscape事件流是事件捕获流. 事件冒泡 IE的事件流叫做事件冒泡 ...

  2. oracle如何进行索引监控分析和优化

    在生产环境.我们会发现: ① 索引表空间 I/O 非常高     ② "db file sequential read" 等待事件也比较高   这种迹象表明.整个数据库系统.索引的 ...

  3. 403/you don't have the permission to access on this server

    Localhost/index.php出现 错误403 you don't have the permission to access on this server 现在已经解决,特将方法与大家分享. ...

  4. PHP 使用 GeoLiteCity 库解析 IP 为地理位置

    关于把 IP 地址转换为地理位置可以使用网络上很多的 API,好处就是不用在本地存储一个 IP 数据库,而且一般网络上的 IP 库会自动更新,不利的地方就是太依赖于网络,性能表现也可能会弱些.比如像下 ...

  5. Visual Studio的 Apache Cordova 插件CTP3.0发布!

    北京时间12号晚23点开始的Connect()活动上,微软发布了一系列激动人心的消息! .NET开源了!以后.NET将可在Linux和Mac OS平台上运行! VS免费了!!如果你是学生,个人开发者, ...

  6. staitic_cast原理与使用

    本文以下述结构为例: 总结如下: 1) static_cast用于有直接或间接关系的指针或引用之间 转换.没有继承关系的指针不能用此转换,即使二者位于同一类体系中.比如,Left,Right之间不能用 ...

  7. 关于js语法中的一些难点(预解析,变量提前,作用域)

    ******标题很吓人************ 其实就是一个小小的例子 ,从例子中简单的分析一下作用域.预解析和变量提前的概念 <!DOCTYPE html> <html> & ...

  8. C语言基础知识【循环】

    C 循环1.有的时候,我们可能需要多次执行同一块代码.一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许我 ...

  9. 修改Jmeter配置使能支持更大并发

    Jmeter做并发测试时,报错 java.lang.OutOfMemoryError:gc overhead limit exceeded. 原因是jmeter默认分配内存的参数很小,256M吧.故而 ...

  10. 获取系统时间的DOS命令

    DOS C:\Users\yaozhendong>echo %date:~0,10% %time%2011/12/24 19:45:41.25 前段时间工作中需要对一个地址做定时PING操作,并 ...