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. Android Developer:合并清单文件

    使用Android Studio而且基于Gradle构建.每一个App能在多个位置包括清单文件,比如在src/main文件夹下productFlavor.库.Android ARchive(AAR) ...

  2. YUV420视频上面添加字幕

    1.source_codemain.c中实现了函数draw_Font_Func(),这个函数可以直接移植到C程序中使用.zimo.h里面放的是字模转码后的数据. 2.data_yuv测试用的yuv42 ...

  3. Ubuntu 16.04主题美化和软件推荐

    http://www.linuxidc.com/Linux/2016-09/135165.htm http://www.techweb.com.cn/network/system/2015-11-20 ...

  4. 堆排序算法的java实现

         堆积排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,可以利用数组的特点快速定位指定索引的元素.堆排序是不稳定的排序方法,辅助空间为O(1), 最坏时间复杂度为O ...

  5. Python之比较运算符

    python中的比较运算符有8个. 运算 | 含义=============< | 小于<= | 小于等于> | 大于>= |大于等于== | 等于!= |不等于is | 是i ...

  6. 用Python抓取漫画并制作mobi格式电子书

    想看某一部漫画,但是用手机看感觉屏幕太小,用电脑看吧有太不方面.正好有一部Kindle,决定写一个爬虫把漫画爬取下来,然后制作成 mobi 格式的电子书放到kindle里面看. 一.编写爬虫程序 用C ...

  7. C# virtual,override,new 整理

    今天仔细学习了一下C#中virtual, override, new关键字,参考了网上的很多资料,现整理一下. Virtual: virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可 ...

  8. git学习之时光机穿梭(四)

    时光机穿梭 我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed ver ...

  9. Elasticsearch5.3 学习(一):安装、Yii2.0 下载es扩展

    Elasticsearch是一个基于Lucene,提供了一个分布式多用户能力的全文搜索引擎.其他就不多说了,官方文档有详细的介绍. 我自己是在CentOS 7.0安装的 Elasticsearch 是 ...

  10. readonly const关键字

    readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数,readonly  ...