创建不同GUI显示对象并使用CardLayout在它们之间切换的最佳方法是什么? (JAVA)

时间:2014-06-18 02:45:27

标签: java swing user-interface jpanel cardlayout

我想创建一个相当简单的GUI程序,根据用户点击的按钮在面板之间切换。我四处搜索并想出了CardLayout是最好的建议。

基本上在CardLayout的例子中,你创建了一张"卡" (JPanel),然后添加每个组件,如按钮等...并在卡之间切换。

我想要创建的是一个"卡"已经设置了所有组件,在一个单独的类中,只需在主程序中创建一个实例。我是初学者,不知道最好的设计实践,所以我不想创建自己的扩展JPanel的类,我很确定这是一个糟糕的设计。

1 个答案:

答案 0 :(得分:1)

您可以像创建JPanel的实例一样直接添加组件。

您需要确保自定义类从JComponentJPanel(最好)扩展,并像任何其他组件一样将它们添加到容器中,例如......

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCard {

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

    public TestCard() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final CardLayout cardLayout = new CardLayout();
                final JPanel cardPane = new JPanel(cardLayout);
                cardPane.add(new Card01(), "Card01");
                cardPane.add(new Card02(), "Card02");

                JToggleButton btnCard01 = new JToggleButton("#1");
                JToggleButton btnCard02 = new JToggleButton("#2");
                ButtonGroup bg = new ButtonGroup();
                bg.add(btnCard01);
                bg.add(btnCard02);
                JPanel buttons = new JPanel();
                buttons.add(btnCard01);
                buttons.add(btnCard02);

                btnCard01.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cardLayout.show(cardPane, "Card01");
                    }
                });
                btnCard02.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cardLayout.show(cardPane, "Card02");
                    }
                });
                btnCard01.setSelected(true);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(cardPane);
                frame.add(buttons, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Card01 extends JPanel {

        public Card01() {
            setLayout(new GridBagLayout());
            add(new JLabel("#1"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

    public class Card02 extends JPanel {

        public Card02() {
            setLayout(new GridBagLayout());
            add(new JLabel("#2"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

}