⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right5_1_4_1.htm

📁 清华JAVA教程。不用多说了吧
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"><script language="JavaScript"><!--function MM_openBrWindow(theURL,winName,features) { //v2.0  window.open(theURL,winName,features);}//--></script></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="90%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top">             <p> <span class="pt9-black">  布局管理器主要包括:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout<br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例5.3<a name="04"></a></font><br>                  import java.awt.*;<br>                  public class ExGui{<br>                      private Frame f;<br>                      private Button b1;<br>                      private Button b2;<br>                      public static void main(String args[]){<br>                          ExGui that = new ExGui();<br>                          that.go();<br>                  }</span></p>            <p><span class="pt9-black">         public void go(){<br>                          f = new Frame(&quot;GUI example&quot;);<br>                          f.setLayout(new FlowLayout()); <font color="339900"><br>                          //设置布局管理器为FlowLayout</font><br>                          b1 = new Button(&quot;Press Me&quot;); <font color="339900"><br>                          //按钮上显示字符&quot;Press Me&quot;</font><br>                          b2 = new Button(&quot;Don't Press Me&quot;);<br>                          f.add(b1);<br>                          f.add(b2);<br>                          f.pack(); <font color="339900"><br>                          //紧凑排列,其作用相当于setSize(),即让窗口<br>                          尽量小,小到刚刚能够包容住b1、b2两个按钮</font><br>                          f.setVisible(true);<br>                      }<br>                  }<br>              <br>                  <a href="#04" ><font color="#FF0000" onClick="MM_openBrWindow('tanchu3.htm','','width=177,height=57')">查看运行结果</font></a><br>              <br>               </span><span class="zhongdian">1. FlowLayout<a name="01"></a></span><span class="pt9-black"><br>              <br>                FlowLayout 是Panel,Applet的缺省布局管理器。其组件的放置规律是从上到下、从左到右进行放置,如果容器足够宽,第一个组件先添加到容器中第一行的最左边,后续的组件依次添加到上一个组件的右边,如果当前行已放置不下该组件,则放置到下一行的最左边。<br>              <br>                构造方法主要下面几种:<br>                FlowLayout(FlowLayout.RIGHT,20,40);<br>                <font color="339900">/*第一个参数表示组件的对齐方式,指组件在这一行中的位置是居中对齐、居右对齐还是居左对齐,第二个参数是组件之间的横向间隔,第三个参数是组件之间的纵向间隔,单位是象素。*/</font><br>                FlowLayout(FlowLayout.LEFT); <font color="339900"><br>                //居左对齐,横向间隔和纵向间隔都是缺省值5个象素</font><br>                FlowLayout(); <font color="339900"><br>                //缺省的对齐方式居中对齐,横向间隔和纵向间隔都是缺省值5个象素<br>              <br>              </font><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例5.4<a name="05"></a></font><br>                  import java.awt.*;<br>                  public class myButtons{ <br>                   public static void main(String args[])<br>                   {<br>                      Frame f = new Frame(); <br>                      f.setLayout(new FlowLayout());<br>                      Button button1 = new Button(&quot;Ok&quot;);<br>                      Button button2 = new Button(&quot;Open&quot;);<br>                      Button button3 = new Button(&quot;Close&quot;);<br>                      f.add(button1);<br>                      f.add(button2);<br>                      f.add(button3);<br>                      f.setSize(300,100); <br>                      f.setVisible(true);<br>                   }<br>                  }<br>              <br>                  <a href="#05" onClick="MM_openBrWindow('tanchu4.htm','','width=199,height=66')"><font color="#FF0000">查看运行结果</font></a><br>              <br>                <font color="000099">当容器的大小发生变化时,用FlowLayout管理的组件会发生变化,其变化规律是:组件的大小不变,但是相对位置会发生变化。例如上图中有三个按钮都处于同一行,但是如果把该窗口变窄,窄到刚好能够放下一个按钮,则第二个按钮将折到第二行,第三个按钮将折到第三行。按钮&quot;Open&quot;本来在按钮&quot;OK&quot;的右边,但是现在跑到了下面,所以说&quot;组件的大小不变,但是相对位置会发生变化&quot;。<br>              <br>               </font></span><font color="000099"><span class="zhongdian">2. BorderLayout<a name="02"></a></span></font><span class="pt9-black"><font color="000099"><br>              <br>              </font>  BorderLayout 是Window,Frame和Dialog的缺省布局管理器。BorderLayout布局管理器把容器分成5个区域:North,South,East,West和Center,每个区域只能放置一个组件。各个区域的位置及大小如下图所示:<font color="000099"><br>              <br>                  <img src="../../../images/tu/ch05/5_1_4_1.gif" width="240" height="210"><br>              <br>              </font><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例5.5<a name="06"></a></font><br>                  import java.awt.*;<br>                  public class buttonDir{<br>                   public static void main(String args[]){<br>                    Frame f = new Frame(&quot;BorderLayout&quot;);<br>                    f.setLayout(new BorderLayout());<br>                    f.add(&quot;North&quot;, new Button(&quot;North&quot;));<font color="339900"><br>                    //第一个参数表示把按钮添加到容器的North区域</font><br>                    f.add(&quot;South&quot;, new Button(&quot;South&quot;));<font color="339900"><br>                    //第一个参数表示把按钮添加到容器的South区域</font><br>                    f.add(&quot;East&quot;, new Button(&quot;East&quot;)); <font color="339900"><br>                    //第一个参数表示把按钮添加到容器的East区域</font><br>                    f.add(&quot;West&quot;, new Button(&quot;West&quot;));<br>                    <font color="339900">//第一个参数表示把按钮添加到容器的West区域</font><br>                    f.add(&quot;Center&quot;, new Button(&quot;Center&quot;));<br>                    <font color="339900">//第一个参数表示把按钮添加到容器的Center区域</font><br>                    f.setSize(200,200);<br>                    f.setVisible(true); <br>                   }<br>                  }<font color="000099"><br>              <br>                  </font><a href="#06" onClick="MM_openBrWindow('tanchu5.htm','','width=225,height=186')"><font color="#FF0000">查看运行结果</font></a><br>              <br>                <font color="000099">在使用BorderLayout的时候,如果容器的大小发生变化,其变化规律为:组件的相对位置不变,大小发生变化。例如容器变高了,则North、South区域不变,West、Center、East区域变高;如果容器变宽了,West、East区域不变,North、Center、South区域变宽。不一定所有的区域都有组件,如果四周的区域(West、East、North、South区域)没有组件,则由Center区域去补充,但是如果Center区域没有组件,则保持空白,其效果如下几幅图所示:<br>              <br>                <img src="../../../images/tu/ch05/5_1_4_2.gif" width="220" height="220"><br>                     North区域缺少组件         <br>              <br>                <img src="../../../images/tu/ch05/5_1_4_3.gif" width="219" height="219"><br>                    North和Center区域缺少组件<br>              <br>               </font></span><font color="000099"></font><span class="zhongdian">3.               GridLayout<a name="03"></a></span><span class="pt9-black"><br>              <br>                使容器中各个组件呈网格状布局,平均占据容器的空间。<br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例5.6<a name="07"></a></font><br>                  import java.awt.*;<br>                  public class ButtonGrid {<br>                  public static void main(String args[]) {<br>                    Frame f = new Frame(&quot;GridLayout&quot;);<br>                    f.setLayout(new GridLayout(3,2)); <br>                               <font color="339900">//容器平均分成3行2列共6格</font><br>                    f.add(new Button(&quot;1&quot;)); <font color="339900">//添加到第一行的第一格</font><br>                    f.add(new Button(&quot;2&quot;)); <font color="339900">//添加到第一行的下一格</font><br>                    f.add(new Button(&quot;3&quot;)); <font color="339900">//添加到第二行的第一格</font><br>                    f.add(new Button(&quot;4&quot;)); <font color="339900">//添加到第二行的下一格</font><br>                    f.add(new Button(&quot;5&quot;)); <font color="339900">//添加到第三行的第一格</font><br>                    f.add(new Button(&quot;6&quot;)); <font color="339900">//添加到第三行的下一格</font><br>                    f.setSize(200,200);<br>                    f.setVisible(true);<br>                  }<br>                  }<font color="000099"><br>              <br>                  </font><a href="#07" onClick="MM_openBrWindow('tanchu6.htm','','width=228,height=228')"><font color="#FF0000">查看运行结果</font></a></span></p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -