GUI(Graphical User Interfaces):由各种图形对象组成的用户界面,在这种用户界面下,用户的命令和对程序的控制是通过“选择”各种图形对象来实现的。
Component:抽象类,定义了GUI组件的基本特性和功能
属性名 | 含义 | 设置属性的方法 | 获取属性的方法 |
visible | 可见性 | void setVisible(boolean) | boolean getVisible() |
background | 背景色 | void setBackground(Color) | Color getBackground() |
bounds | 边界 | void setBounds(Rectangle) void setBounds((int,int,int,int)【x,y,w,h】 |
Rectangle getBounds() |
size | 尺寸 | void setSize(Dimension)【w,h】 | Dimension getSize() |
location | 位置 | void setLocation(Point) void setLocation(int,int)【x,y】 |
Point getLocation() |
font | 字体 | void setFont(Font) | Font getFont() |
layoutMgr | 布局 | void setLayout(LayoutManager) | LayoutManager getLayout() |
foreground | 前景色 | void setForeground(Color) | Color getForeground() |
dropTarget | 拖放目标 | void setDropTarget(DropTarget) | DropTarget getDropTarget() |
enabled | 使能 | void setEnabled(boolean) | boolean getEnabled() |
cursor | 光标 | void setCursor(Cursor) | Cursor getCursor() |
locale | 地区 | void setLocale(Locale) | Locale getLocale() |
name | 组件名称 | void setName(String) | String getName() |
import java.awt.*; public class FirstFrame { public static void main(String[] args) { Frame f = new Frame("第一个图形用户界面"); Label l = new Label("这是我的第一个图形用户界面"); Button b = new Button("按钮"); f.setBackground(Color.yellow); l.setBackground(Color.pink); f.setBounds(100, 200, 300, 200); //f.setSize(300,200); //f.setLocation(100,200); f.add(l); f.add(b); f.setVisible(true); f.setLayout(new FlowLayout()); } }
import java.awt.*; public class FirstFrame2 extends Frame{ Label l; Button b; public FirstFrame2(){ super("第一个图形用户界面"); l = new Label("这是我的第一个图形用户界面"); b = new Button("按钮"); setBackground(Color.yellow); l.setBackground(Color.pink); setBounds(100, 200, 300, 200); //f.setSize(300,200); //f.setLocation(100,200); add(l); add(b); setVisible(true); setLayout(new FlowLayout()); } public static void main(String[] args) { new FirstFrame2(); } }
import java.awt.*; public class PaneDemo { public static void main(String[] args) { Frame f = new Frame("容器组件Pane的使用"); Panel p = new Panel(); Button b = new Button("确定"); p.setBackground(Color.pink); p.setBounds(50,50,80,60); f.setLayout(null); f.add(p); p.add(b); f.setBounds(200,200,200,160); f.setVisible(true); } }
import java.awt.*; public class FlowLayoutDemo{ public static void main(String[] args) { Frame f = new Frame("流动布局"); Button b1 = new Button("按钮1"); Button b2 = new Button("按钮2"); Button b3 = new Button("按钮3"); f.setLayout(new FlowLayout()); f.add(b1); f.add(b2); f.add(b3); f.setSize(200,300); f.setVisible(true); } }
import java.awt.*; public class BorderLayoutDemo extends Frame { Button bNorth,bSouth,bWest,bEast,bCenter; public BorderLayoutDemo(){ super("边框布局"); bNorth = new Button("按钮1"); bSouth = new Button("按钮2"); bWest = new Button("按钮3"); bEast = new Button("按钮4"); bCenter = new Button("按钮5"); add(bNorth,"North"); add(bSouth,"South"); add(bWest,"West"); add(bEast,"East"); add(bCenter,"Center"); setBounds(200,200,300,300); setVisible(true); } public static void main(String[] args){ new BorderLayoutDemo(); } }
import java.awt.*; public class GridLayoutDemo extends Frame { Button[] b = new Button[5]; public GridLayoutDemo(){ super("网格布局"); for(int i=0; i<b.length; i++){ b[i] = new Button("按钮"+i); } setLayout(new GridLayout(3,2)); add(b[0]); add(b[1]); add(b[2]); add(b[3]); add(b[4]); pack(); setSize(300,100); setLocation(100,200); //setBounds(200.100,300,100); setVisible(true); } public static void main(String[] args) { new GridLayoutDemo(); } }pack():最紧凑的格式摆放
import java.awt.*; public class CardLayoutDemo { public static void main(String[] args) { Frame f=new Frame("CardLayout Example"); CardLayout c1=new CardLayout(); f.setLayout(c1); Label lbl[]=new Label[4]; for(int i=0;i<4;i++){ lbl[i]=new Label("第"+i+"页"); f.add(lbl[i],"card"+i); } while(true){ try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } c1.next(f); } } }