
Awt介绍
- Abstract windows tools ,包含了很多的类和接口
- 元素:窗口、按钮、文本框
- java.awt包
如何使用这些类??
组件和容器.setVisible(true/false)
.setBackground(new Color(R,G,B,透明度))
.setLocation(x,y) 默认左上角
.setResizable(true/false); 能否改变窗口大小
import java.awt.*;
//GUI的第一个类
public class Testframe {
public static void main(String[] args) {
//frame
frame f = new frame("我的第一个Java图形界面窗口");
//可见性 默认没有长宽高
f.setVisible(true);
//设置窗口大小
f.setSize(400,400);
//颜色
f.setBackground(new Color(34, 175, 232, 255));
//d出的初始位置(默认最中间),0,0点在最左上角
f.setLocation(200,200);
//设置大小固定
f.setResizable(false);
}
}
Panel面板
特性:可以看成一个空间,在frame上,不能单独存在。
package com.Gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
frame f = new frame();
Panel p = new Panel();
f.setLayout(null);
f.setBounds(300,300,500,500);
f.setBackground(new Color(107, 208, 121));
//设置面板panel坐标,相对于frame
p.setBounds(50,50,400,400);
p.setBackground(new Color(255, 0, 0));
f.setVisible(true);
f.add(p);
p.setVisible(true);
f.addWindowListener(new WindowAdapter() {
//点击窗口关闭时做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
三种布局管理器
流式布局
package com.Gui;
import java.awt.*;
public class WindowLayout {
public static void main(String[] args) {
frame frame = new frame();
//按钮
Button button1 = new Button("固傻");
Button button2 = new Button("固傻1");
Button button3 = new Button("gusha");
//设置流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
frame.setVisible(true);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
东西南北中
package com.Gui;
import java.awt.*;
public class WindowLayout {
public static void main(String[] args) {
frame frame = new frame("GUSHA");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(300,600);
frame.setVisible(true);
}
}
表格布局
案例:做出如图所示的画面:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)