zoukankan      html  css  js  c++  java
  • AWT


     

    GUI(Graphical User Interface),图形用户界面。

     

    AWTAbstract Window Toolkit),抽象窗口工具集,第一代的java GUI组件,是重量

    级的

     

    Swing,不依赖于底层细节,轻量级的组件。

     

    Window有两种形式,一种是Frame ,一种是Dialog

     

    Frame是一个顶级窗口,缺省为BorderLayout布局管理器

     

    Panel无法单独显示,必须添加到某个容器中,缺省为FlowLayout布局管理器。

     

    Frame的使用

    public class FrameTest

    {

        public static void main(String[] args)

        {

           Frame frame = new Frame();

          

           frame.setSize(500,500);

           frame.setBackground(Color.GRAY);

           frame.setTitle("AWT first App");

          

           frame.setVisible(true);

        }

    }

     

     

     

    带容器窗口的实现

    import java.awt.Color;

    import java.awt.Frame;

    import java.awt.Panel;

     

    public class FrameWithPanel extends Frame

    {

        public FrameWithPanel(String title)

        {

           super(title);

        }

       

        public static void main(String[] args)

        {

           FrameWithPanel frame = new FrameWithPanel("Frame with panel");

           Panel panel = new Panel();

          

           frame.setSize(200, 200);

           frame.setBackground(Color.BLACK);

           frame.setLayout(null);

          

           panel.setSize(100 , 100);

           panel.setBackground(Color.YELLOW);

          

           frame.add(panel);

           frame.setVisible(true);

        }

    }

     

    带按钮的窗口

    public class ExGui extends Frame

    {

        private Frame frame;

        private Button btn1;

        private Button btn2;

       

        public void go()

        {

           frame = new Frame("gui example");

          

           frame.setLayout(new FlowLayout());

          

           btn1 = new Button("Press me");

           btn2 = new Button("Don't Press me");

          

           frame.add(btn1);

           frame.add(btn2);

          

           frame.pack();

           frame.setVisible(true);

          

        }

       

        public static void main(String[] args)

        {

           ExGui ex = new ExGui();

           ex.go();

        }

    }

     

     

     

    布局管理器

    BorederLayout

    FlowLayout

    GridLayout

    CardLayout

    GridBagLayout

     

     

    FlowLayout的使用,流式布置,按钮的位置会随窗口的大小改变

     

    public class MyFlow

    {

        private Frame frame;

       

        private Button btn1,btn2,btn3;

       

        public void go()

        {

           frame = new Frame("Flow Layout");

          

           frame.setLayout(new FlowLayout());

          

           btn1 = new Button("btn1");

           btn2 = new Button("btn2");

           btn3 = new Button("btn3");

          

           frame.add(btn1);

           frame.add(btn2);

           frame.add(btn3);

          

           frame.setSize(100,100);

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           MyFlow flow = new MyFlow();

          

           flow.go();

        }

    }

     

    BorderLayout的使用,固定按钮到某一面上(东,南,西,北,中)

    public class ExGui2 extends Frame

    {

        private Frame frame;

       

        private Button bn,bs,bw,be,bc;

       

        public void go()

        {

           frame = new Frame("Border Layout");

          

           bn = new Button("north");

           bs = new Button("south");

           bw = new Button("west");

           be = new Button("east");

           bc = new Button("center");

          

           frame.add(bn, BorderLayout.NORTH);

           frame.add(bs, BorderLayout.SOUTH);

           frame.add(bw, BorderLayout.WEST);

           frame.add(be, BorderLayout.EAST);

           frame.add(bc, BorderLayout.CENTER);

          

           frame.setSize(200,200);

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           ExGui2 ex = new ExGui2();

           ex.go();

        }

    }

     

    GridLayout的使用,用表格对按钮进行定位。会忽略组件的大小,使它们有相同的大小

    public class GridEx

    {

        private Frame frame;

       

        private Button b1,b2,b3,b4,b5,b6;

       

        public void go()

        {

           frame = new Frame("Grid Layout");

          

           frame.setLayout(new GridLayout(3,2));

          

           b1 = new Button("1");

           b2 = new Button("2");

           b3 = new Button("3");

           b4 = new Button("4");

           b5 = new Button("5");

           b6 = new Button("6");

          

           frame.add(b1);

           frame.add(b2);

           frame.add(b3);

           frame.add(b4);

           frame.add(b5);

           frame.add(b6);

          

           frame.setSize(200,200);

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           GridEx ex = new GridEx();

           ex.go();

        }

    }

     

    CardLayout的使用

    package com.anllin.awt;

     

    import java.awt.BorderLayout;

    import java.awt.CardLayout;

    import java.awt.Color;

    import java.awt.Container;

    import java.awt.FlowLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

     

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JPanel;

     

    public class CardLayoutTest

    {

        public static void main(String[] args)

        {

           MyFrame frame = new MyFrame();

           frame.setVisible(true);

        }

    }

     

    class MyFrame extends JFrame

    {

        JPanel Pleft = new JPanel();

        JPanel Pright = new JPanel();

        CardLayout car;

     

        public MyFrame()

        {

           this.setBounds(80, 60, 600, 300);

           this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

           Container c = this.getContentPane();

           c.setLayout(new BorderLayout());

     

           Pleft.setBackground(Color.red);

           Pright.setBackground(Color.blue);

     

           c.add(Pleft, BorderLayout.CENTER);

           c.add(Pright, BorderLayout.EAST);

     

           Pright.setLayout(new FlowLayout());

     

           JButton[] b = new JButton[10];

           for (int i = 0; i < 10; i++)

           {

               b[i] = new JButton("" + i + "");

               Pleft.add(b[i]);

           }

           car = new CardLayout();

           Pleft.setLayout(car);

     

           JButton b1 = new JButton("next");

           class MyEvent implements ActionListener

           {

               public void actionPerformed(ActionEvent e)

               {

                  car.next(Pleft);

               }

           }

           b1.addActionListener(new MyEvent());

           Pright.add(b1);

        }

    }

     

    GridBagLayout的使用

    GridBagLayout
    【常用构造函数】

        public GridBagLayout()

    【特点】:

          GridBagLayout
    是所有AWT布局管理器当中最复杂的,同时他的功能也是最强大的.这种现象源于它所提供的众多的可配置选项,你几乎可以完全地 控制容器的布局方式.尽管复杂性很明显,只要理解了基本思想,就很容易使用GridBagLayout.
          GridBagLayout
    从它的名字中你也可以猜到,它同GridLayout一样,在容器中以网格形式来管理组件.GridBagLayout功能要来得强大得多.
    1
    GridBagLayout管理的所有行和列都可以是大小不同的.
    2
    GridLayout把每个组件限制到一个单元格,GridLayout并不这样:组件在容器中可以占据任意大小的矩形区域,
          GridBagLayout
    通常由一个专用类来对他布局行为进行约束,该类叫GridBagConstraints.其中的所有成员都是public, 因此要学好如何使用GridBagLayout首先要了解有那些约束变量,以及如何设置这些约束变量.
    以下是GridBagConstraints的公有成员变量
      public int anchor
      public int fill
      public gridheight
      Public gridweight
      public girdx
      public gridy
      public Insets insets
      public int ipadx
      public int ipady
      public double weightx
      public double weighty
         
    看起来有很多约束需要进行设置,但事实上许多约束只需设置一次,并对多个组件重用,每次添加组件时只有少数的项需要修改.
         
    下面是一个具有简单约束的GridBagLayout示例
    public class GridBagLayoutExample2 extends JPanel {

    public GridBagLayoutExample2() {

    this.setLayout(new GridBagLayout());
    this.setOpaque(true);
    GridBagConstraints c = new GridBagConstraints();
    JButton b = new JButton ("One"
    ;
    c.gridx = 0 ;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 1;
    this.add(b,c);//button 1 added
    c.gridy++;
    b= new JButton("Two"
    ;
    this.add(b,c);

    c.gridx = 2;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 2;
    b = new JButton("Three"
    ;
    this.add(b,c);

    c.gridx = 0 ;
    c.gridy = 2;
    c.gridwidth = 4;
    c.gridheight =1 ;
    this.add(new JTextField(35),c);
    }

    public static void main(String[] args) {
    JFrame f = new JFrame("GridBagLayout 2"
    ;
    JPanel p = new GridBagLayoutExample2();
    f.getContentPane().add(p);
    f.pack();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setVisible(true);

    }

    }

     

    图表 1

         
    上述示例首先生成了约束对象c , 并设置了它的属性gridx , gridy , gridwidth, girdheight四个属性,下面简要介绍这四个属性的作用
    gridx, gridy
    这个属性是用来描述组件在布局时应处于那个网格位置,即描述网格开始的位置
    gridwidth,gridheigh
    这两个属性用来描述组件在布局中的所占的网格的个数,gridwidth描述了该组件在横向上所占网格的个数,gridheight描述了该组件在纵向上所占网格的个数.
         
    你也可以通过GridBagConstraintsRELETIVE,REMAINDER来进行指定,它的用法是:
         
    当把gridx值设置为GridBagConstriants.RELETIVE,在一个组件添加时,将被放置在前一个组件的右侧.同理,gridy 值设置为GridBagConstraints.RELETIVE,在添加组件时,将被放置在前一个组件的下方,(这是一种根据前一个组件而决定当前组 件的相对放置方式)
         
    gridweightgridheight也可以应用GridBagConstraintsREMAINDER方式,创建的组件会从创建的起点位置 开始一直延伸到容器所能允许的限度为止.该功能使得你可以创建跨越某些行或列的组件,从而改变相应方向上组件的数目,即使其后在布局的其他地方添加额外的 组件也是如此.
         
    你可以注意到图表1buttonOnebuttonTwo上下紧靠,你可以通过设置GridBagConstraints成员Insets来调整它周围的空间大小,
    new Insets(int top , int left , int bottom, int right)
          c.insets = new Insets(4,4,4,4);


     

    图表2


         
    接下去要解决的一个问题是容器缩放时的行为.对于文本栏这个问题是最明显的,无论容器如何变形,它都保持同样的大小.文本区域应该总是跨越在容器的底部,但你肯定不希望在纵向缩放时,文本栏也纵向增长.
          weightx
    weighty成员就是用来控制在容器变形时,单元格本身如何缩放.这两个属性都是浮点数值,描述了每个单元格在拉伸时横向或纵向等到的分配比例.如对上例中的weightx设置
          button one 0.4
          button two 0.4
          button three 0.6
          text area 1.0
       
    则在拉伸过程中,假设拉伸了10个象素,button one横向得到10*0.4 = 4个象素
        button three
    得到了剩余的6个象素.在拉伸过程中按钮one和按钮three0.4:0.6的方式放大
       
    为了放置纵向拉伸时,文本框的纵向扩展,你可以把weighty设置为0

    图表3

     

     

    图表4


       
    你可能已经注意到,尽管我们已经将文本框的weight设置为1.0,但它并没有占据额外的可用的横向空间,即并没有在横向拉伸时始终占据整个底部行.
    所以出现这个问题,是因为单元格和组件之间的区别还没有弄清楚,weightxweighty值控制的是容器增长时单元格扩展的程度,但它们对各个单元 格中的组件并没有直接的效应.实际上,当窗口扩展时容器的所有单元格都增长了,包括文本框所在的一横行单元格.但文本栏根本没有增长.这是因为在所分配的 单元格内部,组件的增长是由GridBagConstraints对象的fill成员控制的,它可取下列值
    GridBagConstraints.NONE
    不增长
    GridBagConstraints.HORIZONTAL
    只横向增长
    GridBagConstraints.VERTICAL
    只纵向增长
    GridBagConstraints.BOTH  
    双向增长
    当你创建一个GridBagConstraints对象时,fill值设置为NONE,因此在单元格增长时,单元格内部组件不会增长.

    图表5




          ipadxipady两个属性:

    GridBagLayout 对容器进行布局时,它把每个组件的最小尺寸作为如何分配空间的一个约束条件来考虑;如果一个按钮的最小尺寸是30象素宽,20象素高,而相关联的约束对象 ,ipadx4,ipady2,那么按钮的最小尺寸将会成为横向38象素,纵向24象素.
          anchor
    属性:
    当组件在横向或纵向上小于所分配到的单元格面积时,该字段会起作用.在这些情况下,anchor将决定组件如何在可用的空间中对齐.默认情况下,组件固定在单元格的中心,周围均匀分布多余空间.你也可以指定其他对齐方式:
    GridBagConstraints.NORTH
    GridBagConstraints.SOUTH
    GridBagConstraints.NORTHWEST
    GridBagConstraints.SOUTHWEST
    GridBagConstraints.SOUTHEAST
    GridBagConstraints.NORTHEAST
    GridBagConstraints.EAST
    GridBagConstraints.WEST

    图表6

     

     

    综合使用

    public class ExGui3

    {

        private Frame frame;

        private Panel panel;

        private Button b1, b2, b3, b4;

       

        public void go()

        {

           frame = new Frame("Complex Layout");

           b1 = new Button("west");

           b2 = new Button("hello");

          

           frame.add(b1,BorderLayout.WEST);

           frame.add(b2,BorderLayout.CENTER);

          

           panel = new Panel();

          

           b3 = new Button("world");

           b4 = new Button("welcome");

          

           panel.add(b3);

           panel.add(b4);

          

           frame.add(panel,BorderLayout.NORTH);

          

           frame.setSize(200,200);

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           ExGui3 ex = new ExGui3();

           ex.go();

        }

    }

     

     

    AWT事件模型

     

    事件描述发生了什么的对象。(比如ActionEvent

    事件源事件的产生器(比如Button

    事件处理器接收事件、解释事件并处理用户交互的方法(比如ActionHandler

    事件监听器实现了监听器接口的类。一个监听器对象是一个实现了专门的监听器的类的实例。(ActionListeneràActonPerformed()

     

    委托模型

    优点:

    --事件不会被意外地处理

    --有可能创建并使用适配器(adapter)类对事件动作进行分类。

    --委托模型有利于把工作分布到各个类中。

     

    Public class TestButton

    {

        public static void main(String[] args)

        {

           Frame frame = new Frame("Test Button");

          

           Button button = new Button("Press me");

          

           button.addActionListener(new ButtonHandler());

          

           frame.add(button,BorderLayout.CENTER);

          

           frame.setSize(200, 200);

          

           frame.pack();

          

           frame.setVisible(true);

        }

    }

     

    class ButtonHandler implements ActionListener

    {

        @Override

        public void actionPerformed(ActionEvent e)

        {

           String label = e.getActionCommand();

          

           System.out.println(label);

        }

    }

     

     

    几类具有典型代表意义的事件:

    MouseEvent—鼠标的相关事件,如鼠标移动等

    WindowEvent—窗口的相关事件,如窗口关闭等

    ActionEvent—组件本身的相关事件,如被点击等。

     

    public class TwoListen implements MouseMotionListener,MouseListener

    {

        private Frame frame;

       

        private TextField textField;

       

        public void go()

        {

           frame = new Frame("Two Listeners Example");

          

           frame.add(new Label("click"),BorderLayout.NORTH);

          

           textField = new TextField(30);

          

           frame.add(textField,BorderLayout.SOUTH);

          

           frame.addMouseMotionListener(this);

           frame.addMouseListener(this);

          

           frame.addMouseListener(new MyMouseListener());

           frame.addWindowListener(new WindowHandle());

          

           frame.setSize(300,200);

          

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           TwoListen listen = new TwoListen();

           listen.go();

        }

     

        @Override

        public void mouseClicked(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mousePressed(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mouseReleased(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mouseEntered(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mouseExited(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "The mouse has left the frame";

        }

     

        @Override

        public void mouseDragged(MouseEvent e)

        {

            // TODO Auto-generated method stub

           String str = "x:" + e.getX() + ",y:" + e.getY();

           this.textField.setText(str);

        }

     

        @Override

        public void mouseMoved(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

    }

     

    class MyMouseListener implements MouseListener

    {

     

        @Override

        public void mouseClicked(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mousePressed(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mouseReleased(MouseEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void mouseEntered(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "The Mouse has entered the Frame";

          

           System.out.println(str);

        }

     

        @Override

        public void mouseExited(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "The Mouse has Exited the Frame";

          

           System.out.println(str);

        }

    }

     

    class WindowHandle implements WindowListener

    {

     

        @Override

        public void windowOpened(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void windowClosing(WindowEvent e)

        {

           // TODO Auto-generated method stub

           System.exit(0);

        }

     

        @Override

        public void windowClosed(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void windowIconified(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void windowDeiconified(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void windowActivated(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

     

        @Override

        public void windowDeactivated(WindowEvent e)

        {

           // TODO Auto-generated method stub

          

        }

       

    }

     

     

    事件适配器(Adapter

    --让你只对感兴趣的事件进行监听。

    public class TwoAdapter extends WindowAdapter

    {

        private Frame frame;

       

        private TextField textField;

       

        public void go()

        {

           frame = new Frame("Two Listeners Example");

          

           frame.add(new Label("click"),BorderLayout.NORTH);

          

           textField = new TextField(30);

          

           frame.add(textField,BorderLayout.SOUTH);

          

           frame.addMouseListener(new MyAdapter());

          

           frame.setSize(300,200);

          

           frame.setVisible(true);

        }

       

        public static void main(String[] args)

        {

           TwoListen listen = new TwoListen();

           listen.go();

        }

       

        @Override

        public void windowClosing(WindowEvent e)

        {

           // TODO Auto-generated method stub

           System.exit(0);

        }

    }

     

    class MyAdapter extends MouseAdapter

    {

        @Override

        public void mouseEntered(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "The Mouse has entered the Frame";

          

           System.out.println(str);

        }

     

        @Override

        public void mouseDragged(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "x:" + e.getX() + ",y:" + e.getY();

           System.out.println(str);

        }

       

        @Override

        public void mouseExited(MouseEvent e)

        {

           // TODO Auto-generated method stub

           String str = "The Mouse has Exited the Frame";

          

           System.out.println(str);

        }

    }

     

     

    MouseAdapter的底层实现

    它实现了MouseListener, MouseWheelListener, MouseMotionListener接口,但是是对接口的空实现,这样当我们去继承它时,就可以只对我们感兴趣的方法进行重写。

    public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {

        /**

         * {@inheritDoc}

         */

        public void mouseClicked(MouseEvent e) {}

     

        /**

         * {@inheritDoc}

         */

        public void mousePressed(MouseEvent e) {}

     

        /**

         * {@inheritDoc}

         */

        public void mouseReleased(MouseEvent e) {}

     

        /**

         * {@inheritDoc}

         */

        public void mouseEntered(MouseEvent e) {}

     

        /**

         * {@inheritDoc}

         */

        public void mouseExited(MouseEvent e) {}

     

        /**

         * {@inheritDoc}

         * @since 1.6

         */

        public void mouseWheelMoved(MouseWheelEvent e){}

     

        /**

         * {@inheritDoc}

         * @since 1.6

         */

        public void mouseDragged(MouseEvent e){}

     

        /**

         * {@inheritDoc}

         * @since 1.6

         */

        public void mouseMoved(MouseEvent e){}

    }

     

     

     

     

  • 相关阅读:
    sql sever 数据字典语法
    端口使用情况
    koa中间件说明
    FLIP动画思想
    跨域下载文件显示文件名
    post方法打开新页面并提交参数
    常用快捷键
    cnpm与npm安装的包不一样
    chrome devTools变量不提示,断点点击去不掉问题
    未修改的模块失效排查方法
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2143665.html
Copyright © 2011-2022 走看看