1.awt
2.组件和容器
3.布局管理器
4.事件处理
5.java图形
6.window事件
AWT(abstract window toolkit)抽象窗口开发包。他包含了很多类和接口,用于java application的GUI(Graphics User Interface图形用户界面)编程
所有的可以显示出来的图形元素都叫Component
Container是容纳其他Component的元素的容器
window是可以显示的独立窗口
panel不能作为程序的窗口独立的显示出来,若想显示,必须要将panel放在window中显示
window的子类
import java.awt.*;
//基本窗口的显示
public class TestFrame
{
//生成的图形都是不能关掉的,除非在cmd窗口中按ctrl+c键可以关闭窗口
public static void main(String[] args){
Frame f = new Frame("my first Test");
f.setLocation(300,300);//设定图形的左上方的左边,若不设置,则默认为左上方的坐标是(0,0)
f.setSize(170,100);
f.setBackground(Color.blue);
f.setResizable(false);//不能改变大小
f.setVisible(true);//可见
}
}
import java.awt.*;
//显示多个窗口
public class TestMutiFrame
{
public static void main(String[] args){
MyFrame m1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame m2 = new MyFrame(200,200,200,200,Color.red);
MyFrame m3 = new MyFrame(300,300,200,200,Color.black);
MyFrame m4 = new MyFrame(400,400,200,200,Color.green);
}
}
class MyFrame extends Frame
{
static int id = 0;
MyFrame(int x,int y,int w,int h,Color c){
super("MyFrame"+(++id));
setBackground(c);
setBounds(x,y,w,h);
setVisible(true);
}
}
panel
import java.awt.*;
public class TestPanel
{
public static void main(String[] args){
Frame f = new Frame("java Frame with Panel");
Panel p = new Panel(null);//panel传空值进去表明该panel不带自己的布局管理器
f.setVisible(true);
f.setBounds(300,300,500,500);
f.setBackground(new Color(0,0,102));
p.setBounds(50,50,400,400);
p.setBackground(new Color(205,205,255));
f.add(p);
f.setLayout(null);
}
}
import java.awt.*;
public class TestMutiPanel
{
public static void main(String[] args){
new MyFrame2("MyFramwWithPanel",300,300,400,300);
}
}
class MyFrame2 extends Frame
{
private Panel p1,p2,p3,p4;
MyFrame2(String s,int x,int y,int w,int h){
super(s);
setLayout(null);
p1 = new Panel(null);//不使用自己的布局管理器
p2 = new Panel(null);
p3 = new Panel(null);
p4 = new Panel(null);
p1.setBounds(0,0,w/2,h/2);
p2.setBounds(0,0,w/2,h/2);
p3.setBounds(0,0,w/2,h/2);
p4.setBounds(0,0,w/2,h/2);
p1.setBackground(Color.BLUE);
p2.setBackground(Color.GREEN);
p3.setBackground(Color.YELLOW);
p4.setBackground(Color.RED);
add(p1);add(p2);add(p3);add(p4);
setBounds(x,y,w,h);
setVisible(true);
}
}
自定义panel
//设计一个含有Panel的自定义的Frame类含有,形式如下图
import java.awt.*;
public class AfferClass
{
public static void main(String[] args){
new MyFrame3(300,300,400,300,Color.BLUE);
}
}
class MyFrame3 extends Frame
{
private Panel p;
MyFrame3(int x,int y,int w,int h,Color c){
super("MYframeTest");
setLayout(null);
setBounds(x,y,w,h);
setBackground(c);
p = new Panel(null);
p.setBounds(w/4,h/4,w/2,h/2);
p.setBackground(Color.YELLOW);
add(p);
setVisible(true);
}
}
运行结果:
布局管理器:
FlowLyout布局管理器是Panel默认的布局管理器
frameLayOUT的布局管理器的构造方法:
import java.awt.*;
public class FlowLayoutTest
{
public static void main(String[] args){
Frame f = new Frame("myFrame");
Button t1 = new Button("ok");
Button t2 = new Button("close");
Button t3 = new Button("open");
f.setLayout(new FlowLayout());
f.add(t1);
f.add(t2);
f.add(t3);
f.setSize(100,100);
f.setVisible(true);
}
}
运行结果:
import java.awt.*;
public class TestFlowLayout2
{
public static void main(String[] args){
Frame f = new Frame("FLAYOUT");
FlowLayout fl = new FlowLayout(FlowLayout.CENTER,20,40);//设置两个布局的行距是20,高度的差距是40
f.setBackground(Color.BLUE);
f.setLayout(fl);
f.setSize(300,200);
f.setLocation(300,400);
for(int i = 0;i<7;i++){
f.add(new Button("BUTTON"));
}
f.setVisible(true);
}
}
boderLayout布局管理器是frame的默认的布局管理器
import java.awt.*;
public class TestBorderLayout
{
public static void main(String[] args){
Frame f = new Frame("my1");
Button t1= new Button("north");
Button t2 = new Button("south");
Button t3 = new Button("east");
Button t4 = new Button("west");
Button t5 = new Button("center");
f.add(t1,BorderLayout.NORTH);
f.add(t2,BorderLayout.SOUTH);
f.add(t3,BorderLayout.EAST);
f.add(t4,BorderLayout.WEST);
f.add(t5,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(200,300);
}
}
运行结果:
gridLayout
import java.awt.*;
public class TestGridLayout
{
public static void main(String[] args){
Frame f = new Frame("test");
Button t1 = new Button("Button1");
Button t2 = new Button("Button2");
Button t3 = new Button("Button3");
Button t4 = new Button("Button4");
Button t5 = new Button("Button5");
Button t6 = new Button("Button6");
GridLayout gl = new GridLayout(3,2);//以3行2列来显示
f.setLayout(gl);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(t4);
f.add(t5);
f.add(t6);
f.pack();
f.setVisible(true);
}
}
运行结果:
事件监听