Swing CardsLayout中button相互切换问题

Z.Chen 2020-02-25 01:32:37
要求:建一个frame, 包含三个panel, 每个panel包含一个button。
点击button1进入panel2,
点击button2进入panel3,
点击button3进入panel1.


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MyFrame extends JFrame
{
JComboBox<String> options = new JComboBox<>();
JPanel cards = new JPanel();

public MyFrame(String title)
{
super(title);

Container contentPane = getContentPane();
//CardLayout cardLayout = (CardLayout)cards.getLayout();

// 给顶层容器,设置 BorderLayout
contentPane.setLayout(new BorderLayout());


contentPane.add(cards, BorderLayout.CENTER);




// 创建第1个面板
MyPanel p1 = new MyPanel();


JButton P1Button = new JButton("build a character");
p1.add(P1Button);
// P1Button.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent e)
// {
// cardLayout.show(cards, "CharacterCreation");
// }
// });


//////////////////////////////////////////////////////////////////////////////////////
// 创建第2个面板
MyPanel p2 = new MyPanel();


JButton P2Button = new JButton("start battle");
p2.add(P2Button);
//P2Button.addActionListener();


//////////////////////////////////////////////////////////////////////////////////////
// 创建第3个面板
MyPanel p3 = new MyPanel();


JButton P3Button = new JButton("play again");
p3.add(P3Button);
//P3Button.addActionListener();


/////////////////////////////////////////////////
cards.setLayout(new CardLayout());
cards.add(p1, "Splash");
cards.add(p2, "CharacterCreation");
cards.add(p3, "BattleSummary");


if(???)
{
cardLayout.show(cards, "Splash");
}
else if(??)
{
cardLayout.show(cards, "CharacterCreation");
}
else if(???)
{
cardLayout.show(cards, "BattleSummary");
}
}


}


代码最后是一些混乱的思路。。。
谢谢了

在我的代码里,40行被//掉了。如果不//掉,则我需要在20行定义
CardLayout cardLayout = (CardLayout)cards.getLayout();

但是这样做则会报错:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.FlowLayout cannot be cast to java.awt.CardLayout
...全文
224 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2020-02-26
  • 打赏
  • 举报
回复
引用 楼主 Z.Chen 的回复:
要求:建一个frame, 包含三个panel, 每个panel包含一个button。 代码最后是一些混乱的思路。。。 谢谢了 在我的代码里,40行被//掉了。如果不//掉,则我需要在20行定义 CardLayout cardLayout = (CardLayout)cards.getLayout(); 但是这样做则会报错: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.FlowLayout cannot be cast to java.awt.CardLayout

·public class MyFrame extends JFrame implements ActionListener {

    CardLayout cl=new CardLayout();

    public MyFrame() throws HeadlessException {
        setTitle("卡片布局");
        setSize(400,300);
        setLocation(200,200);
        setLayout(cl);

        Panel p1=new Panel();
        Button b1=new Button("第二张卡片");
        b1.addActionListener(this::actionPerformed);
        p1.add(b1);

        Panel p2=new Panel();
        Button b2=new Button("第三张卡片");
        b2.addActionListener(this::actionPerformed);
        p2.add(b2);

        Panel p3=new Panel();
        Button b3=new Button("第一张卡片");
        b3.addActionListener(this::actionPerformed);
        p3.add(b3);

        add(p1);
        add(p2);
        add(p3);

        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        cl.next(getContentPane());
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

sunyiz 2020-02-26
  • 打赏
  • 举报
回复
你注释掉的代码如果放开,会报错时正常的
因为刚 new 出来的 JPanel 也是有个默认的布局管理器的,就是一个 FlowLayout
你把之强转成 CardLayout 肯定不行

你需要自己 new 一个 CardLayout
然后通过 setLayout 的方式设置给这个 JPanel
就像你 66 行写的那样
不过你需要为的 CardLayout 设定一个引用
(因为切换面板时,需要用到 CardLayout 的引用)
比如这样:
CardLayout cardLayout = new CardLayout();
cards.setLayout(cardLayout);
……
……
三仙半 2020-02-25
  • 打赏
  • 举报
回复
我按照自己的思路实现了你所说的功能需求,给你做个参考吧。因为不知道你的MyPanel是如何定义的,就用JPanel代替了。

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestCardLayout extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
new TestCardLayout().setVisible(true);
}

// 窗体的内容面板
private JPanel contentPanel;
// 卡片式布局管理器,用于在内容面板中依次切换需要的面板
private CardLayout layout;

public TestCardLayout() {
setBounds(0, 0, 400, 300);

contentPanel = new JPanel();
layout = new CardLayout();
contentPanel.setLayout(layout);
setContentPane(contentPanel);

// 构造面板1,注意给按钮增加的事件监听
JPanel p1 = new JPanel();
JButton btn1 = new JButton("Go p2");
btn1.addActionListener(this);
p1.add(btn1);

// 构造面板2
JPanel p2 = new JPanel();
JButton btn2 = new JButton("Go p3");
btn2.addActionListener(this);
p2.add(btn2);

// 构造面板3
JPanel p3 = new JPanel();
JButton btn3 = new JButton("Go p1");
btn3.addActionListener(this);
p3.add(btn3);

// 将各个面板增加到内容面板
contentPanel.add(p1);
contentPanel.add(p2);
contentPanel.add(p3);

}

@Override
public void actionPerformed(ActionEvent e) {
// 卡片式布局管理器是可以依次切换面板的
layout.next(contentPanel);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