AWT: Java图形界面编程
组件 compontent
元素
- Button
- TextArea
- Label
容器
window
Frame
在主函数中直接定义一个Frame对象,并设置相应属性,运行时就可以显现Frame组件
在Frame中可以添加面板和其他组件用add()方法
```java
Frame frame = new Frame();
//设置边界 bounds(x,y,w,h) 分别对应x坐标y坐标,宽和长
frame.setBounds(100, 100, 200, 200);
//设置背景颜色 color(r,g,b)分别对应red,green,blue
frame.setBackground(new Color(10,20,100));
//设置是否可以改变边框大小
frame.setResizable(true);
//设置布局 :有FlowLayout(),BorderLayout(),GridLayout()等布局
frame.setLayout(new FlowLayout());
//在frame中添加面板
frame.add(new Panel())
//添加关闭窗口监听
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//设置可视化
frame.setVisible(true);
```
Dialog
对话框,要和Frame连用
Dialog dialog = new Dialog(frame);
Panel
一般在panel中添加图形和按钮等组件,然后把panel加到frame中,单独无法使用
//在panel中画图形
panel.paint(new Graphics(){
@Override
public void clearRect(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void clipRect(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void copyArea(int arg0, int arg1, int arg2, int arg3,
int arg4, int arg5) {
// TODO Auto-generated method stub
}
@Override
public Graphics create() {
// TODO Auto-generated method stub
return null;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void drawArc(int arg0, int arg1, int arg2, int arg3,
int arg4, int arg5) {
// TODO Auto-generated method stub
}
@Override
public boolean drawImage(Image arg0, int arg1, int arg2,
ImageObserver arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void drawLine(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void drawOval(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void drawPolygon(int[] arg0, int[] arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void drawPolyline(int[] arg0, int[] arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void drawRoundRect(int arg0, int arg1, int arg2, int arg3,
int arg4, int arg5) {
// TODO Auto-generated method stub
}
@Override
public void drawString(String arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void drawString(AttributedCharacterIterator arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub
}
@Override
public void fillArc(int arg0, int arg1, int arg2, int arg3,
int arg4, int arg5) {
// TODO Auto-generated method stub
}
@Override
public void fillOval(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void fillPolygon(int[] arg0, int[] arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void setColor(Color arg0) {
// TODO Auto-generated method stub
}
});
//或者添加组件
panel.add(new Button());