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

📄 right5_1_4_2.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="97%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top">             <p> <span class="pt9-black">  </span><span class="zhongdian">4. CardLayout<a name="01"></a></span><span class="pt9-black"><br>              <br>                CardLayout布局管理器能够帮助用户处理两个以至更多的成员共享同一显示空间,它把容器分成许多层,每层的显示空间占据整个容器的大小,但是每层只允许放置一个组件,当然每层都可以利用Panel来实现复杂的用户界面。牌布局管理器(CardLayout)就象一副叠得整整齐齐的扑克牌一样,有54张牌,但是你只能看见最上面的一张牌,每一张牌就相当于牌布局管理器中的每一层。<br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例5.7</font><br>               import java.awt.*;<br>               import java.awt.event.*; <font color="339900">//事件处理机制,下一节的内容</font><br>               public class ThreePages implements MousListener {<br>                  CardLayout layout=new CardLayout(); <font color="339900">//实例化一个牌布局管理器对象</font><br>                  Frame f=new Frame(&quot;CardLayout&quot;);<br>                  Button page1Button;<br>                  Label page2Label; <font color="339900">//Label是标签,实际上是一行字符串</font><br>                  TextArea page3Text; <font color="339900">//多行多列的文本区域</font><br>                  Button page3Top;<br>                  Button page3Bottom;</span></p>            <p><span class="pt9-black"> public static void main(String args[])               <br>               { new ThreePages().go(); }</span><br>              <br>               <span class="pt9-black">Public void go()<br>               {   f.setLayout(layout); <font color="339900">//设置为牌布局管理器layout</font><br>                  f.add(page1Button=new Button(&quot;Button page&quot;),&quot;page1Button&quot;);	              <font color="339900">/*第二个参数&quot;page1Button&quot;表示的是你对这层牌所取的名字*/</font><br>                  page1Button.addMouseListener(this); <font color="339900">//注册监听器</font><br>                  f.add(page2Label=new Label(&quot;Label page&quot;),&quot;page2Label&quot;);<br>                  page2Label.addMouseLisener(this); <font color="339900">//注册监听器</font><br>                  Panel panel=new Panel();<br>                  panel.setLayout(new BorderLayout());<br>                  panel.add(page3Text=new TextArea(&quot;Composite page&quot;),&quot;Center&quot;);<br>                  page3Text.addMouseListener(this); <br>                  panel.add(page3Top=new Button(&quot;Top button&quot;) , &quot;North&quot;);<br>                  page3Top.addMouseListener(this);<br>                  panel.add(page3Bottom=new Button(&quot;Bottom button&quot;)               ,&quot;South&quot;);<br>                  page3Bottom.addMouseListener(this);<br>                  f.add(panel,&quot;panel&quot;);<br>                  f.setSize(200,200);<br>                  f.setVisible(true);<br>               }<br>               ……<br>               }<br>              <br>               </span><span class="zhongdian">5.容器的嵌套<a name="02"></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.8<a name="04"></a></font><br>                  import java.awt.*;<br>                  public class ExGui3{<br>                  private Frame f;<br>                  private Panel p;<br>                  private Button bw,bc;<br>                  private Button bfile,bhelp;<br>                     public static void main(String args[])<br>                     {<br>                       ExGui3 gui = new ExGui3();<br>                       gui.go(); <br>                     }</span></p>            <p> <span class="pt9-black">    public void go(){<br>                     f = new Frame(&quot;GUI example 3&quot;);<br>                     bw=new Button(&quot;West&quot;);<br>                     bc=new Button(&quot;Work space region&quot;);<br>                     f.add(bw,&quot;West&quot;);<br>                     f.add(bc,&quot;Center&quot;);<br>                     p = new Panel();<br>                     f.add(p,&quot;North&quot;);<br>                     bfile= new Button(&quot;File&quot;);</span><br>              <span class="pt9-black">       bhelp= new Button(&quot;Help&quot;);<br>                     p.add(bfile);<br>                     p.add(bhelp);<br>                     f.pack();<br>                     f.setVisible(true);<br>                  }<br>                  }</span><br>              <br>                  <span class="pt9-black"><a href="#04" onClick="MM_openBrWindow('tanchu8.htm','','width=208,height=104')"><font color="#FF0000">查看运行结果</font></a></span><br>                <br>               <span class="pt9-black"><b class="zhongdian">小 结:<a name="03"></a></b><br>              <br>                 1.Frame是一个顶级窗口。Frame的缺省布局管理器为BorderLayout。<br>              <br>                 2.Panel 无法单独显示,必须添加到某个容器中。 Panel 的缺省布局管理器为FlowLayout。<br>              <br>                 3.当把Panel 作为一个组件添加到某个容器中后,该Panel 仍然可以有自己的布局管理器。因此,可以利用Panel 使得BorderLayout               中某个区域显示多个组件,达到设计复杂用户界面的目的 。<br>              <br>                 4.如果采用无布局管理器 setLayout(null),则必须使用setLocation(),setSize(),setBounds()等方法手工设置组件的大小和位置,此方法会导致平台相关,不鼓励使用。</span></p>            <p>&nbsp;</p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

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