gpt4 book ai didi

java - 在不执行新 JFrame 的情况下刷新 JFrame Java

转载 作者:搜寻专家 更新时间:2023-10-31 20:10:45 24 4
gpt4 key购买 nike

我正在开发一款游戏,您可以在其中升级商店中的统计数据。当你买东西时,它应该刷新显示你的硬币的 JLabel

有没有办法在不创建新的 JFrame 的情况下做到这一点?

public void actionPerformed(ActionEvent e) {
user.setCoin(user.getCoin() - 5);
user.setMaxJump(5);
EpixController.getInstance().coinsUpdate(user, -5);
SwingUtilities.updateComponentTreeUI(epix);
epix.revalidate();
epix.repaint();
}

这是我要刷新 JFrame epix 的 ActionListener 的代码

这是 View 的代码

public EpixView(User user) {


this.setUser(user);

JFrame epix = new JFrame();
System.out.println(user.getMaxJump());
epix.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epix.setResizable(false);
epix.setBounds(100, 100, 801, 523);
epix.setLocationRelativeTo(null);
epix.getContentPane().removeAll();


/*
* menuCard
*/
menuCard.setLayout(null);

//coin
menuCardCoinImage.setBounds(0, 10, 64, 64);
menuCard.add(menuCardCoinImage);
menuCardCoinImage.setIcon(new ImageIcon(coinSpinPath));
menuCardCoinLabel = new JLabel("" + getUser().getCoin());
menuCardCoinLabel.setFont(new Font("Modern No. 20", Font.PLAIN, 26));
menuCardCoinLabel.setBounds(67, 10, 56, 64);
menuCard.add(menuCardCoinLabel);

//player
menuCardPlayerLabel = new JLabel("Willkommen zurück, " + getUser().getUsername());
menuCardPlayerLabel.setBounds(598, 18, 239, 36);
menuCard.add(menuCardPlayerLabel);


spielenButton.setBounds(485, 319, 89, 23);
menuCard.add(spielenButton);

ranglisteButton.setBounds(485, 353, 89, 23);
menuCard.add(ranglisteButton);

menuCardBackgroundImage.setIcon(new ImageIcon(backgroundPath));
menuCardBackgroundImage.setBounds(0, 0, 801, 523);
menuCard.add(menuCardBackgroundImage);


cards.add(menuCard, "menuCard");


/*
* upgradeCard
*/
upgradeCard.setLayout(null);

//coin
upgradeCardCoinImage.setBounds(0, 10, 64, 64);
upgradeCardCoinImage.setIcon(new ImageIcon(coinSpinPath));
upgradeCard.add(upgradeCardCoinImage);
upgradeCardCoinLabel = new JLabel(""+ getUser().getCoin());
upgradeCardCoinLabel.setBounds(67, 10, 56, 64);
upgradeCardCoinLabel.setFont(new Font("Modern No. 20", Font.PLAIN, 26));
upgradeCard.add(upgradeCardCoinLabel);

//player
upgradeCardPlayerLabel = new JLabel("Willkommen zurück, " + getUser().getUsername());
upgradeCardPlayerLabel.setBounds(598, 18, 239, 36);
upgradeCard.add(upgradeCardPlayerLabel);

//maxHealthBar
healthLabel.setHorizontalAlignment(SwingConstants.RIGHT);
healthLabel.setBounds(191, 127, 110, 36);
upgradeCard.add(healthLabel);
JProgressBar maxHealthBar = new JProgressBar();
maxHealthBar.setValue(0);
maxHealthBar.setBounds(327, 127, 185, 36);
upgradeCard.add(maxHealthBar);
plusHealthButton.setBounds(522, 130, 41, 30);
upgradeCard.add(plusHealthButton);

//maxJumpBar
jumpLabel.setHorizontalAlignment(SwingConstants.RIGHT);
jumpLabel.setBounds(191, 174, 110, 36);
upgradeCard.add(jumpLabel);
JProgressBar maxJumpBar = new JProgressBar();
maxJumpBar.setValue(0);
maxJumpBar.setBounds(327, 174, 185, 36);
upgradeCard.add(maxJumpBar);
plusJumpButton.setBounds(522, 177, 41, 30);
plusJumpButton.addActionListener(new PlusJumpListener(getUser(), maxJumpBar, epix));
upgradeCard.add(plusJumpButton);


//maxSpeed
speedLabel.setHorizontalAlignment(SwingConstants.RIGHT);
speedLabel.setBounds(191, 221, 110, 36);
upgradeCard.add(speedLabel);
JProgressBar maxSpeedBar = new JProgressBar();
maxSpeedBar.setValue(0);
maxSpeedBar.setBounds(327, 221, 185, 36);
upgradeCard.add(maxSpeedBar);
plusSpeedButton.setBounds(522, 224, 41, 30);
upgradeCard.add(plusSpeedButton);

//damageBar
damageLabel.setHorizontalAlignment(SwingConstants.RIGHT);
damageLabel.setBounds(191, 268, 110, 36);
upgradeCard.add(damageLabel);
JProgressBar damageBar = new JProgressBar();
damageBar.setValue(0);
damageBar.setBounds(327, 268, 185, 36);
upgradeCard.add(damageBar);
plusDamageButton.setBounds(522, 271, 41, 30);
upgradeCard.add(plusDamageButton);


levelAuswählenButton.setBounds(395, 361, 168, 30);
upgradeCard.add(levelAuswählenButton);

upgradeZurückButton.setBounds(254, 361, 131, 30);
upgradeCard.add(upgradeZurückButton);

upgradeCardBackgroundImage.setBounds(0, 0, 801, 523);
upgradeCardBackgroundImage.setIcon(new ImageIcon(backgroundPath));
upgradeCard.add(upgradeCardBackgroundImage);

cards.add(upgradeCard, "upgradeCard");


/*
* levelAuswahlCard
*/
levelAuswahlCard.setLayout(null);

level1Button.setBounds(67, 159, 171, 170);
EpixController.getInstance().checkLevelSaved(user, level1Button);
levelAuswahlCard.add(level1Button);


level2Button.setBounds(308, 159, 171, 170);
EpixController.getInstance().checkLevelSaved(user, level2Button);
levelAuswahlCard.add(level2Button);


bossLevelButton.setBounds(550, 159, 171, 170);
EpixController.getInstance().checkLevelSaved(user, bossLevelButton);
levelAuswahlCard.add(bossLevelButton);

levelZurückBtton.setBounds(334, 378, 121, 28);
levelAuswahlCard.add(levelZurückBtton);

levelAuswahlCardCoinImage.setBounds(0, 10, 64, 64);
levelAuswahlCardCoinImage.setIcon(new ImageIcon(coinSpinPath));
levelAuswahlCard.add(levelAuswahlCardCoinImage);

levelAuswahlCardCoinLabel = new JLabel("" + getUser().getCoin());
levelAuswahlCardCoinLabel.setBounds(67, 10, 56, 64);
levelAuswahlCardCoinLabel.setFont(new Font("Modern No. 20", Font.PLAIN, 26));
levelAuswahlCard.add(levelAuswahlCardCoinLabel);

levelAuswahlCardPlayerLabel = new JLabel("Willkommen zurück, " + getUser().getUsername());
levelAuswahlCardPlayerLabel.setBounds(598, 18, 239, 36);
levelAuswahlCard.add(levelAuswahlCardPlayerLabel);

levelAuswahlCardBackgroundImage.setIcon(new ImageIcon(backgroundPath));
levelAuswahlCardBackgroundImage.setBounds(0, 0, 801, 523);
levelAuswahlCard.add(levelAuswahlCardBackgroundImage);

cards.add(levelAuswahlCard, "levelAuswahlCard");


/*
* Listener
*/
//menuCard
spielenButton.addActionListener(new MenuCardButtonListener(cards));

//upgradeCard
upgradeZurückButton.addActionListener(new UpgradeCardButtonListener(cards));
levelAuswählenButton.addActionListener(new UpgradeCardButtonListener(cards));

//levelAuswahlCard
levelZurückBtton.addActionListener(new LevelAuswahlCardButtonListener(cards, getUser(), epix));
level1Button.addActionListener(new LevelAuswahlCardButtonListener(cards, getUser(), epix));
level2Button.addActionListener(new LevelAuswahlCardButtonListener(cards, getUser(), epix));
bossLevelButton.addActionListener(new LevelAuswahlCardButtonListener(cards, getUser(), epix));


epix.setContentPane(cards);
epix.setVisible(true);

}

最佳答案

您不必刷新框架或 JPanel。在 Actionlistener 中,您必须像在 levelauswahllistener 中那样显示 CardLayout“升级卡”。然后它应该刷新它。

关于java - 在不执行新 JFrame 的情况下刷新 JFrame Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30122185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com