CardLayout关闭最后一张牌

时间:2013-04-03 23:09:10

标签: java swing cardlayout

所以我有一个带有CardLayout的JPanel。 正如预期的那样,这个CardLayout管理框架中面板的切换。 切换由两个按钮完成:“后退”和“下一步”。

我想知道是否有办法关闭整个应用程序(即调用System.exit(0)),当它在最后一张卡上并再次按下“下一步”时。

我到处寻找解决方案,但我找不到任何东西。

问题是:我不知道如何检查哪一个是最后一个。

以下是我的代码的听众摘录:

public void actionPerformed(ActionEvent arg0) {
        CardLayout l = (CardLayout) holder.getLayout();
        if(arg0.getSource() == opt[1]){ //opt[1] is the "Next" button


                //Insert if statement here to check if
                //the CardLayout is on the last card
                {
                System.exit(0);
                } else {
                    l.next(holder); //holder is the JPanel with the CardLayout
                }
        }
}

4 个答案:

答案 0 :(得分:3)

dispose()继承的Window怎么样? 确保设置:

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

<击>

JFrame frame = ...

// ...

frame.setVisible(false); // hide the GUI
frame.dispose(); // destroy and release the GUI resources

例如:

enter image description here

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

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

public class CardLayoutGUI
{
    private JFrame frame;
    private JButton btnBack;
    private JButton btnNext;
    private CardLayout cLayout;
    private JPanel panUp;
    private JPanel panDown;

    private static final String[] cards =
    {"card1", "card2", "card3", "card4", "card5"}; 

    private int currentCard = 0;

    public void init()
    {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ((JPanel)frame.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        btnBack = new JButton("Back");
        btnNext = new JButton("Next");

        btnBack.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                btnNext.setText("Next");
                currentCard--;
                cLayout.show(panUp, cards[currentCard]);
                if(currentCard == 0) btnBack.setVisible(false);
            }
        });

        btnNext.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                btnBack.setVisible(true);
                currentCard++;

                if(currentCard == cards.length - 1) // last card
                {
                    btnNext.setText("Exit");
                    cLayout.show(panUp, cards[currentCard]);
                }
                else if(currentCard >= cards.length)
                {
                    frame.setVisible(false);
                    frame.dispose();
                }
                else
                {
                    cLayout.show(panUp, cards[currentCard]);
                }
            }
        });


        cLayout = new CardLayout();
        panUp = new JPanel(cLayout);
        panDown = new JPanel();
        frame.add(panUp, BorderLayout.CENTER);
        frame.add(panDown, BorderLayout.SOUTH);
        panDown.add(btnBack);
        panDown.add(btnNext);

        for(int i = 0; i < cards.length; i++) createPanels(panUp, cards[i]);

        frame.pack();
        frame.setLocationRelativeTo(null);

        btnBack.setVisible(false);
    }

    public void showGUI()
    {
        frame.setVisible(true);
    }

    private void createPanels(JPanel container, String label)
    {
        JPanel pan = new JPanel();
        pan.add(new JLabel(label));
        container.add(pan, label);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                CardLayoutGUI clg = new CardLayoutGUI();
                clg.init();
                clg.showGUI();
            }
        });
    }
}

答案 1 :(得分:3)

我扩展了CardLayout以添加一些功能。其中一个功能是isNextCardAvailable()方法。有关所有功能,请参阅Card Layout Focus

答案 2 :(得分:2)

问题是确定哪张卡是最后一张。您可以使用卡String数组索引来管理当前位置,并使用show方法显示下一个“卡”。当您超过卡片阵列索引时,您可以处置JFrame

答案 3 :(得分:0)

如果运行System.exit(0),那将关闭所有应用程序,但如果只关闭JFrame,则可以使用JFrame Object.dispose()。