GUI内容
AWT
组件和容器
布局管理器
事件处理
Java图形
Window事件
AWT
(abstract windows toolkit)
包括了很多类和接口
用于java application的GUI(Graphics User Interface 图形用户界面)
Container和component是AWT中的两个核心类
Frame
setBounds(int x,int y,int width,int height)
setSize(int width,int height)
setLocation(int x,int y);
setBackground(Color c)设置背景颜色
setVisible(Boolean b)设置是否可见
setTitle(String name)String getTitle()
setResizable(Boolean b)设置是否可以调整大小
代码清单:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
public class TestFrame {
public static void main(String []args){
Frame f = new Frame("My Frame");
f.setSize(400,300);
f.setBackground(Color.CYAN);
f.setResizable(false);
//f.setBounds(300,400,400,300);
f.setVisible(true);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
public class TestMultiFrame{
public static void main(String [] args){
MyFrame f1 =
new MyFrame(100,100,200,200,Color.black);
MyFrame f2 =
new MyFrame(300,100,200,200,Color.cyan);
MyFrame f3 =
new MyFrame(100,300,200,200,Color.gray);
MyFrame f4 =
new MyFrame(300,300,200,200,Color.pink);
}
}
class MyFrame extends Frame{
static int id = 0;
MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame "+(++id));
setBackground(color);
setLayout(null);
setBounds(x,y,w,h);
setVisible(true);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
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);
f.setLayout(null);
f.setBounds(300,300,500,500);
f.setBackground(new Color(0,0,102));
p.setBounds(50,50,400,400);
p.setBackground(new Color(204,204,255));
f.add(p);
f.setVisible(true);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
public class TestMultiPanel{
public static void main(String [] args){
new MyFrame2("MyFrameWithPanel",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,h/2,w/2,h/2);
p3.setBounds(w/2,0,w/2,h/2);
p4.setBounds(w/2,h/2,w/2,h/2);
p1.setBackground(Color.blue);
p2.setBackground(Color.green);
p3.setBackground(Color.yellow);
p4.setBackground(Color.magenta);
add(p1);add(p2);
add(p3);add(p4);
setBounds(x,y,w,h);
setVisible(true);
}
}