CardLayout 应 用 示 例: 功能描述: 本程序将当前JFrame 的 Center 区域设置为 CardLayout 布局, 然后每张卡片中添 加一个按钮, 在South 区域添加了4 个按钮, 分别 控制第一张、 上一张、 下一张、 最后一张卡片的按钮。
时间: 2024-02-22 18:55:35 浏览: 30
好的,这是一个使用 CardLayout 的示例,以下是代码实现:
```java
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutDemo extends JFrame implements ActionListener {
private JPanel cardPanel;
private JButton btnFirst, btnPrev, btnNext, btnLast;
private int cardCount = 4;
private int currentCard = 0;
public CardLayoutDemo() {
super("CardLayout Demo");
cardPanel = new JPanel();
cardPanel.setLayout(new CardLayout());
for (int i = 1; i <= cardCount; i++) {
String cardName = "Card " + i;
JButton btnCard = new JButton(cardName);
btnCard.setPreferredSize(new Dimension(200, 100));
btnCard.setBackground(Color.WHITE);
cardPanel.add(btnCard, cardName);
}
JPanel controlPanel = new JPanel();
btnFirst = new JButton("First");
btnPrev = new JButton("Prev");
btnNext = new JButton("Next");
btnLast = new JButton("Last");
controlPanel.add(btnFirst);
controlPanel.add(btnPrev);
controlPanel.add(btnNext);
controlPanel.add(btnLast);
btnFirst.addActionListener(this);
btnPrev.addActionListener(this);
btnNext.addActionListener(this);
btnLast.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(cardPanel, BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnFirst) {
currentCard = 0;
} else if (e.getSource() == btnPrev) {
currentCard--;
if (currentCard < 0) {
currentCard = cardCount - 1;
}
} else if (e.getSource() == btnNext) {
currentCard++;
if (currentCard >= cardCount) {
currentCard = 0;
}
} else if (e.getSource() == btnLast) {
currentCard = cardCount - 1;
}
CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
cardLayout.show(cardPanel, "Card " + (currentCard + 1));
}
public static void main(String[] args) {
new CardLayoutDemo();
}
}
```
在这个示例中,我们首先创建了一个 `JPanel`,并将其布局设置为 `CardLayout`。然后,我们创建了四个按钮,将它们添加到面板上,并使用不同的字符串命名它们的卡片。我们还创建了一个控制面板,其中包含了四个按钮,用于控制卡片的切换。最后,我们将这两个面板添加到主窗口中,并设置了一些事件监听器来处理按钮点击事件。
在事件处理程序中,我们根据按钮的来源来确定要切换到哪个卡片。如果用户单击了“下一张”按钮,我们增加 `currentCard` 的值,然后检查是否超出了卡片的数量。如果超出了,我们将 `currentCard` 设置为零,以循环到第一张卡片。类似地,如果用户单击“上一张”按钮,我们将 `currentCard` 的值减少,并检查是否小于零。如果是,我们将 `currentCard` 设置为卡片数量减一,以循环到最后一张卡片。最后,我们使用 `CardLayout` 的 `show()` 方法来显示当前卡片。
希望这个示例能够帮助你理解如何使用 CardLayout 布局。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)