我的JButton被按下时什么都没做?

时间:2019-11-10 07:51:44

标签: java swing

当前,我创建了一个Cookie图像,当按下该图像时,其加倍数为1。我的问题出在我的CookieClickingConfig方法中,其中isPressed似乎不起作用。如果可以的话,请帮我弄清楚为什么,这很重要。谢谢!

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

主类-

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);
        }
    }


}

1 个答案:

答案 0 :(得分:0)

您需要运行一个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();
           }
        });
    }
}