cardlayoutframe.java~11~

来自「java2参考大全上的例子的源码和自己的理解.」· JAVA~11~ 代码 · 共 96 行

JAVA~11~
96
字号
package CardLayoutFrame;//CardLayoutFrame.javaimport java.awt.*;import javax.swing.*;import java.awt.event.*;public class CardLayoutFrame extends JFrame {  JButton btPrevious = new JButton("前一张");  JButton btNext = new JButton("下一张");  /*  A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph.  Flow layouts are typically used to arrange buttons in a panel.  It will arrange buttons left to right until no more buttons fit on the same line.  Each line is centered.  */  JPanel flowPanel = new JPanel(new FlowLayout());  /*  A CardLayout object is a layout manager for a container.  It treats each component in the container as a card.  Only one card is visible at a time, and the container acts as a stack of cards.  The first component added to a CardLayout object is the visible component  when the container is first displayed.  */  JPanel cardPanel = new JPanel(new CardLayout());  int currentIndex = 0;  public CardLayoutFrame() {    this.getContentPane().add(flowPanel,BorderLayout.SOUTH);    this.getContentPane().add(cardPanel,BorderLayout.CENTER);    cardPanel.add(getCard(1),"Card1");    cardPanel.add(getCard(2),"Card2");    flowPanel.add(btPrevious);    flowPanel.add(btNext);    ActionListener listener = new  ActionListener() {      public void actionPerformed(ActionEvent e) {        switchCard();      }    };    btPrevious.addActionListener(listener);    btNext.addActionListener(listener);    this.setSize(300,200);    this.setTitle("GardLayoutDemo");    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    this.show();  }  JPanel getCard(int index){    JPanel panel = new JPanel(new BorderLayout());    /*     A border layout lays out a container, arranging and resizing its components to fit in five regions:     north, south, east, west, and center. Each region may contain no more than one component,     and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER.     When adding a component to a container with a border layout, use one of these five constants,     for example:     Panel p = new Panel();     p.setLayout(new BorderLayout());     p.add(new Button("Okay"), BorderLayout.SOUTH);    */   JLabel label = new JLabel("<HTML><h1 style=color:red>"+    "这是第"+index+"张卡片"+    "</h1></HTML>"    );  //  JLabel label = new JLabel( "这是第"+index+"张卡片");//也可以,只是字体不同    label.setHorizontalAlignment(JLabel.CENTER);    panel.add(label);    return panel;  }  void switchCard(){     //Gets the layout manager for this container.    CardLayout cl = (CardLayout)cardPanel.getLayout();    if (currentIndex==0){      currentIndex++;    /*     public void show(Container\u00A0parent,     String\u00A0name)     Flips to the component that was added to this layout with the specified name,    using addLayoutComponent. If no such component exists, then nothing happens.    */      cl.show(cardPanel,"Card2");    }else{      currentIndex--;      cl.show(cardPanel,"Card1");    }  }  public static void main(String[] args) {    CardLayoutFrame frame = new CardLayoutFrame();  }}

⌨️ 快捷键说明

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