[转贴]马士兵Java学习笔记

上一篇 / 下一篇  2011-05-08 17:28:19 / 个人分类:JAVA

1101

GUIGraphics User Interfaca)图形用户界面

AWTAbstract Window Toolkit)包括许多类和接口用于实现GUI编程

ContainerComponent(可以显示的图形元素)是AWT的两个核心类。ContainerComponent的子类。Container用来容纳其他Component元素

Container包含Window(包含FrameDialog)和Panel。但是Window作为一个应用程序的出口可以独立显示出来,其表示对象自由停泊的顶级窗口;Panel不能作为一个应用程序的出口可以独立显示出来,要想显示得把自己装到其他的Container里(比如WindowApplet)。

 

Frame

例子:

范例名称:Frame应用举例

源文件名称:TestFrame.java

 点:Frame组件的创建及显示设置

 

import java.awt.*;

public class TestFrame. {

public static void main( String args[]) {

                                   Frame. f = new Frame("My First Test");

f.setLocation(300, 300);//指定Frame左上角的坐标

f.setSize( 170,100);//单位是像素

f.setBackground( Color.blue);

f.setResizable(false);//设置是否可以更改大小

f.setVisible( true);//设置可见

}

}

 

 

1102

Frame

例子:

import java.awt.*;

public class TestMultiFrame. {

   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.GREEN);

       MyFrame. f4 = new MyFrame(300,300,200,200,Color.MAGENTA);

   }

}

 

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);

   }

}

 

 

Panel

Panel对象可以看成可以容纳Component的空间

Panel对象可以拥有自己的布局管理器

 

例一:

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));// 0,0,102代表红绿蓝三原色的分量

        p.setBounds(50,50,400,400);//相对于frame的坐标

        p.setBackground(new Color(204,204,255));

        f.add(p);//panel添加到frame

        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);//null表示此panel不带自己的布局管理器

       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);

   }

}

 

1103

布局管理器1

Awt提供了五种布局管理器

FlowLayout

BorderLayout

GridLayout

CardLayout

GridBagLayout

 

FlowLayoutPanel类的默认布局管理器,逐行开始排列从左到右。FlowLayout默认对齐方式是居中

 

例子:

范例名称:FlowLayout用法举例

源文件名称:TestFlowLayout.java

 点:

            1.布局管理器的概念和作用

       2. FlowLayout的性质及用法

 

import java.awt.*;

public class TestFlowLayout {

   public static void main(String args[]) {

           Frame. f = new Frame("Flow Layout");

       Button button1 = new Button("Ok");

       Button button2 = new Button("Open");

       Button button3 = new Button("Close");

       f.setLayout(new FlowLayout(FlowLayout.LEFT));//指定布局管理器,且左对齐

       f.add(button1);

       f.add(button2);

       f.add(button3);

       f.setSize(100,100);

       f.setVisible(true);

   }

}

 

TAG:

 

评分:0

我来说两句

Open Toolbar