GUI编程
GUI 即(Grahics User Interface 图形界面化编程),经常用Swing和AWT去实现,下面介绍一下AWT的相关知识,
AWT
AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI编程。
Container和Component是AWT中的两个核心类。
Container里面又分为Window和Pannel,window是可以独立显示出来的,它里面包含了Frame(我们平时看到的一般窗口)和Dialog(就是隐藏的一些窗口,需要点击下拉菜单才能看见的对话框),Pannel可以容纳其他的图形元素,一般看不见Pannel ,要显示出来,必须加入到Window才能显示出来。
Component 所有的可以显示出来的图形元素都称为Component,也包含了Container。
代码实现Frame创建的窗口
public class MyTest {
public static void main(String[] args) {
MyFrame f1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame f2 = new MyFrame(300,100,200,200,Color.yellow);
MyFrame f3 = new MyFrame(100,300,200,200,Color.red);
MyFrame f4 = new MyFrame(300,300,200,200,Color.MAGENTA);
}
}
class MyFrame extends Frame{
static int id=0;
public MyFrame(int x,int y,int w,int h, Color color) {
super("MyFrame"+id++);
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
setLayout(null);
}
}
输出结果:
Awt提供了5种布局管理器**
1.FlowLayout
2.BorderLayout
3.GriderLayout
4.GardLayout
5.GridBagLayout
BorderLayout 边界布局的应用
public class MyTest2 {
public static void main(String[] args) {
Frame frame = new Frame("边界布局");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.add(new Button("East"),BorderLayout.EAST);
frame.add(new Button("West"),BorderLayout.WEST);
frame.add(new Button("South"),BorderLayout.SOUTH);
frame.add(new Button("North"),BorderLayout.NORTH);
frame.add(new Button("Center"),BorderLayout.CENTER);
}
}
输出结果:
GriderLayout 表格布局的应用
public class MyTest3 {
public static void main(String[] args) {
Frame frame = new Frame("表格布局");
frame.setVisible(true);
frame.setBounds(100,200,300,400);
frame.setLayout(new GridLayout(3,2));//把页面分成三行两列
//把按钮放进去
frame.add(new Button("1"));
frame.add(new Button("3"));
frame.add(new Button("4"));
frame.add(new Button("5"));
frame.add(new Button("6"));
frame.add(new Button("7"));
/* frame.add(new Button("8"));*/
}
}
输出结果:
布局管理器的嵌套使用
public class MyTest4 {
public static void main(String[] args) {
Frame frame = new Frame("布局管理器的嵌套使用");
frame.setBackground(Color.RED);//设置背景颜色
frame.setBounds(100,100,300,400);//设置大小位置
frame.setVisible(true);//设置为可见的
frame.setLayout(new GridLayout(2,1));//设置表格为两行一列
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2, 1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2, 2));
p1.add(new Button("1"),BorderLayout.WEST );
p1.add(new Button("2"),BorderLayout.EAST);
p2.add(new Button("3"));
p2.add(new Button("4"));
//p1里面嵌套p2,把p2里面的按钮作为p的中间部分装入到p1里面
//把p2作为元素加入到p1里面
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("5"),BorderLayout.WEST);
p3.add(new Button("6"),BorderLayout.EAST);
for (int i = 0; i < 4; i++) {
p4.add(new Button("键"+i));
}
//p3里面嵌套p4,把p4里面的按钮作为p的中间部分装入到p3里面
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {//创建监听,点击窗口的关闭按钮就可以关闭
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
输出结果:
利用监听事件创建一个计算器窗口
public class MyTest5 {
public static void main(String[] args) {
Frame frame = new Frame("计算器");
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setBounds(100,100,500,500);
Button button = new Button("=");
Label label = new Label("+"); //“+”是一个静态文本,所以使用Label类创建一个静态文本对象
TextField t1 = new TextField(10);
TextField t2 = new TextField(20);
TextField t3 = new TextField(30);
frame.add(t1);
frame.add(label);
frame.add(t2);
frame.add(button);
frame.add(t3);
button.addActionListener(new MyLove(t1,t2,t3)); //给等号按钮加上监听,让点击按钮后有响应事件发生
}
}
class MyLove implements ActionListener{
//为了使对按钮的监听能够对文本框也起作用
//所以在自定义类MyLover里面定义三个TextField类型的对象 num1,num2,num3,
//并且定义了MyLove类的一个构造方法 这个构造方法带有三个TextField类型的参数,
//用于接收 从TFFrame类里面传递过来的三个TextField类型的参数
//然后把接收到的三个TextField类型的参数赋值给在本类中声明的 三个TextField类型的参数num1,num2,num3
//然后再在actionPerformed()方法里面处理num1,num2,num3
TextField num1,num2,num3;
public MyLove(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
String t1 = num1.getText();
String t2 = num2.getText();
num3.setText(String.valueOf(Integer.parseInt(t1)+Integer.parseInt(t2)));
num1.setText("");
num2.setText("");
}
}
Graphics 类
每个Component都有一个paint(Graphics g)用于实现绘图目的,每次重画该Component时都自动调用paint方法。
public class MyTest6 {
public static void main(String[] args) {
new MyPaint().launchFrame();
//在main()方法里面并没有显示调用paint(Graphics g)方法
//可是当创建出Frame窗体后却可以看到Frame窗体上画出了圆和矩形
//这是因为paint()方法是一个比较特殊的方法
//在创建Frame窗体时会自动隐式调用
//当我们把Frame窗体最小化又再次打开时
//又会再次调用paint()方法重新把圆和矩形在Frame窗体上画出来
//即每次需要重画Frame窗体的时候就会自动调用paint()方法
}
}
class MyPaint extends Frame{
public void launchFrame(){
setBounds(200,200,640,480);
setVisible(true);
}
public void paint(Graphics g){
//paint(Graphics g)方法有一个Graphics类型的参数g
//我们可以把这个g当作是一个画家,这个画家手里拿着一只画笔
//我们通过设置画笔的颜色与形状来画出我们想要的各种各样的图像
g.setColor(Color.red);/*设置画笔的颜色*/
g.fillOval(100,100,100,100);/*画一个实心椭圆*/
g.setColor(Color.green);
Color color = g.getColor();//前面设置了画笔的颜色,现在就应该把画笔的初始颜色恢复过来
g.setColor(color);
}
}
输出结果: