在JFrame 窗体下部添加5个按钮,分别显示为“上一张”“下一张”“1”“2”“3”,控制窗体上部显示的红色、蓝色、绿色卡片,使用 Cardlayout 布局管理器实现这些效果
时间: 2023-10-11 07:11:37 浏览: 51
好的,我可以为您解答这个问题。
首先,您需要创建一个 JFrame 窗体,然后在窗体下部添加 5 个按钮。代码如下:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo extends JFrame implements ActionListener {
private JPanel cardPanel; // 用于存放卡片的面板
private CardLayout cardLayout; // 卡片布局管理器
public CardLayoutDemo() {
super("CardLayout Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建窗体下部的按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 5));
// 创建 5 个按钮
JButton prevButton = new JButton("上一张");
JButton nextButton = new JButton("下一张");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
// 将按钮添加到按钮面板中
buttonPanel.add(prevButton);
buttonPanel.add(nextButton);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
// 为按钮添加事件监听器
prevButton.addActionListener(this);
nextButton.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
// 创建卡片面板,并设置卡片布局管理器
cardPanel = new JPanel();
cardLayout = new CardLayout();
cardPanel.setLayout(cardLayout);
// 创建三个面板,分别用于显示红色、蓝色、绿色卡片
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.RED);
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.BLUE);
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.GREEN);
// 将面板添加到卡片面板中
cardPanel.add(redPanel, "red");
cardPanel.add(bluePanel, "blue");
cardPanel.add(greenPanel, "green");
// 将按钮面板和卡片面板添加到窗体中
add(buttonPanel, BorderLayout.SOUTH);
add(cardPanel, BorderLayout.CENTER);
// 设置窗体大小并显示窗体
setSize(400, 300);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 判断点击的是哪个按钮,并根据按钮名称切换卡片
switch (e.getActionCommand()) {
case "上一张":
cardLayout.previous(cardPanel);
break;
case "下一张":
cardLayout.next(cardPanel);
break;
case "1":
cardLayout.show(cardPanel, "red");
break;
case "2":
cardLayout.show(cardPanel, "blue");
break;
case "3":
cardLayout.show(cardPanel, "green");
break;
}
}
public static void main(String[] args) {
new CardLayoutDemo();
}
}
```
上面的代码中,我们创建了一个名为 `CardLayoutDemo` 的类,继承自 `JFrame` 类,并实现了 `ActionListener` 接口,用于监听按钮的点击事件。
在类的构造方法中,我们首先创建了窗体下部的按钮面板,并添加了 5 个按钮。然后创建了一个卡片面板,并设置了卡片布局管理器。接着,我们创建了三个面板,分别用于显示红色、蓝色、绿色卡片,并将它们添加到卡片面板中。最后,将按钮面板和卡片面板添加到窗体中,并设置窗体大小并显示窗体。
在 `actionPerformed` 方法中,我们根据点击的按钮名称判断应该切换到哪个卡片,然后使用卡片布局管理器的 `show`、`previous`、`next` 方法切换卡片。
相关推荐
![](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)