2

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

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

4

2 回答 2

3

也许您想为使用 CardLayout 的类提供公共 loadCard 方法,例如

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

其中 cardHolderPanel 是存放卡片的容器。

由于您创建的类充当卡片,请考虑让它们都从基本抽象类或接口扩展,该接口具有允许此类保存其自己的键 String 的方法。要么使用 JComponent 名称属性,要么简单地使用 JComponent 名称属性让组件拥有自己的键字符串,可以通过getName().

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

于 2011-12-03T23:22:39.897 回答
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.
            }
        }
    }
}
于 2011-12-03T23:30:28.697 回答