一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java卡片布局管理器解释及实例

时间:2017-09-06 编辑:猪哥 来源:一聚教程网

由 CardLayout 类实现的布局管理器称为卡片布局管理器,用来操纵其所管理容器中包含的容器或组件。每个直接添加到其所管理容器中的容器或组件为一个卡片,最先被添加的容器或组件被认为是第一个卡片,最后被添加的则为最后一个卡片,初次运行时将显示第一个卡片。下面,我们就来通过一个实例看看它是怎么用的。

源码:

importjava.awt.BorderLayout;
importjava.awt.CardLayout;
importjava.awt.Color;
importjava.awt.Insets;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.UIManager;
  
 
publicclassTestMainextendsJFrame {
  privateJPanel pane =null;// 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
  privateJPanel p =null;// 放按钮的JPanel
  privateCardLayout card =null;// CardLayout布局管理器
  privateJButton button_1 =null;// 上一步
  privateJButton button_2 =null;// 下一步
  privateJButton b_1 =null, b_2 =null, b_3 =null;// 三个可直接翻转到JPanel组件的按钮
  privateJPanel p_1 =null, p_2 =null, p_3 =null;// 要切换的三个JPanel
   
  publicTestMain() {
    super("CardLayout Test");
    try{
      // 将LookAndFeel设置成Windows样式
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }catch(Exception ex) {
      ex.printStackTrace();
    }
    /**创建一个具有指定的水平和垂直间隙的新卡片布局*/
    card =newCardLayout(5,5);
    pane =newJPanel(card);// JPanel的布局管理将被设置成CardLayout
    p =newJPanel();// 构造放按钮的JPanel
    button_1 =newJButton("< 上一步");
    button_2 =newJButton("下一步 >");
    b_1 =newJButton("1");
    b_2 =newJButton("2");
    b_3 =newJButton("3");
    b_1.setMargin(newInsets(2,2,2,2));
    b_2.setMargin(newInsets(2,2,2,2));
    b_3.setMargin(newInsets(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 =newJPanel();
    p_2 =newJPanel();
    p_3 =newJPanel();
    p_1.setBackground(Color.RED);
    p_2.setBackground(Color.BLUE);
    p_3.setBackground(Color.GREEN);
    p_1.add(newJLabel("JPanel_1"));
    p_2.add(newJLabel("JPanel_2"));
    p_3.add(newJLabel("JPanel_3"));
    pane.add(p_1,"p1");
    pane.add(p_2,"p2");
    pane.add(p_3,"p3");
    /**下面是翻转到卡片布局的某个组件,可参考API中的文档*/
    button_1.addActionListener(newActionListener(){// 上一步的按钮动作
      publicvoidactionPerformed(ActionEvent e) {
        card.previous(pane);
      }
    });
    button_2.addActionListener(newActionListener(){// 下一步的按钮动作
      publicvoidactionPerformed(ActionEvent e) {
        card.next(pane);
      }
    });
    b_1.addActionListener(newActionListener() {// 直接翻转到p_1
      publicvoidactionPerformed(ActionEvent e) {
        card.show(pane,"p1");
      }
    });
    b_2.addActionListener(newActionListener() {// 直接翻转到p_2
      publicvoidactionPerformed(ActionEvent e) {
        card.show(pane,"p2");
      }
    });
    b_3.addActionListener(newActionListener() {// 直接翻转到p_3
      publicvoidactionPerformed(ActionEvent e) {
        card.show(pane,"p3");
      }
    });
    this.getContentPane().add(pane);
    this.getContentPane().add(p, BorderLayout.SOUTH);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300,200);
    this.setVisible(true);
  }
   
  publicstaticvoidmain(String[] args) {
    newTestMain();
  }
   
}

注意:当利用方法 next() 或 previous() 显示卡片时,将循环显示所有卡片。例如,如果当前显示的是最后一个卡片,再调用方法 next() 将显示第一个卡片;如果当前显示的是第一个卡片,再调用方法 previous() 将显示最后一个卡片。

热门栏目