我一直坚持为自己拥有的字符添加标签。例如,我将单击金色按钮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();
}
}
答案 0 :(得分:0)
我不了解您的Clicker游戏。我建议您在开始编码之前做一些游戏设计(图像帮助)。
创建一个用于处理游戏交互的Controller类,并通过访问ClickerButton可以执行一些操作。 我建议您将按钮注册到控制器中,控制器将为您执行操作。不要将每个按钮的代码放入动作侦听器中。这将增加复杂性并使代码不清楚。对ActionListener使用新的类。并在控制器内部注册您的按钮。控制器将调用您的类ClickerActionListener并处理该操作。
如果您要开发Clicker Game,建议您使用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);
}
}
}
}