Java學習筆記之卡片式布局CardLayout__Java

來源:互聯網
上載者:User

CardLayout(構造)方法:

方法 說明
public CardLayout() 建立一個間距大小為0的新卡片布局
public CardLayout(int hgap,int vgap) 建立一個具有指定水平間距和垂直間距的新卡片布局。水平間距置於左右邊緣。垂直間距置於上下邊緣
public void first(Container parent) 翻轉到容器的第一張卡片
public void last(Contain parent) 翻轉到容器的最後一張卡片
public void next(Contain parent) 翻轉到指定容器的下一張卡片

public void previous(Contain

parent)

翻轉到指定容器的前一張卡片
public void show(Contain parent,String name) 翻轉到使用addLayoutComponent添加到此布局的具有指定name的組件

樣本:

package com.lc.awt;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class CardLayoutDemo extends JFrame {private JPanel pane = null;private JPanel p = null;private CardLayout card = null;private JButton button_1=null;private JButton button_2=null;private JButton b_1=null,b_2=null,b_3=null;private JPanel p_1=null,p_2=null,p_3=null; public CardLayoutDemo(){card = new CardLayout(5,5);pane = new JPanel(card);p = new JPanel();button_1 = new JButton("< 上一步");button_2 = new JButton("下一步   >");b_1 = new JButton("1");b_2 = new JButton("2");b_3 = new JButton("3");b_1.setMargin(new Insets(2,2,2,2));b_2.setMargin(new Insets(2,2,2,2));b_3.setMargin(new Insets(2,2,2,2));p.add(button_1);p.add(b_1);p.add(b_2);p.add(b_3);p.add(button_2);p_1 = new JPanel();p_2 = new JPanel();p_3 = new JPanel();p_1.setBackground(Color.RED);p_2.setBackground(Color.BLUE);p_3.setBackground(Color.GREEN);p_1.add(new JLabel("JPanel_1"));p_2.add(new JLabel("JPanel_2"));p_3.add(new JLabel("JPanel_3"));pane.add(p_1,"p1");pane.add(p_2,"p2");pane.add(p_3,"p3");button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {card.previous(pane);}});button_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {card.next(pane);}});b_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {card.show(pane,"p1");}});b_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {card.show(pane,"p2");}});b_3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {card.show(pane,"p3");}});this.getContentPane().add(pane);this.getContentPane().add(p,BorderLayout.SOUTH);this.setSize(300, 200);this.setVisible(true);}public static void main(String[] args) {new CardLayoutDemo();}}

運行結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.