首页 | 手机版 | 三国演义 | 三国志 | 史将 | 背景 | 藏书阁
首页 -> 精彩文章 -> 国培JAVA编程笔记第七章3

国培JAVA编程笔记第七章3

作者wangger 标签java 阅读次数:3

一、布局管理器
    布局管理器是系统提供的,用来帮助进行组件的布局。常用的布局管理器有:流式布局管理器、边界布局管理器,网格布局管理器。
    1、流式布局管理器FlowLayout
 流式布局管理安排组件对齐的方式有三种:靠左对齐,靠右对齐,居中对齐。
 靠左对齐安排组件的方式是从左到右,从上到下。如一行放不下,会自动排到下一行。
    2、边界布局管理器BorderLayout
 边界布局管理器只有五个方位“上、下、左、右、中”,主要是做大布局。
  3、网格布局管理器GridLayout
 网格布局管理器分行和列。布局方式也是从左到右,从上到下。生成网格布局管理器时以行为准,列只做参考。
二、工具栏
 1、创建工具栏JToolBar;
 把工具栏加入顶层容器中
  2、创建按钮;
 使用位图ImageIcon创建按钮;
 再把按钮加到工具栏中。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Button extends J implements ActionListener{
 JButton btn[];
 Container container;//顶层容器
 JPanel panel;//中间容器
 JMenuBar mb;
 JMenu menu;
 JMenuItem menuItem[];
 ImageIcon img[];
 JButton tBtn[];
 JToolBar tb;
 
 public Button(){
  this.setTitle("布局管理器");
  this.setBounds(200, 200, 800, 600);
  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  //设置布局管理器为流式管理器左靠齐
  this.setLayout(new FlowLayout(FlowLayout.LEFT));
  //设置布局管理器为流式管理器左靠齐
//  this.setLayout(new FlowLayout(FlowLayout.RIGHT));
  //设置布局管理器为边界布局管理器
//  this.setLayout(new BorderLayout(5, 5));
  //设置布局管理器为网格布局管理器
//  this.setLayout(new GridLayout(3, 2));
  container = this.getContentPane();
  this.setLayout(null);
  panel = new JPanel();
  panel.setBounds(0,90, 800, 450);
  container.add(panel);
  initMenu();
  addMenu();
  initImage();
  initToolButton();
  initButton();
//  initBorder();
  addButton();
  this.setVisible(true);
 }
 //初始化图像的类
 public void initImage(){
  img = new ImageIcon[6];
  String[] str = new String[6];
  for(int n = 0; n < img.length; n++){
   str[n] = n + ".gif";//图片的名字
   img[n] = new ImageIcon(str[n]);//生成图片对象
  }
 }
 public void initToolButton(){
  tBtn = new JButton[6];
  tb = new JToolBar();
  for(int n = 0; n < tBtn.length; n++){
   tBtn[n] = new JButton(img[n]);
   tb.add(tBtn[n]);
   tBtn[n].addActionListener(this);
  }
  tb.setBounds(0, 30, 500, 60);
  container.add(tb);
 }
 public void initButton(){
  btn = new JButton[6];
  btn[0] = new JButton("流式布局靠左");
  btn[1] = new JButton("流式布局靠右");
  btn[2] = new JButton("流式布局居中");
  btn[3] = new JButton("边界布局");
  btn[4] = new JButton("网格布局3*2");
  btn[5] = new JButton("网格布局2*3");
 }
 public void initMenu(){
  menuItem = new JMenuItem[6];
  menuItem[0] = new JMenuItem("流式布局靠左");
  menuItem[1] = new JMenuItem("流式布局靠右");
  menuItem[2] = new JMenuItem("流式布局居中");
  menuItem[3] = new JMenuItem("边界布局");
  menuItem[4] = new JMenuItem("网格布局3*2");
  menuItem[5] = new JMenuItem("网格布局2*3");
  menu = new JMenu("流式布局");
  mb = new JMenuBar();
 }
 public void addMenu(){
  
  for(int n = 0; n < menuItem.length; n++){
   menu.add(menuItem[n]);
   menuItem[n].addActionListener(this);
  }
  mb.add(menu);
  mb.setBounds(0, 0, 500, 30);
  container.add(mb);
 }
 public void addButton(){
  
  for(int n = 0; n < btn.length; n++){
   panel.add(btn[n]);
   btn[n].addActionListener(this);
  }
 }
 //初始化边界布局管理器
 public void initBorder(){
  panel.add(btn[0], "North");//上
  panel.add(btn[1], "South");//下
  panel.add(btn[2], "West");//左
  panel.add(btn[3], "East");//右
  panel.add(btn[4], "Center");//中
 }
 public void actionPerformed(ActionEvent arg0) {
  if(arg0.getSource() == btn[0] || arg0.getSource() == menuItem[0]
              || arg0.getSource() == tBtn[0]){
   panel.setLayout(new FlowLayout(FlowLayout.LEFT));
   this.addButton();
  }else{
   if(arg0.getSource() == btn[1] || arg0.getSource() == menuItem[1] || arg0.getSource() == tBtn[1]){
    panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    this.addButton();
   }else{
    if(arg0.getSource() == btn[2] || arg0.getSource() == menuItem[2] || arg0.getSource() == tBtn[2]){
     panel.setLayout(new FlowLayout(FlowLayout.CENTER));
     this.addButton();
    }else{
     if(arg0.getSource() == btn[3] || arg0.getSource() == menuItem[3] || arg0.getSource() == tBtn[3]){
      panel.setLayout(new BorderLayout());
      this.initBorder();
     }else{
      if(arg0.getSource() == btn[4] || arg0.getSource() == menuItem[4] || arg0.getSource() == tBtn[4]){
       panel.setLayout(new GridLayout(3, 2));
       this.addButton();
      }else{
       if(arg0.getSource() == btn[5] || arg0.getSource() == menuItem[5] || arg0.getSource() == tBtn[5]){
        panel.setLayout(new GridLayout(2, 3));
        this.addButton();
       }
      }
     }
    }
   }
  }
  this.validate();//让界面重新刷新,让改动生效.
 }
}



浙ICP备06020153号-1