java的CardLayout

來源:互聯網
上載者:User

CardLayout允許我們在更複雜的擁有真正的檔案夾卡片與一條邊相遇的環境裡建立大致相同於“卡片式對話方塊”的布局,我們必須壓下一個卡片使不同的對話方塊帶到前面來。在AWT裡不是這樣的:CardLayout是簡單的空的空格,我們可以自由地把新卡片帶到前面來。(JFC/Swing庫包括卡片式的窗格看起來非常的棒,且可以我們處理所有的細節。)

1. 聯合布局(Combining layouts)
下面的例子聯合了更多的布局類型,在最初只有一個布局管理器被程式片或應用程式操作看起來相當的困難。這是事實,但如果我們建立更多的面板對象,每個面板都能擁有一個布局管理器,並且像被整合到程式片或應用程式中一樣使用程式片或應用程式的布局管理器。這就象下面程式中的一樣給了我們更多的靈活性:
 

//: CardLayout1.java// Demonstrating the CardLayoutimport java.awt.*;import java.applet.Applet;class ButtonPanel extends Panel {  ButtonPanel(String id) {    setLayout(new BorderLayout());    add("Center", new Button(id));  }}public class CardLayout1 extends Applet {  Button    first = new Button("First"),    second = new Button("Second"),    third = new Button("Third");  Panel cards = new Panel();  CardLayout cl = new CardLayout();  public void init() {    setLayout(new BorderLayout());    Panel p = new Panel();    p.setLayout(new FlowLayout());    p.add(first);    p.add(second);    p.add(third);    add("North", p);    cards.setLayout(cl);    cards.add("First card",       new ButtonPanel("The first one"));    cards.add("Second card",       new ButtonPanel("The second one"));    cards.add("Third card",       new ButtonPanel("The third one"));    add("Center", cards);  }  public boolean action(Event evt, Object arg) {    if (evt.target.equals(first)) {      cl.first(cards);    }     else if (evt.target.equals(second)) {      cl.first(cards);      cl.next(cards);    }     else if (evt.target.equals(third)) {      cl.last(cards);    }     else       return super.action(evt, arg);    return true;  }} ///:~

這個例子首先會建立一種新類型的面板:BottonPanel(按鈕面板)。它包括一個單獨的按鈕,安放在BorderLayout的中央,那意味著它將充滿整個的面板。按鈕上的標籤將讓我們知道我們在CardLayout上的那個面板上。
在程式片裡,面板卡片上將存放卡片和布局管理器CL因為CardLayout必須組成類,因為當我們需要處理卡片時我們需要訪問這些控制代碼。
這個程式片變成使用BorderLayout來取代它的預設FlowLayout,建立面板來容納三個按鈕(使用FlowLayout),並且這個面板安置在程式片末尾的“North”。卡片面板增加到程式片的“Center”裡,有效地佔據面板的其餘地方。
當我們增加BottonPanels(或者任何其它我們想要的組件)到卡片面板時,add()方法的第一個自變數不是“North”,“South”等等。相反的是,它是一個描述卡片的字串。如果我們想輕擊那張卡片使用字串,我們就可以使用,雖然這字串不會顯示在卡片的任何地方。使用的方法不是使用action();代之使用first()、next()和last()等方法。請查看我們有關其它方法的檔案。
在Java中,使用的一些卡片式面板結構十分的重要,因為(我們將在後面看到)在程式片編程中使用的彈出式對話方塊是十分令人沮喪的。對於Java 1.0版的程式片而言,CardLayout是唯一有效取得很多不同的“彈出式”的表單。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.