首页 > 解决方案 > 如何添加显示我拥有多少冠军的标签?

问题描述

我一直坚持为我拥有多少个字符添加标签。例如,我会点击金色按钮 20 次。现在,我有 20 枚金币,因为我有 20 枚金币,所以我可以花 20 枚金币购买一个角色。现在我拥有 1 个字符,我想要一个标签出现在它显示我拥有多少个字符的位置。对于前面的示例,将出现一个标签并显示“您拥有 1 个字符”。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Clicker {
    int first = 0;
    int second = 1;
    Clicker() {
        JButton button = new JButton ("gold");
        button.setBounds(0,0,100,40);

        JButton button1 = new JButton ("Gold mult");
        button1.setBounds(0,80,100,40);

        JButton button2 = new JButton ("Champion");
        button2.setBounds(0,140,100,40);

        final JLabel label = new JLabel ("First # "+ first);
        label.setBounds(110,0,200,40);

        final JLabel label1 = new JLabel ("Second # "+ second);
        label1.setBounds(110,80,100,40);

        JFrame frame = new JFrame ("clicker game");
        frame.add(button);
        frame.add(button1);
        frame.add(button2);
        frame.add(label);
        frame.add(label1);

        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,500);
        frame.setLayout(null);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                first += second;
                label.setText("First #"+ first);
            }
        });

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(first > 2*second){
                    second++;
                    first-=2*second;
                    //first= first - (2*second);
                    label.setText("First #" + first);
                    label.setText("second # " + second);                       
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (first>=20) {
                    if (second==2){
                        first++;   
                    }
                    first -= 20;
                    label.setText("first # " + first);
                    label1.setText("second # " + second);
                }

            }
        });
    }

    public static void main(String[] args) {
        new Clicker();
    }
}

标签: javaswingjframejbutton

解决方案


我不明白你的 Clicker 游戏。我建议您在开始编码之前进行一些游戏设计(图像有帮助)。

  1. 如果您使用的是 Eclipse-IDE,则有一种重构方法,您可以在其中更改变量的名称,它将通过您的代码传播。标签使用:lblOfGold 按钮使用:btnOfGold
  2. 创建您自己的自定义 JButton,例如 ClickerButton 扩展 JButton 并放置一些与您的游戏相关的属性和方法。
  3. 创建一个控制器类来处理您的游戏交互,并通过访问 ClickerButton 您可以执行一些操作。我建议您在控制器中注册您的按钮,控制器将为您执行操作。不要将代码放在每个按钮的动作侦听器中。这会增加复杂性并使代码不清楚。为 ActionListener 使用一个新类。并在控制器内注册您的按钮。控制器将调用您的类 ClickerActionListener 并处理该操作。

  4. 如果您想开发 Clicker 游戏,我建议您使用 Java-FX。您可以创建场景并在场景中使用 3D 模型和一些不错的动画。

在此处输入图像描述

示例代码:

