界面编程-1

 

  
  
  
  
  1. AWT常用控件 
  2.     1. Frame窗口 
  3.         它是一个容器,初始状态是不可见且是最小化状态 
  4.         其常用的构造方法如下: 
  5.         Frame()  :构造一个默认的不可见的窗口实例 
  6.         Frame(String title) :具有指定标题的实例 
  7.         例如: 
  8.         Frame frame=new Frame("实例"); 
  9.         frame.setSize(412,123); 
  10.         frame.setVisible(true); 
  11.         也可以定义一个类继承Frame类 
  12.         import java.awt.*; 
  13.         import java.awt.event.*; 
  14.         class jjtt extends Frame{ 
  15.                 public jjtt(){ 
  16.                     initCompontent(); 
  17.                 } 
  18.  
  19.             private void initCompontent(){ 
  20.                 setTitle("实例"); 
  21.                 setSize(400,600); 
  22.                 addWindowListener(new WindowAdapter(){ 
  23.                     public void windowClosing(WindowEvent e){ 
  24.                         System.exit(0); 
  25.                     } 
  26.                 }); 
  27.             } 
  28.             public static void main(String args[]){ 
  29.                 new frame().setVisible(true); 
  30.             } 
  31.         } 
  32.          
  33. 2. Label标签 
  34.     Label label1=new Label("xxx");   //左对齐 
  35.     Label label1=new Label("xxx",Label.RIGHT);  //右对齐 
  36.  
  37. 3. Button 按钮 
  38.     Button quert=new Button(); 
  39.     query.setLabel("query"); 
  40.     Button exit=new Button("exit"); 
  41. 4. Checkbox复选框  CheckboxGroup 
  42.     构造方法如下: 
  43.     Checkbox()  无文本的复选框 
  44.     Checkbox(String label)  指定文本的复选框 
  45.     Checkbox(String label,boolean state)   并设置指定状态state 
  46.     Checkbox(String label,boolean state,CheckboxGroup group)  该复选框包含在复选框组group中 
  47.     Checkbox(String label,CheckboxGroup group,boolean state)   
  48.      
  49.     Checkbox foot=new Checkbox("foot");  Checkbox hand=new Checkbox("hand",true);  //默认选中 
  50.      
  51.     将多个复选框纳入一个组中就可以构成单选框 
  52.     CheckboxGroup cbg=new CheckboxGroup(); 
  53.     Checkbox married=new Checkbox("yes",false,cbg); 
  54.     Checkbox unmarried=new Checkbox("no",false,cbg); 
  55. 5. Panel 面板 
  56.     它是一种没有标题的容器,且不能独立存在,必须通过add方法添加到另一容器中,例如Frame,其构造方法如下: 
  57.     Panel()  :使用默认的布局管理器 
  58.     Panel(LayoutManager layout)  创建具有指定布局管理器的新面板 
  59.     add(Component comp)  添加指定的控件 
  60.     setColor c) 设置窗口的背景颜色 < /span>
  61.     setBounds(int x,int y,int width,int height) 设置指标位置和大小 
  62. 6. TextField文本框  TextArea 
  63.     TextField 主要用来接收单行信息,其构造方法如下: 
  64.     TextField()   
  65.     TextField(int columns)  :具有指定列数的对象 
  66.     TextField(String text)  :具有初始文本 
  67.     TextField(String text,int columns) 
  68.     例如: 
  69.     TextField textno=new TextField(10); 
  70.      
  71.      
  72.     TextArea 用来表示多行文本  Label用来表示一行文本 
  73.     其构造方法如下: 
  74.     TextArea() 
  75.     TextArea(int row,int col) 
  76.     TextArea(String text) 
  77.     TextArea(String text,int row,int col) 
  78.     TextArea(String text,int row,int col,int scrollbar)  具有滚动条的文本 
  79.     其中 
  80.     static int SCROLLBARS_BOTH  显示垂直和水平滚动条 
  81.     static int SCROLLBARS_HORIZONTAL_ONLY  水平 
  82.     static int SCROLLBARS_VERTICAL_ONLY 垂直 
  83.     static int SCROLLBARS_NONE 不显示 
  84. 7. 选项框Choice 和列表List 
  85.     Choice 类用来实现下拉选项框,且一次只能显示一个选项 
  86.     Choice choice=new Choice(); 
  87.      
  88.     void add(String item) 添加item项到选项框 
  89.     String getItem(int index)  返回指定索引上的选项 
  90.     int getItemCount()  返回选项的数量 
  91.     int getSelectIndex() 获取选定项的索引 
  92.     String getSelectItem() 获取选定项的字符串 
  93.     void insert(String item,int index) 将菜单插入到指定位置 
  94.     void remove(int position)  删除选项 
  95.     void remove(String item)   删除选项中第一个item 
  96.     void removeAll()  删除全部选项 
  97.     void select(int pos) 将指定的选项设为指定的项 
  98.     void select(String item) 将指定的选项设为指定的项    
  99.     Choice  city=new Choice(); 
  100.     city.add("wuhan"); 
  101.     city.add("hangzhou"); 
  102.     city.select(0); 
  103.      
  104.     List() 
  105.     为用户提供了一个可滚动的文本项列表,且允许进行单选或多项选择 
  106.     List() 
  107.     List(int row) 
  108.     list(int row,boolean multipleMode)  row行允许多选 
  109.     List list=new List(5,true); 
  110.     list.add("hangzhou"); 
  111.     list.add("beijing"); 
  112.     list.select(1); 
  113.      
  114. 8. Scrollbar 滚动条 和ScrollPanel滚动面板 
  115.     Scrollbar提供了一种在一定范围内的值中进行选择的便捷方式 
  116.     Scrollbar() 
  117.     Scrollbar(int orientation)   orientation是指定的滚动方向条 可以是 Scrollbar.HORIZONTAL 和Scrollbar.VERTIVAL 
  118.     Scrollbar(int orientation,int value,int visible,int min,int max) 指定方向 初始值 可见量(滑动块本身的宽度) 最小值 最大值 
  119.     final Scrollbar scroe=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,20); 
  120.      
  121.     ScrollPanel是一种容器类 
  122.     ScrollPanel() 
  123.     ScrollPanel(int displaypolicy)  其中displaypolicy为  
  124.     static int SCROLLBARS_ALWAYS 总是显示水平 垂直的滚动条 
  125.     static int SCROLLBARS_AS_NEEDED  只在需要的时候显示水平 垂直的滚动条 
  126.     static int SCROLLBARS_NEVER 不显示 
  127.     ScrollPanel memo=new ScrollPanel(SCROLLBARS_AS_NEEDED); 
  128.     memo.setSize(200,60); 
  129.     memo.add(new TextArea()) 
  130.  
  131. 9. 画布Canvas 
  132.     此控件表示屏幕上的一个矩形区域,要绘制图形我们需要创建一个继承自Canvas的类,然后覆盖paint()方法,其格式如下:  void paint(Graphics g) 
  133. 10. 菜单MenuBar Menu 和MenuItem 
  134.     首先创建菜单栏MenuBar 然后 创建Menu 和MenuItem 将MenuItem加入Menu 将Menu加入MenuBar 将MenuBar加到窗口 
  135.      
  136.     创建菜单栏对象 
  137.     MenuBar mb=new MenuBar(); 
  138.     创建菜单对象 
  139.     Menu file=new Menu("文件"); 
  140.     创建菜单项 
  141.     MenuItem newItem=new MenuItem("新建"); 
  142.     CheckboxMenuItem openItem=new CheckboxMenuItem("打开");   带对钩的菜单项 
  143.     将MenuItem加入Menu 
  144.     file.add(newItem); 
  145.     file.add(openItem); 
  146.     将Menu加入MenuBar 
  147.     mb.add(file); 
  148.          
  149.          
  150.          
  151.          
  152.          
  153. 布局管理器 
  154.     1. FlowLayout 布局 
  155.     其布局的策略是将控件按添加顺序从左到右在容器中排放,默认的对齐方式是居中对齐,其构造方法如下: 
  156.     FlowLayout()   
  157.     FlowLayout(int align) 构造一个具有一个指定对齐方式的布局,默认的是水平和垂直距离是5个单位 
  158.     FlowLayout(int align,int hgap,int vgap) 有指定的对齐方式,水平间距 垂直间距 
  159.      
  160.     FlowLayout.CENTER 
  161.     FlowLayout.LEFT 
  162.     FlowLayout.RIGHT 
  163.     FlowLayout.LEADING 
  164.     FlowLayout.TRALILING 
  165.     2. CardLayout布局 
  166.     此布局的原则是将控件放在一张卡片中,且每次只显示一张卡片,可以通过其提供的方法切换卡片 
  167.     CardLayout() 构造一个间距为0的布局器 
  168.     CardLayout(int hgap,int vgap)  构造一个具有指定水平间距  和垂直间距的布局器 
  169.     例如: 
  170.     CardLayout cl=new CardLayout(); 
  171.     其提供的方法如下: 
  172.     void first(Component parent)     翻转到第一张卡片 
  173.     void last(Component parent)      翻转到最后一张卡片 
  174.     void next(Component parent)      翻转到下一张卡片 
  175.     void previous(Component parent) 翻转到前一张卡片 
  176.     int getHgap()             获取控件之间的水平间距    
  177.     int getVgap()               获取控件之间的垂直间距  
  178.     int setVgap()              设置控件之间的垂直间距   
  179.     int setHgap()              设置控件之间的水平间距 
  180.     void show(Container parent,String name)     翻转到name的控件 
  181.     例如: 
  182.     设置窗口的布局: 
  183.     CardLayout cl=new CardLayout(); 
  184.     setLayout(cl); 
  185.     然后加入两个标签: 
  186.     add(new Label("1"),"lb11"); 
  187.     add(new Label("2"),"lb22");      //lb11是给标签的一个代号我们可以利用代号显示标签 
  188.     cl.show(this,"lb11"); 
  189.     cl.show(this,"lb22"); 
  190.     步骤是先声明布局对象,然后把控件加入窗口中(不需要显式的添加到布局管理器中),最后调用show函数显示控件 
  191.      
  192.     3. BorderLayout布局 
  193.     此布局将容器按照“上北下南,左西右东” 的原则排列,中间的区域占得空间最大。每个区域只能包括一个控件,其提供静态常量: 
  194.     BorderLayout.CENTER  BorderLayou.NORTH BorderLayou.SOUTH BorderLayou.WEST  BorderLayou.EAST   
  195.     其构造方法如下: 
  196.     BorderLayout()  :控件之间没有间距的布局 
  197.     BorderLayout(int hgap,int vgap) :  水平间距  垂直间距  
  198.     4. GridLayout 
  199.     5null  
  200.     6. BoxLayout  box 
  201.      
  202.  
  203.      
  204.  
  205.  
  206.  
  207.  
  208.     事件处理 
  209.     在java.util包中定义的EventObject是事件类层次的根节点,它是所有事件类的父类 
  210.     在java.awt包中定义的AWTEvent是其子类  是所有awt事件的父类 
  211.      
  212.     事件监听器 
  213.     可以直接给事件添加监听器 
  214.     addButton.addActionListener(new ActionListener(){ 
  215.         public void actionPerformed(actionEvent e){} 
  216.     }); 
  217.      
  218.     也可以令当前类实现监听器接口,然后覆盖监听器方法: 
  219.     public class JTable extends JFrame implements ActionListener{ 
  220.         public void actionPerformed(actionEvent e){} 
  221.     }        在这种情况下给控件添加监听器就比较简单了,  addButton.addActionListener(this
  222.      
  223.     当同一个监听器同时监听多个控件时,在监听器的方法中需要对事件源进行判断例如: 
  224.     if(e.getActionCommand().equals(ADD_COMMAND)){}   //通过动作命令 判断事件源 
  225.     if(e.getSource()==addButton){}    // 直接判断事件源  
  226.     如果通过 动作命令 来判断事件源,则需要给控件设置动作命令标示 
  227.     private static String ADD_COMMAND="add" 
  228.     addButton.setActionCommand(ADD_COMMAND); 
  229.      
  230.     常见的事件处理 
  231.     1. 动作事件 
  232.     ActionListener 接口定义了动作事件监听器,可以实现该接口并覆盖actionPerformed() 方法  或者直接添加事件处理器 
  233.     2. 文本事件 
  234.     TextListener表示文本监听器 
  235.     public class Text extends JFrame implements TextListener{ 
  236.         public void textValueChanged(TextEvent e){} 
  237.     } 
  238.     3. 选项事件 
  239.     ItemListener 可以用于复选框,列表,下拉组合框,单选控件 
  240.     comboBox.addItemListener(new ItemListener(){ 
  241.         public void itemStateChanged(ItemEvent e){} 
  242.     }) 
  243.     4. 键盘事件 
  244.     KeyListener接口定义了三个方法: 
  245.     void KeyPressed(KeyEvent e){} 
  246.     void KeyReleased(KeyEvent e){} 
  247.     void KeyTyped(KeyEvent e){} 键入某个键时调用 
  248.     5. 鼠标事件 
  249.     MouseListener接口  
  250.     方法有: 
  251.     void mouseClicked(MouseEvent e) 
  252.          mouseEnter(MouseEvent e)  //鼠标到达控件之上 
  253.          mouseExit(MouseEvent e)  //鼠标离开控件 
  254.          mousePressed(MouseEvent e) //在控件上按下鼠标键 
  255.          mouseReleased(MouseEvent e) //在控件上按下鼠标键时 
  256.           
  257.     MouseMotionListener 接口 
  258.     方法有: 
  259.     void mouseDragged(MouseEvent e)  //在控件上按下并拖动鼠标时 
  260.          mouseMoved(MouseEvent e)  //鼠标光标移动到控件上时 
  261.     MouseWheelListener接口 
  262.     方法有: 
  263.     void mouseWheelMoved(MouseWheelEvent e)  //鼠标滚轮滑动 
  264.     6. 焦点事件 
  265.     FocusListener接口用于监听控件键盘焦点事件,其方法有: 
  266.     void focusGained(FocusEvent e)  //获得键盘焦点 
  267.     void focusLost(FocusEvent e)  //失去键盘焦点 
  268.     7. 窗口事件 
  269.     windowFocusListener 接口 
  270.     其方法有: 
  271.     void windowGainedFocus(WindowEvent e) // 窗口获得焦点 
  272.          windowLostFocus(WindowEvent e)   //失去焦点 
  273.     WindowListener 接口 
  274.     其方法有: 
  275.     void windowActived(WindowEvent e)    //激活窗口时候 
  276.          windowClosed(WindowEvent e)    //关闭窗口时 
  277.          windowClosing(WindowEvent e)   //用户从窗口的菜单中关闭窗口时 
  278.          windowDeactivated(WindowEvent e)   //不再是活动窗口时候 
  279.          windowDeiconified(WindowEvent e)   //窗口从最小化到正常状态时 
  280.          windowIconified(WindowEvent e)    //从正常变为最小化时候 
  281.          windowOpened(WindowEvent e)   //首次可视时候 
  282.     WindowStateListener接口 
  283.     方法有: 
  284.     void windowStateChanged(WindowEvent e)   //窗口的状态改变时 
  285.     8. 表格模型事件 
  286.     其是由TableModelListener接口定义的,其只有一个方法 tableChanged() 
  287.     tableMode1.addTableModelListener(new TableModelListener(){ 
  288.         public void tableChanged(TableModelEnent e){ 
  289.             e.getColumn();  //事件发生的列 
  290.             e.getFirstRow();//触发事件表格的最小行 
  291.             e.getLastRow(); //触发事件表格的最大行 
  292.             e.getSource();  //事件源 
  293.             e.getType();   //事件类型  添加,删除,修改 
  294.         } 
  295.     }) 
  296.   
  297.  
  298.  
  299.  
  300.  
  301.     事件适配器 
  302.     要实现监听接口就必须是想其声明的所有方法,我们可以使用一组带Adapter标记的适配器类,来不用实现所有方法 
  303.     常用的适配器类有: 
  304.     windowAdapter    对应的接口 windowListener 
  305.     FocusAdapter     对应的接口 FocusListener 
  306.     KeyAdapter        对应的接口 KeyListener 
  307.     MouseAdapter      对应的接口 KeyListener 
  308.     MouseMotionAdapter  对应的接口 MouseMotionListener 
  309.  
  310.     Swing 窗口 
  311.     1. JFrame窗口 
  312.     其构造方法如下: 
  313.     JFrame() 
  314.     JFrame(String title) 
  315.     其常用方法如下: 
  316.     void setDefaultCloseOperation(int operation)  设置关闭按钮的操作 
  317.          setIconImage(Image image)   设置窗口图标 
  318.          setContentPane(Container contentPane)  设置内容面板 
  319.          setJMenuBar(JMenuBar menubar)   设置菜单栏 
  320.          repaint(long time , int x,int y,int width,int height)  在time毫秒内重绘指定的区域 
  321.          setLayout(Layout)  设置布局管理器 
  322.          remove(Component comp)  移除指定控件 
  323.          static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)  新建的窗口是否具有外观装饰 
  324.     例如:         import javax.swing.*; 
  325.                 import java.awt.*; 
  326.                 public class jjtt extends JFrame{ 
  327.                     public jjtt(String title){ 
  328.                         super(title); 
  329.                         setSize(315,190); 
  330.                         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  331.                         Panel panel=new Panel(); 
  332.                         panel.add(new Button("a")); 
  333.                         setContentPane(panel); 
  334.                     } 
  335.                     public static void main(String args[]){ 
  336.                         JFrame.setDefaultLookAndFeelDecorated(true); 
  337.                         new jjtt("jjtt").setVisible(true); 
  338.                     } 
  339.                 } 
  340.         setDefaultCloseOperation(int xx)   其参数可以取值: 
  341.         WindowConstants.DO_NOTHING_ON_CLOSE 
  342.         windowConstants.HIDE_ON_CLOSE 
  343.         windowConstants.DISPOSE_ON_CLOSE 
  344.         windowConstants.EXIT_ON_CLOSE  
  345.          
  346.  
  347.     2. JOptionPane 标准对话框 
  348.     此用于创建向用户发出信息的对话框,最常见的对话框有四种: 
  349.     showConfirmDialog    showInputDialog  showMessageDialog   showOptionDialog 
  350.         1. showConfirmDialog 
  351.         static int showConfirmDialog(Component parentCom,Object message,String title,int optionType,int messageType,Icon icon) 
  352.         message要显示的内容     title标题  optionTYpe  对话框按钮的类型  messageType  为图标的类型  icon为自定义图标 
  353.          
  354.         optionType可以为: 
  355.         YES_NO_OPTION :是否按钮 
  356.         YES_NO_CANCEL_OPTION :是否取消按钮 
  357.         OK_CANCEL_OPTION :确定,取消按钮 
  358.          
  359.         MessageType可以为: 
  360.         ERROR_MESSAGE :出错图标 
  361.         INFORMATION_MESSAGE:信息图标 
  362.         WARING_MESSAGE :警告图标 
  363.         QUESTION_MESSAGE: 询问图标 
  364.         PLAIN_MESSAGE  :无图标 
  365.          
  366.         该各个返回值可以是: 
  367.         YES_OPTION   NO_OPTION  CANCEL_OPTION  OK_OPTION   CLOSED_OPTION 
  368.         例如: 
  369.         import javax.swing.*; 
  370.         public class jjtt extends JFrame{ 
  371.             public jjtt(){ 
  372.                 setSize(345,260); 
  373.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  374.             } 
  375.             public static void main(String args[]){ 
  376.                 jjtt j=new jjtt(); 
  377.                 j.setVisible(true); 
  378.                 int select=JOptionPane.showConfirmDialog(j,"go on ?","option",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); 
  379.                 System.out.println(select==JOptionPane.YES_OPTION?"GO ON":"STOP"); 
  380.                 int s=JOptionPane.showConfirmDialog(j,"go on ?","option",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,new ImageIcon("1.jpg")); 
  381.             } 
  382.         } 
  383.  
  384.             2. showMessageDialog 
  385.             static void showMessageDialog(Component parentcom,Object message,String titlt,int messageType,Icon icon) 
  386.             示例:  JOptionPane.showMessgaeDialog(this,"done","option",JOptionPane.INFORMATION_MESSAGE); 
  387.             3. showInputDialog 
  388.             static String showInputDialog(Component parentcomp,Object message,String title,int messageType) 
  389.             示例:  String postcode=JOptionPane.showInputDialog(this,"input code","option",JOptionPane.WARING_MESSAGE); 
  390.             4. showOptionDialog 
  391.             该类型对话框可以自定义操作按钮: 
  392.             static int showOptionDialog(Component parentcom,Object Message,String title,int optionType,int messageType,Icon icon,Object[] option,Object initialValue); 
  393.             这里的options表示用户可能的选择项数组,initialValue表示默认值只有在使用options时候才有意义,否则为null 
  394.              
  395.  
  396.  
  397.     3. JColorChooser 颜色选择器 
  398.     其提供了一个重要的静态方法  showDialog() ,   用于弹出颜色选择对话框 
  399.     static Color showDialog(Component parentcom,String title,Color initialColor); 
  400.     Color color=JColorChooser.showDialog(this,"choose color",Color.RED); 
  401.     示例: Label lable=new Label("aaa"); 
  402.         j.add(lable); 
  403.         Color color=JColorChooser.showDialog(j,"choose",j.getBackground()); 
  404.         lable.setForeground(color); 
  405.  
  406.     4. 文件选择器JFileChooser 
  407.     其常用的构造方法如下: 
  408.     JFileChooser()  构造一个指向用户目录的JFileChooser 
  409.     JFileChooser(File currentDirectory)  根据指定的路径构造JFileChooser 
  410.     JFileChooser(String currentDirectoryPath)  根据指定的路径构造JFileChooser 
  411.     例如: 
  412.     JFileChooser jfc1=new JFileChooser(); 
  413.     jfc1.showOpenDialog(this);     在window平台下默认打开 我的文档 文件夹 
  414.     JFileChooser jfc2=new JFileChooser(new File("."));    
  415.     jfc2.showOpenDialog(this);       默认打开读取工作目录 
  416.  
  417.     有时可能只希望选择文件,或者只选择文件夹,JFileChooser通过三个静态属性: 
  418.     static int DIRECTORIES_ONLY    仅显示目录 
  419.     static int FILES_AND_DIRECTORIES   显示目录和文件 
  420.     static int FILES_ONLY   仅显示文件 
  421.     JFileChooser 同时提供了setFileSelectMode() 方法来设置模式 
  422.      
  423.     如果只希望选择word ...文件,也就是文件过滤,我们需要利用javax.swing.filechooser包中的文件扩展名过滤器 FileNameExtensionFilter 
  424.     FileNameExtensionFilter(String description,String ...extensions)  description是对文件扩展名的描述,extensions表示具体的扩展名 
  425.     示例:  FileNameExtensionFilter filter=new FileNameExtensionFilter("office","doc","xml","ppt"); 
  426.      
  427.     现在我们需要打开该对话框,JFileChooser提供了静态方法  showDialog()   showOpenDialog() 
  428.     int showDialog(Component parentcom,String approveButtonText)  弹出具有自定义打开按钮的自定义文件选择对话框 
  429.     int showOpenDialog(Component parentcom)  在父容器中打开文件选择器对话框 
  430.     示例:  jfc1.showDialog(this,"choose file")           jfc2.showOpenDialog(this
  431.     区别在于前者使用"choose file" 作为打开按钮和对话框标题文字,而后者使用默认的打开字样 
  432.      
  433.     最后,我们怎么判断用户是选择了打开还是 取消, 可以通过showDialog() showOpenDialog() 的返回值来判定 
  434.     可能的返回值如下: 
  435.     JFileChooser.CANCEL_OPRION 
  436.     JFileChooser.APPROVE_OPTION 
  437.     JFileChooser.ERROR_OPTION  
  438.     示例: 
  439.     JFileChooser chooser=new JFileChooser(); 
  440.         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
  441.         FileNameExtensionFilter filter=new FileNameExtensionFilter("office","doc","xls","ppt"); 
  442.         chooser.setFileFilter(filter); 
  443.         int se=chooser.showOpenDialog(j); 
  444.         if(se==JFileChooser.APPROVE_OPTION){ 
  445.             try
  446.                 Runtime.getRuntime().exec(new String[] { 
  447.                     "cmd.exe","/C",chooser.getSelectedFile().toString() 
  448.                 }); 
  449.             }catch(IOException e){ 
  450.             e.printStackTrace(); 
  451.             } 
  452.         } 
  453.  
  454.  
  455.  
  456.  
  457.  
  458.      
  459.      
  460.      
  461.      
  462.     Swing 容器 
  463.     1. JPanel面板 
  464.     通过JPanel对控件进行包装,是一种通用的小型容器,其构造方法: 
  465.     JPanel()  创建具有双缓冲和FlowLayoutb布局的JPanel 
  466.     JPanel(boolean isDoubleBuffered)  是否使用双缓冲 
  467.     JPanel(Layout)   具有指定Layout 
  468.     JPanel(Layout,boolean isDoubleBuffered)      
  469.      
  470.     2. JTabbedPanel 标签化面板 
  471.     此面板为用户提供了一组控件之间进行切换的能力,其构造方法如下: 
  472.     JTabbedPanel()  创建一个具有默认 JTabbedPanel.TOP 选项卡布局的标签化面板 
  473.     JTabbedPanel(int tabPlacement) 使用指定的布局tabPlacement创建  tabPlacement可以是JTabbedPanel.TOP JTabbedPanel.BOTTOM  JTabbedPanel.LEFT JTabbedPanel.ROGHT 
  474.     JTabbedPanel(int tabPlacement,int tabLayputPolicy)  使用指定的布局  指定的布局策略  布局策略可以是JTabbedPane.WRAP_TAB_LAYOUT 自动折行 
  475.     JTabbedPane.SCROLL_TAB_LAYOUT 滚动选项卡 
  476.     JTabbedPane提供了addTab()方法用于添加选项卡标签,该方法格式如下: 
  477.     void addTab(String title,Component com)   添加一个标题为title且没有图标的选项卡 com为单击此选项卡时要显示的控件 
  478.     void addTab(String title,Icon icon,Component com)      有图标 
  479.     void addTab(String title,Icon icon,Component com,String tip)  tip为鼠标指向选项卡时要显示的提示文字 
  480.     此过程为: 
  481.     创建JTabbedPane对象,创建包含各种组件的容器Container,添加选项卡标签 container作为addTab()的参数,将tabbedPane加到窗口 
  482.      
  483.     3. JSplitPane 拆分面板 
  484.     用于将两个控件进行图形化分割,而且这两个控件可以有用户交互式调整大小,其构造函数如下: 
  485.     JSplitPane()  将控件水平排列 
  486.     JSplitPane(int newOrientation) 创建一个指定方向且无连续布局的JSplitPane,方向可以是 JSplitPane.HORIZONTAL_SPLIT 水平 JSplitPane.VERTICAL_SPLIT垂直 
  487.     JSplitPane(int newOrientation,boolean newContinuousLayout)  具有指定重绘方式,为true时表示当分割条改变位置时 连续重绘控件,否则只有当移动停止时才重绘控件 
  488.     JSplitPane(int newOrientation,boolean newContinuousLayout,Component newLeftComponent,Component newRightComponent)  第一个控件出现在左边或上边,第二个出现在下面或右边 
  489.     JSplitPane(int newOrientation,Component newLeftComponent,Component newRightComponent)  不连续重绘 
  490.     示例: 
  491.     JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,new Button("a"),new Button("b")); 
  492.      
  493.     4. JScrollPane 滚动面板 
  494.     此面板是根据滚动条策略显示水平或垂直滚动条,其构造方法如下: 
  495.     JScrollPane()  创建一个根据面板内容自动显示水平和垂直滚动条的面板 
  496.     JScrollPane( Component view ) 创建一个显示指定控件view  自动显示滚动条 
  497.     JScrollPane(Component view ,int vsbPolicy,int hsbpolicy)  具有指定滚动策略  vsbpolicy 可以是ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED ,  当需要时垂直 
  498.     ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER 从不显示垂直滚动条   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS 
  499.     ScrollPaneConstants.HPRIZONTAL_SCROLLBAR_AS_NEEDED  ScrollPaneConstants.HPRIZONTAL_SCROLLBAR_NEVER  ScrollPaneConstants.HPRIZONTAL_SCROLLBAR_ALWAYS 
  500.     示例: 
  501.     JScrollPane srcollPane=new JScrollPane(new Button("aa"),ScrollPaneConstants.HPRIZONTAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
  502.      
  503.      
  504.     工具栏JToolBar 
  505.     其提供了快捷方式,其创建方法如下: 
  506.     JToolBar()  默认水平的工具栏 
  507.     JToolBar(int orientation)  具有指定方向  JToolBar.HORIZONTAL  JToolBar.VERTICAL 
  508.     JToolBar(String name)  具有指定名称的工具栏 
  509.     JToolBar(String name,int orientation) 
  510.     示例: 
  511.     JToolBar toolBar=new JToolBar("toolbar"); 
  512.     toolbar.setFloatable(false);         //不允许拖动工具栏 
  513.     Button o=new Button("open"); 
  514.     Button c=new Button("close"); 
  515.     Button s=new Button("save"); 
  516.     Button e=new Button("exit"); 
  517.     toolBar.add(o); 
  518.     toolBar.add(c); 
  519.     toolBar.add(s); 
  520.     toolBar.add(new JToolBar.Separator()); //按钮之间添加分隔符 
  521.     toolBar.add(e); 
  522.     toolBar.setLayout(new FlowLayout(FlowLayput.CENTER,1,1)); 
  523.     add(toolBar); 
  524.      
  525.      
  526.     5. 桌面面板和内部框架 
  527.     JDesktopPane用于创建多文档界面的容器,可以在其内部包含多个子窗口。其只有一个构造方法:  JDesktopPane() 
  528.     对于子窗口,JDesktopPane提供了setDragMode(int dragMode) 方法设置拖拉方式,参数可以是 JDesktopPane.LIVE_DRAG_MODE 拖动子窗口时候显示器内容 
  529.     JDesktopPane.OUTLINE_DRAG_MODE  不显示 
  530.     我们可以先创建JDesktopPane然后向其内部加入子窗口 JInternalFrame,其构造方法如下: 
  531.     JInternalFrame()  子窗口不可调整大小不可最大最小化,不可关闭 
  532.     JInternalFrame(String title)  具有标题 
  533.     JInternalFrame(String title,boolean resizable)  是否可调大小  
  534.     JInternalFrame(String title,boolean resizable,boolean closable)   是否可关闭 
  535.     JInternalFrame(String title,boolean resizable,boolean closable,boolean maximizable)  是否可最大化 
  536.     JInternalFrame(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable) 是否可图标化 
  537.     示例: 
  538.     import javax.swing.*; 
  539.     import java.awt.*; 
  540.     public class jjtt extends JFrame { 
  541.         public  jjtt(String title){ 
  542.             super(title); 
  543.             setSize(900,1236); 
  544.             setResizable(false); 
  545.             JDesktopPane desktop=new CreateDeaktopPane(); 
  546.             createFrame(desktop,3);        //创建三个子窗口 
  547.             setContentPane(desktop); 
  548.         } 
  549.         protected void createFrame(JDesktopPane desktop,int num){ 
  550.             JInternalFrame frame=null
  551.             for(int i=0;i<num;i++){ 
  552.                 frame=new JInternalFrame("窗口"+i,true,true,true,true); 
  553.                 frame.setSize(179,100); 
  554.                 frame.setLocation(i*20,i*20); 
  555.                 frame.setVisible(true); 
  556.                 desktop.add(frame); 
  557.             } 
  558.         } 
  559.         public static void main(String args[]){ 
  560.             //JFrame.setDefaultLookAndDecorated(false); 
  561.             new jjtt("multi").setVisible(true); 
  562.         } 
  563.         class CreateDeaktopPane extends JDesktopPane{ 
  564.             public CreateDeaktopPane(){ 
  565.                 setDragMode(JDesktopPane.LIVE_DRAG_MODE); 
  566.             } 
  567.             protected void paintComponent(Graphics g){ 
  568.                 g.drawImage(Toolkit.getDefaultToolkit().getImage("1.jpg"),0,0,this); 
  569.             } 
  570.         } 
  571.     } 
  572.      
  573.      
  574.      
  575.      
  576.      
  577.      

 

你可能感兴趣的:(界面)