这是当您按下JButton 时如何制作刷新项目JPanel。这个例子不多也不少。发布此答案后,请勿向 cmets 或问题添加任何更多要求。
这是 GUI 的JPanel。
这是 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;
}
}
}