每当我按下按钮时如何制作刷新项目面板?

How to make a refreshing items panel whenever I push a button?

CardLayout cardlay = new CardLayout();
JPanel middle = new JPanel(cardlay);
//what if inside the cardlayout, we always call a new itemPane
        
JTextPane ta = new JTextPane();
ta.setEditable(false);
ta.setContentType("text/html");
ta.setText("<html><h2 style=\"color:white;\">");
        
ta.setBackground(Color.black);
        
middle.add(ta, "text");

JButton item = new JButton("Item");
item.setPreferredSize(new Dimension(200, 60));
panel.add(item);
item.addActionListener(
                   new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent event) {
                     middle.add("item", new ItemsPanel());
                     cardlay.show(middle, "item");
                    }
                   }
                 );
this.add(BorderLayout.SOUTH, item);
this.add(BorderLayout.CENTER, middle);

ItemsPanel.java 的粗略布局: public class ItemsPanel 扩展 JPanel {

public ItemsPanel() {
    GridLayout potionGrid = new GridLayout(4, 4);
    this.setLayout(potionGrid);
    for(Inventory po : CessPool.selected.inventory) {
        if (po instanceof Potion) {
            JButton sumn = new JButton();
            sumn.setText(po.name);
            this.add(sumn);
        }
    }
}

}

所以,我想在我的角色扮演游戏中制作一个物品面板。将有一个黑屏讲述战斗,但每当我想打开项目面板时,我希望它显示项目面板。现在,如果我要使用药剂,我绝对会少一剂药剂。但是我如何确保每次打开它时它都刷新,因为这种方式行不通,我怀疑是由于面板已经接收到没有 ItemsPanel 的 JPanel 中间。

P.S。你看,如果我按下一个药水的按钮,它会用完那个药水,应该从项目面板中删除。我如何使用 CardLayout 做到这一点,或者是否有另一种方法可以在每次刷新时更改面板?

这是当您按 JButton 时刷新项目 JPanel 的方法。这个例子不多也不少。发布此答案后,请勿在评论或问题中添加任何要求。

这是一个 JPanel GUI。

这是 GUI 的另一个 JPanel

我在主要 JPanel 上使用 CardLayout 在 RPG JPanel 和 Potion JPanel 之间来回切换。

当您按下药水 JButton 时,该药水将从物品栏中移除并且 JButton 被禁用。

当你回到 RPG JPanel,然后回到药水 JPanel 时,药水 JPanel 会用剩余的药水重建。

这是完整的可运行代码。这就是人们所说的最小可运行示例的意思。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RefreshingItemsGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new RefreshingItemsGUI());
    }

    private CardLayout cardLayout;

    private Inventory inventory;

    private JPanel cardPanel;

    public RefreshingItemsGUI() {
        this.inventory = new Inventory();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("RPG");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cardPanel = createMainPanel();
        frame.add(cardPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        cardLayout = new CardLayout();
        JPanel panel = new JPanel(cardLayout);

        panel.add(createButtonPanel(), "Button");
        panel.add(createPotionPanel(), "Potion");

        return panel;
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(50, 150, 50, 150));

        JButton button = new JButton("Potions");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                cardLayout.show(cardPanel, "Potion");
            }
        });
        panel.add(button, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createPotionPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel buttonPanel = new JPanel(new GridLayout(0, 3, 10, 10));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        addButtons(buttonPanel);

        JPanel rpgPanel = new JPanel(new FlowLayout());
        JButton rpgButton = new JButton("RPG");
        rpgButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                addButtons(buttonPanel);
                cardLayout.show(cardPanel, "Button");
            }
        });
        rpgPanel.add(rpgButton);

        panel.add(buttonPanel, BorderLayout.CENTER);
        panel.add(rpgPanel, BorderLayout.AFTER_LAST_LINE);

        return panel;
    }

    private void addButtons(JPanel panel) {
        panel.removeAll();

        PotionListener listener = new PotionListener();

        int index = 0;
        for (Potion potion : inventory.getPotions()) {
            JButton button = new JButton(potion.getName());
            button.addActionListener(listener);
            button.setActionCommand(Integer.toString(index++));
            panel.add(button);
        }
    }

    public class PotionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            int index = Integer.valueOf(button.getActionCommand());
            inventory.removePotion(index);
            button.setEnabled(false);
        }

    }

    public class Inventory {

        private final List<Potion> potions;

        public Inventory() {
            this.potions = createPotions();
        }

        private List<Potion> createPotions() {
            List<Potion> potions = new ArrayList<>();

            potions.add(new Potion("Healing"));
            potions.add(new Potion("Double Damage"));
            potions.add(new Potion("Group Healing"));
            potions.add(new Potion("Healing"));
            potions.add(new Potion("Group Healing"));

            return potions;
        }

        public List<Potion> getPotions() {
            return potions;
        }

        public void removePotion(int index) {
            potions.remove(index);
        }

    }

    public class Potion {

        private final String name;

        public Potion(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

    }

}