首页 > 解决方案 > 按下时我的 JButton 什么也没做?

问题描述

目前,我已经创建了一个 cookie 的图像,当按下该图像时,双数会增加 1。我的问题出在我的 CookieClickingConfig 方法中,其中 isPressed 似乎不起作用。如果你能帮我弄清楚这是为什么,那将意味着很多。谢谢!

我尝试添加一个动作监听器而不是一个不起作用的 isPressed,然后写出方法 clickerUpgrade 的代码,而不是从那里引用它也没有改变任何东西(这意味着问题 [最有可能] 是'不是从升级类派生的)。

主要课程 -

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

public class Main {


    public static void main(String args[]) {
        Franels windows = new Franels();
        windows.frameConfig(900, 800);
        windows.cookiePosOrganize();
        windows.amountLConfig();
        windows.cookieLConfig();
        windows.cookieClickingConfig();
    }


}

摇摆班 -

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

public class Franels extends MouseAdapter {
    Upgrades cookieUpgrade = new Upgrades();

    double width, height;
    double amount = 0;
    double clickUpgradeCost = 2;
    double cookieClickAmount = 1;

    JFrame cookieGameWindow = new JFrame("Title");

    JPanel cookieHolder = new JPanel();

    JLabel cookieAmount = new JLabel(String.valueOf(amount));
    JLabel cookieImage = new JLabel();

    JButton clickMeasure = new JButton(String.valueOf(clickUpgradeCost));

    ImageIcon image = new ImageIcon();

    public void frameConfig(int width, int height) {
        this.width = width;
        this.height = height;
        cookieGameWindow.setSize(width, height);
        cookieGameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cookieGameWindow.setVisible(true);
        cookieGameWindow.setResizable(false);

        cookieGameWindow.add(cookieHolder);
    }

    public void cookiePosOrganize() {
        cookieHolder.setLayout(new GridLayout(3, 1));

        cookieHolder.add(cookieAmount);
        cookieHolder.add(clickMeasure);
        cookieHolder.add(cookieImage);
    }

    public void amountLConfig() {
        cookieAmount.setForeground(Color.black);
        cookieAmount.setFont(new Font("Times New Roman", Font.PLAIN, 48));
        cookieAmount.setHorizontalAlignment(JLabel.CENTER);
        cookieAmount.setVerticalAlignment(JLabel.TOP);
    }

    public void cookieLConfig() {
        cookieImage.setIcon(new ImageIcon("C:\\Users\\cmostero\\Downloads\\Webp.net-resizeimage.png"));
        cookieImage.setIcon(new ImageIcon("C:\\Users\\byeta\\Downloads\\Webp.net-resizeimage.png"));
        cookieImage.setHorizontalAlignment(JLabel.CENTER);

        cookieImage.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            amount += cookieClickAmount;
            cookieAmount.setText(String.valueOf(amount));
            }
        });
    }

    public void cookieClickingConfig() {
        if(clickMeasure.getModel().isPressed()) {
            cookieUpgrade.clickerUpgrade(clickUpgradeCost, amount, cookieClickAmount);
            clickMeasure.setText(String.valueOf(clickUpgradeCost));
        }
    }


}

升级类 -

public class Upgrades {

    public void clickerUpgrade(double clickCost, double cookieTotal, double cookieClickAmount) {
        if(cookieTotal >= clickCost) {
            cookieTotal -= clickCost;
            cookieClickAmount += .25;
            clickCost *= (clickCost * .5);
        }
    }


}

标签: javaswing

解决方案


您需要运行一个 EventQueue 才能注册 MouseEvent。然后,您的主要课程将反映以下内容:

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

public class Main {
    public static void main(String args[]) {
         EventQueue.invokeLater(new Runnable() {
           @Override
           public void run() {
                Franels windows = new Franels();
                windows.frameConfig(900, 800);
                windows.cookiePosOrganize();
                windows.amountLConfig();
                windows.cookieLConfig();
                windows.cookieClickingConfig();
           }
        });
    }
}

推荐阅读