简体   繁体   English

java swing CardLayout中每个Card的单独类

[英]A individual class for each Card in java swing CardLayout

For architecture and design purposes I would like to design my GUI with a class for each card in a Java Swing CardLayout. 出于架构和设计目的,我想为Java Swing CardLayout中的每张卡设计一个带有类的GUI。 and then have a mainapp that builds the GUI. 然后有一个用于构建GUI的mainapp。 I am having trouble doing this right now. 我现在无法执行此操作。

I would like to example have a class for the main menu with all the button locations etc. and then just instantiate that card and add it to the layout in another class. 我想举例说明一个主菜单类,其中包含所有按钮位置等,然后实例化该卡并将其添加到另一个类的布局中。 Does anyone know how to achieve this? 有谁知道如何实现这一目标?

Perhaps you want to give your class that uses the CardLayout a public loadCard method, something like 也许您想为使用CardLayout的类提供一个公共loadCard方法,例如

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

where cardHolderPanel is the container that holds the cards. 其中cardHolderPanel是保存卡的容器。

Since your creating classes to act as cards, consider having them all extend from a base abstract class or an interface that has a method that allows this class to hold its own key String. 由于创建类可以用作卡,因此请考虑将它们全部从基本抽象类或具有允许该类保留其自己的键String的方法的接口扩展。 Either that or simply use the JComponent name property to have a component hold its own key String, one that can easily be obtained via getName() . 要么只是使用JComponent name属性,要么让一个组件拥有自己的键String,可以通过getName()轻松获得该键。

For a more detailed answer, you may need to give us more details on your current application and its structure. 要获得更详细的答案,您可能需要向我们提供有关当前应用程序及其结构的更多详细信息。

very simple example that held Swing Objects generated from different Java Classes 一个非常简单的示例,其中包含从不同的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.
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM