---------------------- android培训 、 java培训 、 期待与您交流! ------------------
1、SWT简介:
AWT的全称是抽象窗口工具集(Abstract Window Toolkit),它是一个特殊的组件,其中包含有其他的组件。用户可以利用AWT,在容器中创建标签、按钮、复选框、文本框等用户界面元素。
2、SWT组成部分:
其中主要由以下几部分组成,包括:组件类(Component)、容器类(Container)、图形类(Graphics)和布局管理器(LayoutManager)。
|--Component(组件)
Componet是Java的图形用户界面的最基本组成部分是组件,类java.awt.Component是许多组件类的父类,是按钮、标签、菜单等组件的抽象基本类。Component类中封装了组件通用的方法和属性,如图形的组件对象、大小、显示位置、前景色和背景色、边界、可见性等,相应的成员方法包括:
getComponentAt(int x, int y)
getFont()
getForeground()
getName()
getSize()
paint(Graphics g)
repaint()
update()
setVisible(boolean b)
setSize(Dimension d)
setName(String name)等
|--Container(容器)
Container也是一个类,是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但是它的主要功能是容纳其它组件和容器。
有三种类型的容器:Window、Panel、ScrollPane,常用的有Panel, Frame, Applet。
|--LayoutManager(布局管理器)
LayoutManager定义容器中组件摆放位置和大小接口。
为了实现跨平台的特性并且获得动态的布局效果,java将容器内的所有组件安排给一个"布局管理器"负责管理,如:排列顺序,组件的大小、位置,当窗口移动或调整大小后组件如何变化等功能授权给对应的容器布局管理器来管理,不同的布局管理器使用不同算法和策略,容器可以通过选择不同的布局管理器来决定布局。
布局管理器主要包括:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout。
FlowLayout是Panel类默认的布局管理器。行内从左到右,一行排满后换行。
BorderLayout是Frame类默认的布局管理器。其把整个容器的布局分为:East、West、South、North、Center五个区域。如不指定加入位置,则默认添加在Center。每个区域 只能添加一个组件,如若添加多个,则会覆盖。
GridLayout将容器划分为矩形网格,组件按行、列排序。
CardLayout可存储几个不同布局。当需要很多面板切换,且每个面板需要显示不同布局时可以使用CardLayout。
3、一个小例子:
实现WAT中四种布局管理器:FlowLayout、GridLayout、BorderLayout和CardLayout。
package awt;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame{
private static final long serialVersionUID = 1L;
Panel borderLayoutPanel;
Panel cardLayoutPanel;
Panel flowLayoutPanel;
Panel gridLayoutPanel;
private void generateGridLayoutPanel() {
gridLayoutPanel = new Panel();
gridLayoutPanel.setLayout(new GridLayout(2,2));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
gridLayoutPanel.add(button1);
gridLayoutPanel.add(button2);
gridLayoutPanel.add(button3);
gridLayoutPanel.add(button4);
}
private void generateFlowLayoutPanel() {
flowLayoutPanel = new Panel();
flowLayoutPanel.setLayout(new FlowLayout());
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((Button)e.getSource()).setLabel("welcome ");
}
});
flowLayoutPanel.add(button1);
flowLayoutPanel.add(button2);
flowLayoutPanel.add(button3);
flowLayoutPanel.add(button4);
flowLayoutPanel.add(button5);
}
private void generateBorderLayoutPanel() {
borderLayoutPanel = new Panel();
borderLayoutPanel.setLayout(new BorderLayout());
Button button1 = new Button("South");
Button button2 = new Button("West");
Button button3 = new Button("East");
Button button4 = new Button("North");
Button button5 = new Button("Center");
borderLayoutPanel.add(button1,BorderLayout.SOUTH);
borderLayoutPanel.add(button2,BorderLayout.WEST);
borderLayoutPanel.add(button3,BorderLayout.EAST);
borderLayoutPanel.add(button4,BorderLayout.NORTH);
borderLayoutPanel.add(button5,BorderLayout.CENTER);
}
private void genrateCardLayoutPanel() {
cardLayoutPanel = new Panel();
final CardLayout cl = new CardLayout();
cardLayoutPanel.setLayout(cl);
Button button1 = new Button("black");
Button button2 = new Button("red");
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.next(cardLayoutPanel);
}
};
button1.addActionListener(al);
button2.addActionListener(al);
cardLayoutPanel.add(button1,"1");
cardLayoutPanel.add(button2,"2");
}
public MyFrame(String panelName) {
super("panelName");
generateBorderLayoutPanel();
generateFlowLayoutPanel();
generateGridLayoutPanel();
genrateCardLayoutPanel();
setLayout(new GridLayout(2,2));
add(borderLayoutPanel);
add(flowLayoutPanel);
add(gridLayoutPanel);
add(cardLayoutPanel);
setSize(800, 800);
setLocation(100,100);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
public static void main(String[] args) {
MyFrame myFrame = new MyFrame("welcome");
myFrame.setVisible(true);
}
}
运行后的效果为: