GUI

GUI:Graphics User Interface 图形用户界面。
AWT:窗口框架
          问题:1、使用AWT做出的图形用户界面在所有的平台上都显得丑陋,功能也有限。
               2、最多使用4种字体。
               3、AWT用的是非常笨拙的、非面向对象的编程模式。
 AWT编程中有两个基类:Component和MenuComponent,其中component代表一个能以图形化方式显示出来,并且可以与用户交互的对象,而MenuComponent则代表图形界面的菜单组件,此外,AWT还有两个重要的概念:Container和LayoutManager,其中Container代表一种容器,可以承装普通的component;LayoutManager则是容器管理其他组件布局的方式。
 一、AWT容器
 component类提供如下的几个方法
 

setLocation(int x,int y);设置组件位置
 setSize(int width,int height);设置组件大小
 setBounds(int x,int y,int width,int height);同时设置组件的大小和位置
 setVisible(Boolean b);设置组件的可见性。


 容器类Container:
 

 Component add(Component comp):向容器中添加其他组件
  int getComponentCount();返回该容器的组件数量。
  Component[] getComonent():返回该容器的全部组件
  Component getComponentAt(int x,int y);返回值定点的组件。


  AWT提供两种主要的容器类型
  Window:可独立存在的顶级窗口。
  Panel:可作为容器容纳其他组件,但不能单独存在。
  二、布局管理器
  1、FlowLayout
     组件像流水一样向某个方向流动遇到障碍(边界)返回,重头开始。
     FlowLayout(int align,int hgap,int vgap):
 

   import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class FlowLayoutTest {
        public static void main(String[]args){
            Frame f = new Frame("测试窗口");
            f.setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
            for(int i = 0;i<11;i++){
                f.add(new Button("按钮"+i));
            }
            //事件监听器,关闭窗口
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            f.pack();
              //设置可视
            f.setVisible(true);
    
        }
    }

  2、BorderLayout
        BorderLayout将容器分为ESAT、SOUTH、WEST、NORTH、CENTER五个区域,普通组件可以被放置在这五个区域的任意一个中。
注意:1、当向BorderLayout 布局管理器的容器中添加组件时,需要指定要添加到哪个区域。如果没有指定添加到那个区域,则默认添加到中间。
         2、如果向同一个区域添加多个组件,后放进去的组件会覆盖先放入的。
         Frame 、Dialog、ScrollPanel默认使用BorderLayout 布局管理器。
         BorderLayout(int hgap,int vgap):
   

       

  import java.awt.*;
    import java.awt.event.*;
    
    public class BorderLayoutTest {
        public static void main(String[]args){
        Frame f = new Frame("测试窗口");
        f.setLayout(new BorderLayout(30,5));
        f.add(new Button("南"),BorderLayout.SOUTH);
        f.add(new Button("北"),BorderLayout.NORTH);
        f.add(new Button("中"),BorderLayout.CENTER);
        f.add(new Button("东"),BorderLayout.EAST);
        f.add(new Button("西"),BorderLayout.WEST);
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.pack();
        f.setVisible(true);
        }
    }

3、GridLayout:将容器分割成纵横分隔的网格,每个网格所占区域大小相同。当向使用GridLayout布局管理器的容器中添加组件时,默认从左向右、从上向下依次添加到每个网格。
GridLayout(int rows,int cols);       采用指定的行数列数,以及默认行间距和纵间距。
GridLayout(int rows,int cols,int hgap,int vgap):采用指定的行数列数,以及指定行间距和纵间距。

> import java.awt.*; import java.awt.event.WindowAdapter; import
> java.awt.event.WindowEvent;
> 
> public class GridLayoutTest {
>     public static void main(String[]args){
>         Frame f = new Frame("计算器");
>         Panel p = new Panel();
>         p.add(new TextField(30));
>         f.add(p,BorderLayout.NORTH);
>         Panel p2 =  new Panel();
>         p2.setLayout(new GridLayout(5,4,4,4));
>         String[]  name =  {"1","2","3","+","4","5","6","-","7","8",
>                 "9","*",".","0","=","/","sqrt","^"};
>         for(int i = 0;i<name.length;i++){
>             p2.add(new Button(name[i]));
>         }
>         f.add(p2);
>         f.pack();
>         f.addWindowListener(new WindowAdapter() {
>             @Override
>             public void windowClosing(WindowEvent e) {
>                 System.exit(0);
>             }
>         });
>         f.setVisible(true);
>     }  }

 4、GridBagLayout:一个组件可以跨越多个网格,并可以设置各网格大小互不相同,增加了布局灵活性·。
 使用步骤:
 ①、创建GridBagLayout布局管理器,并指定GUI容器使用该布局管理器
  

 GridBagLayout gb = new GridBagLayout();
   container.setLayout(gb);

②、创建GridBagContaints对象,并设置该对象相关属性
  

 GridBagContaints gbc= new GridBagContaints();
 

 gbc.gridx = 2;//设置受该对象控制的GUI组件位于网格的横向索引。
   gbc.gridy = 1;//设置受该对象控制的GUI组件位于网格的纵向索引。
   gbc.gridwidth = 2;//设置受该对象控制的GUI组件横向跨越多少网格。
    gbc.gridheight = 2;//设置受该对象控制的GUI组件纵向跨越多少网格。

   ③、调用GridBagLayout对象的方法来建立GridBagContraints对象和受控组件间的联系
    gb.setConstraints(c,gb);