公共类 ClickerGame 扩展 JFrame {

private static final String APPLICATION_TITLE = "Clicker Game (Kaisel)";

private static final int WINDOW_WIDTH = 400;
private static final int WINDOW_HEIGHT = 500;

private static final int BUTTON_WIDTH = 100;
private static final int BUTTON_HEIGHT = 40;
private static final int LABEL_HEIGHT = 40;

private ClickerController controller;
private Map<String, ClickerLabel> mapOfLabels;
int first = 0;
int second = 1;

List<String> labelsName;
List<String> labelsKey;
List<String> buttonsName;
List<String> buttonsKey;
List<List<Integer>> boundsLabel;
List<List<Integer>> boundsButton;

ClickerGame() {
    controller = new ClickerController();
    mapOfLabels = new HashMap<>();
    loadResources();
}

private void loadResources() {
    labelsName = Arrays.asList("First # "+ first, "Second # "+ second);
    labelsKey = Arrays.asList("FirstKey", "SecondKey");
    buttonsName = Arrays.asList("Gold", "Gold mult", "Champion");
    buttonsKey = Arrays.asList("GoldKey", "GoldMultKey", "ChampionKey");
    boundsLabel = Arrays.asList(
            Arrays.asList(110,0,200,LABEL_HEIGHT),
            Arrays.asList(110,80,100,LABEL_HEIGHT) );
    boundsButton = Arrays.asList(
            Arrays.asList(0,0,BUTTON_WIDTH,BUTTON_HEIGHT),
            Arrays.asList(0,80,BUTTON_WIDTH,BUTTON_HEIGHT),
            Arrays.asList(0,140,BUTTON_WIDTH,BUTTON_HEIGHT) );
}

public void buildUI() {
    // variables
    int i;
    ClickerLabel label;
    ClickerButton button;
    // bounds rectangle
    int x;
    int y;
    int width;
    int height;
    List<Integer> boundRectangle;

    GridLayout layout = new GridLayout();
    JPanel panel = new JPanel();
    panel.setLayout(null);

    i = 0;
    for (String name : labelsName) {
        label = new ClickerLabel(name);
        boundRectangle = boundsLabel.get(i);
        x = boundRectangle.get(0);
        y = boundRectangle.get(1);
        width = boundRectangle.get(2);
        height = boundRectangle.get(3);
        label.setBounds( x, y, width, height );
        mapOfLabels.put(labelsKey.get(i),label);
        panel.add(label);
        i++;
    }

    i = 0;
    for (String name : buttonsName) {
        button = new ClickerButton(name);
        button.setId(buttonsKey.get(i));
        controller.register(button);

        boundRectangle = boundsButton.get(i);
        x = boundRectangle.get(0);
        y = boundRectangle.get(1);
        width = boundRectangle.get(2);
        height = boundRectangle.get(3);
        button.setBounds( x, y, width, height );

        panel.add(button);
        i++;
    }

    this.add(panel);
}

public void launchApplication() {
    setTitle(APPLICATION_TITLE);
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
}

public static void main(String[] args) {
    ClickerGame game = new ClickerGame();
    game.buildUI();
    game.launchApplication();
}

private class ClickerButton extends JButton {
    private String buttonIdentification;
    ClickerButton(String parText) {
        super(parText);
    }
    public String getId() {
        return buttonIdentification;
    }
    public void setId(String btnId) {
        this.buttonIdentification = btnId;
    }
}

private class ClickerLabel extends JLabel {
    private String identification;
    ClickerLabel(String parText) {
        super(parText);
    }
    public String getId() {
        return identification;
    }
    public void setId(String btnId) {
        this.identification = btnId;
    }
}

private class ClickerActionListener implements ActionListener {
    private ClickerController controller;
    ClickerActionListener(ClickerController parController) {
        controller = parController;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        ClickerButton source = (ClickerButton) e.getSource();
        if (source != null) {
            controller.handle(source.getId());
        }
    }
}

private class ClickerController {
    ClickerActionListener listener;
    ClickerController() {
        listener = new ClickerActionListener(this);
    }
    public boolean register(ClickerButton parButton) {
        if (parButton != null) {
            parButton.addActionListener(listener);
            return true;
        }
        return false;
    }
    public void handle(String parId) {
        if (parId.equals("GoldKey")) {
            controller.handlingGold();
        } else if (parId.equals("GoldMultKey")) {
            controller.handlingGoldMult();
        } else if (parId.equals("ChampionKey")) {
            controller.handlingChampion();
        }
    }

    private void handlingGold() {
        ClickerLabel lblFirst = mapOfLabels.get("FirstKey");
        // TODO document in here what this code is doing..
        first += second;
        lblFirst.setText("First #"+ first);
    }

    private void handlingGoldMult() {
        ClickerLabel lblFirst = mapOfLabels.get("FirstKey");
        ClickerLabel lblSecond = mapOfLabels.get("SecondKey");
        // TODO document in here what this code is doing..
        if(first > 2*second){
            second++;
            first-=2*second;
            //first= first - (2*second);
            lblFirst.setText("First #" + first);
            lblSecond.setText("second # " + second);                       
        }
    }

    private void handlingChampion() {
        ClickerLabel lblFirst = mapOfLabels.get("FirstKey");
        ClickerLabel lblSecond = mapOfLabels.get("SecondKey");
        // TODO document in here what this code is doing..
        if (first>=20) {
            if (second==2){
                first++;   
            }
            first -= 20;
            lblFirst.setText("first # " + first);
            lblSecond.setText("second # " + second);
        }
    }
}

}


推荐阅读