java swing CardLayout中每张卡的单独类

时间:2011-12-03 22:53:52

标签: java swing cardlayout

出于架构和设计目的,我想为Java Swing CardLayout中的每张卡设计一个类。然后有一个构建GUI的mainapp。我现在无法做到这一点。

我想举例说明主菜单的所有按钮位置等等,然后只是实例化该卡并将其添加到另一个类的布局中。有谁知道如何实现这个目标?

2 个答案:

答案 0 :(得分:3)

也许你想给你的使用CardLayout的类一个公共的loadCard方法,比如

public void loadCard(JComponent component, String key) {
  cardHolderPanel.add(component, key);
}

其中cardHolderPanel是持有卡片的容器。

由于您创建的类充当卡片,因此请考虑将它们全部扩展为基本抽象类或具有允许此类保存其自己的键字符串的方法的接口。或者只是使用JComponent name属性让组件拥有自己的键String,可以通过getName()轻松获得。

有关更详细的答案,您可能需要向我们提供有关您当前应用程序及其结构的更多详细信息。

答案 1 :(得分:3)

非常简单的示例,它保存了从不同Java类生成的Swing对象

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OnTheFlyImageTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel cardPanel;
    private CardLayout cardLayout;

    public OnTheFlyImageTest() {
        JPanel cp = new JPanel(new BorderLayout());
        cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        cardLayout = new CardLayout(5, 5);
        cardPanel = new JPanel(cardLayout);
        cp.add(cardPanel);
        for (int i = 0; i < 100; i++) {// Create random panels for testing.
            String name = "ImagePanel" + (i + 1);
            String image = (i & 1) == 0 ? "foo.gif" : "bar.gif";
            ImagePanel imgPanel = new ImagePanel(name, image);
            cardPanel.add(imgPanel, name);
            cardLayout.addLayoutComponent(imgPanel, name);
        }
        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        JButton prevButton = new JButton("< Previous");
        prevButton.setActionCommand("Previous");
        prevButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.previous(cardPanel);
            }
        });
        buttonPanel.add(prevButton);
        JButton nextButton = new JButton("Next >");
        nextButton.setActionCommand("Next");
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(cardPanel);
            }
        });
        buttonPanel.add(nextButton);
        JPanel temp = new JPanel(new BorderLayout());
        temp.add(buttonPanel, BorderLayout.LINE_END);
        cp.add(temp, BorderLayout.SOUTH);
        setContentPane(cp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Test");
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new OnTheFlyImageTest().setVisible(true);
            }
        });
    }
}

class ImagePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String imgString;
    private JLabel imgLabel;

    public ImagePanel(String name, String imgString) {
        setName(name);
        this.imgString = imgString;
        setLayout(new BorderLayout());
        // Ensure size is correct even before any image is loaded.
        setPreferredSize(new Dimension(640, 480));
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            System.err.println(getName() + ": Loading and adding image");
            ImageIcon icon = new ImageIcon(imgString);
            imgLabel = new JLabel(icon);
            add(imgLabel);
        }
        super.setVisible(visible);
        if (!visible) { // Do after super.setVisible() so image doesn't "disappear".
            System.err.println(getName() + ": Removing image");
            if (imgLabel != null) { // Before display, this will be null
                remove(imgLabel);
                imgLabel = null; // Hint to GC that component/image can be collected.
            }
        }
    }
}