簡體   English   中英

CardLayout在最后一張卡上關閉

[英]CardLayout to close on last card

所以我有一個帶有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
                }
        }
}

Window繼承的dispose()呢? 確保設置:

 
 
 
  
  jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  

JFrame frame = ...

// ...

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

例如:

在此處輸入圖片說明

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();
            }
        });
    }
}

我擴展了CardLayout以添加一些功能。 功能之一是isNextCardAvailable()方法。 有關所有功能,請參閱卡布局重點

問題在於確定哪張卡是最后一張。 您可以使用卡片的String數組索引來管理的當前位置,並使用show方法顯示下一個“卡片”。 當您超出卡陣列索引時,就可以處置JFrame

如果運行System.exit(0),則將關閉所有應用程序,但是,如果僅關閉JFrame,則可以使用JFrameObject.dispose()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM