【问题标题】:CardLayout not showing next cardCardLayout 不显示下一张卡片
【发布时间】:2017-01-04 06:23:21
【问题描述】:

我在使用卡片布局显示下一张卡片时遇到问题,我在这里阅读了文档和各种主题。代码正在“工作”,但如前所述,不显示下一张卡片。三张卡分别工作,但通过next()方法访问时不可见

代码如下:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Start {

public static void main(String[] args) {
    new Start().createFrame();
}

public void createFrame() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Card().createMainGui();
        }
    });
}

class Card1 extends Card {

    public Card1() {
        createGui();
    }

    private void createGui() {
        /*
         * Create buttons and add them to the Card1 Panel
         */

        final JButton button = new JButton("Next");
        button.addActionListener(this);

        add(button);
    }
}

class Card2 extends Card {

    public Card2() {
        createGui();
    }

    private void createGui() {
        /*
         * Create label and add it to the Card2 Panel
         */

        JLabel label = new JLabel("Card2");
        add(label);
    }
}

class Card extends JPanel implements ActionListener {

    JFrame frame;
    final JPanel cards = new JPanel(new CardLayout(20, 20));

    void createMainGui() {
        /*
         * create main frame
         */
        frame = new JFrame("Testframe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*
         * add the different cards to the Card Panel (Card Layout)
         */

        Card1 card1 = new Card1();
        cards.add(card1, "card1");

        Card2 card2 = new Card2();
        cards.add(card2, "card2");

        // Card3 card3 = new Card3();
        // cards.add(card3, "card3");

        /*
         * add the Card panel to the frame, pack it and make it visible
         */
        frame.add(cards, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cl = (CardLayout) cards.getLayout();
        cl.next(cards);
    }
}
}

【问题讨论】:

  • 发布minimal reproducible example。一个 MCVE 将有导入,只有一个 public 类(其他类在它之后粘贴就可以了),一个 main(String[]) 方法来运行它,一旦它干净地编译而没有任何更改。
  • 我不知道你想做什么,你混合了你的组件,仍然很困惑这些对象是什么(cards, Card2, Card3)?
  • @AndrewThompson 我也添加了带有 main 方法的 start 类。这三个课程都是公开的,但一个接一个。可以吗?
  • @BoHalim 卡片是主卡片布局,而卡片 1、卡片 2 和卡片 3 是要添加到卡片布局中的卡片
  • 您只想覆盖JPanels 并从一个移动到另一个?

标签: java swing jframe layout-manager cardlayout


【解决方案1】:

我的代码中的错误是,我从 Card 类扩展了 Card1Card2 类。因此,他们也继承了创建变量cards 的方法。在EventListener 中,引用了错误的cards 变量(在Card1 类中创建的变量),因此EventListener 不起作用。 解决方法:去掉Card1Card2中的extend,把EventListener移到Cards类的内部类中

private class NewBlaBlaListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //do your stuff here
    }
}

并将ActionListener 中的Cards 添加到Card1

card1.addNewBlaBlaListener(new NewBlaBlaListener());

干杯

【讨论】:

  • 很高兴你把它整理好了。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2013-02-23
相关资源
最近更新 更多