④、添加组件,与采用普通布局管理器添加组件方法一样。
   

 container.add(c);
GridBagContraints.NONE:组件不扩大
GridBagContraints.HORIZONTAL:GUI组件水平扩大已占据空白区域。
GridBagContraints.VERTICAL:GUI组件垂直扩大以占据空白区域。
GridBagContraints.BOTH:GUI组件双向扩大。
GridBagContraints.CENTER(中间)
GridBagContraints.NORTH(上中)
GridBagContraints.NORTHWEST(左上角)
GridBagContraints.NORTHEAST(右上角)
GridBagContraints.SOUTH(下中)


......

 

  import java.awt.*;
    import java.awt.event.*;
    
    public class GridBagTest {
        private Frame f = new Frame("测试窗口");
        private GridBagLayout gb =  new GridBagLayout();
        private GridBagConstraints gbc = new GridBagConstraints();
        private Button[] bs = new Button[10];
        public void init(){
            f.setLayout(gb);
            for(int i = 0;i<bs.length;i++){
                bs[i] = new Button("按钮"+i);
            }
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx =1;
            addButton(bs[0]);
            addButton(bs[1]);
            addButton(bs[2]);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            addButton(bs[3]);
            gbc.weightx = 0;
            addButton(bs[4]);
            gbc.gridwidth = 2;
            addButton(bs[5]);
            gbc.gridwidth = 1;
            gbc.gridheight = 2;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            addButton(bs[6]);
            gbc.gridwidth =1;
            gbc.gridheight=2;
            gbc.weighty = 1;
            addButton(bs[7]);
            gbc.weighty = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.gridheight = 1;
            addButton(bs[8]);
            addButton(bs[9]);
            f.pack();
            f.setVisible(true);
        }
        private  void addButton(Button button){
            gb.setConstraints(button,gbc);
            f.add(button);
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
        public static void main(String[]args){
            new GridBagTest().init();
    
        }
    }


5、CardLayout:以时间而非空间来管理它的组件,它将加入容器的所有组件看成一叠卡片,每次只有最上面的可见。

   

 import java.awt.*;
    import java.awt.event.*;
    
    
    public class CardLayoutTest {
        Frame f = new Frame("测试窗口");
        String[] names = {"第一张","第二张","第三张","第四张","第五张"};
        Panel p1 = new Panel();
        public void init(){
            final CardLayout c= new CardLayout();
            p1.setLayout(c);
            for(int i = 0;i<names.length;i++){
                p1.add(names[i], new Button(names[i]));
            }
            Panel p = new Panel();
            ActionListener listener = e ->{
                switch (e.getActionCommand()){
                    case "上一张":
                        c.previous(p1);
                        break;
                    case "下一张":
                        c.previous(p1);
                        break;
                    case "第一张":
                        c.previous(p1);
                        break;
                    case "最后一张":
                        c.previous(p1);
                        break;
                    case "第三张":
                        c.previous(p1);
                        break;
                }
            };
            Button previous = new Button("上一张");
            previous.addActionListener(listener);
            Button next = new Button("下一张");
            next.addActionListener(listener);
            Button first = new Button("第一张");
            first.addActionListener(listener);
            Button last = new Button("最后一张");
            last.addActionListener(listener);
            Button third = new Button("第三张");
            third.addActionListener(listener);
            p.add(previous);
            p.add(next);
            p.add(last);
            p.add(first);
            p.add(third);
            f.add(p1);
            f.add(p,BorderLayout.SOUTH);
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            f.pack();
            f.setVisible(true);
        }
        public static void main(String[]args){
            new CardLayoutTest().init();
        }
    }


6、绝对定位
①、将Container布局管理器设成null:setLayout(null);
②、向容器中添加组件时,先调用setBounds()或setSize();设置大小,位置。

   

 import java.awt.*;
    import java.awt.event.*;
    
    
    public class NullLayoutTest {
        Frame f = new Frame("测试窗口");
        Button b1 = new Button("第一个按钮");
        Button b2 = new Button("第二个按钮");
        public void init(){
            f.setLayout(null);
            b1.setBounds(20,20,90,80);
            f.add(b1);
            b2.setBounds(50,45,120,35);
            f.add(b2);
            f.setBounds(50,50,200,200);
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            //f.pack();
            f.setVisible(true);
        }
        public static void main(String[]args){
               new NullLayoutTest().init();
        }
    }


7、BoxLayout:比GriBagLayout简单,可在垂直和水平两个方向上摆放GUI组件
   

   

 import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BoxLayoutTest {
        private Frame F = new Frame("测试");
        public void init(){
            F.setLayout(new BoxLayout(F,BoxLayout.Y_AXIS));
            F.add(new Button("第一个按钮"));
            F.add(new Button("按钮二"));
            F.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            F.setVisible(true);
        }
        public static void main(String[]args){
            new BoxLayoutTest().init();
        }
    }


    
    
    
       
       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值